Skip to content

Commit

Permalink
quicksort.java Add Quicksort Algorithm Closes NITSkmOS#150
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Sassone committed Oct 31, 2018
1 parent 64cd25e commit b5f755c
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 b5f755c

Please sign in to comment.