Skip to content

Commit

Permalink
quicksort.java Add Quicksort Algorithm
Browse files Browse the repository at this point in the history
Returns the concatenation of the quicksorted list of elements
that are less than or equal to the pivot, the pivot, and the
quicksorted list of elements that are greater than the pivot

closes NITSkmOS#150
  • Loading branch information
Federico Sassone committed Oct 31, 2018
1 parent 64cd25e commit 28f4d61
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
51 changes: 51 additions & 0 deletions quicksort/Java/quicksort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 28f4d61

Please sign in to comment.