forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quicksort.java Add Quicksort Algorithm Closes NITSkmOS#150
- Loading branch information
Federico Sassone
committed
Oct 31, 2018
1 parent
64cd25e
commit b5f755c
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |