Skip to content

Instantly share code, notes, and snippets.

@arunma
Last active December 17, 2015 22:49
Show Gist options
  • Select an option

  • Save arunma/5684627 to your computer and use it in GitHub Desktop.

Select an option

Save arunma/5684627 to your computer and use it in GitHub Desktop.
Quick Sort Basic
package basics.sorting.quick;
import static basics.sorting.utils.SortUtils.exchange;
import static basics.sorting.utils.SortUtils.less;
import basics.shuffle.KnuthShuffle;
public class QuickSortBasic {
public void sort (int[] input){
//KnuthShuffle.shuffle(input);
sort (input, 0, input.length-1);
}
private void sort(int[] input, int lowIndex, int highIndex) {
if (highIndex<=lowIndex){
return;
}
int partIndex=partition (input, lowIndex, highIndex);
sort (input, lowIndex, partIndex-1);
sort (input, partIndex+1, highIndex);
}
private int partition(int[] input, int lowIndex, int highIndex) {
int i=lowIndex;
int pivotIndex=lowIndex;
int j=highIndex+1;
while (true){
while (less(input[++i], input[pivotIndex])){
if (i==highIndex) break;
}
while (less (input[pivotIndex], input[--j])){
if (j==lowIndex) break;
}
if (i>=j) break;
exchange(input, i, j);
}
exchange(input, pivotIndex, j);
return j;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment