diff --git a/README.md b/README.md index 0c94e2fe..d0009fb1 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This repository contains examples of various algorithms written on different pro |:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:| | [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) | | [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | [:octocat:](merge_sort/Cpp) | [:octocat:](merge_sort/Java) | [:octocat:](merge_sort/Python) | -| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | | [:octocat:](quicksort/Python) | +| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | [:octocat:](quicksort/Java) | [:octocat:](quicksort/Python) | | [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) | | [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | [:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) | | [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:octocat:](radix_sort/Cpp) | | [:octocat:](radix_sort/Python) | diff --git a/quicksort/Java/quicksort.java b/quicksort/Java/quicksort.java new file mode 100644 index 00000000..a3069a65 --- /dev/null +++ b/quicksort/Java/quicksort.java @@ -0,0 +1,51 @@ +import java.util.Arrays; + +class quicksort { + //Main function. Instanciates the unsorted array and calls quicksort. + public static void main(String args[]) { + int array[] = {9, 5, 4, 6, 1, 2, 3, 1, 2, 3}; + System.out.println("The unsorted array is:"); + System.out.println(Arrays.toString(array)); + + quicksort(array, 0, array.length - 1); + + System.out.println("The sorted array is:"); + System.out.println(Arrays.toString(array)); + } + + //partition returns the array splitted on + // lesser and greater values than a pivot chosen randomly. + //(on this case, the array's first value) + //Then, quicksort is called recursively on both splits. + static void quicksort(int array[], int start, int end) { + if (start < end) { + int pIndex = partition(array, start, end); + quicksort(array, start, pIndex - 1); + quicksort(array, pIndex + 1, end); + } + } + + //Defines the pivot index at the start of array. + //Finds every element with lesser value and swaps places. + //At the end of the cycle: + // pivot splits the array on lesser and greater values. + static int partition(int array[], int start, int end) { + int pivot = array[end]; + int pIndex = start; + for (int i = start; i < end; i++) { + if (array[i] <= pivot) { + swap(array, i, pIndex); + pIndex++; + } + } + swap(array, pIndex, end); + return pIndex; + } + + //Swaps indexes 'a' and 'b' on array + static void swap(int array[], int a, int b) { + int temp = array[a]; + array[a] = array[b]; + array[b] = temp; + } +}