Skip to content

Instantly share code, notes, and snippets.

@drewhoener
Created May 2, 2017 03:17
Show Gist options
  • Save drewhoener/a735bc8caf9c27667b2b25291fc5de6a to your computer and use it in GitHub Desktop.
Save drewhoener/a735bc8caf9c27667b2b25291fc5de6a to your computer and use it in GitHub Desktop.
public QuickSorter(SwapList<T> list, Comparator<T> comparator) {
super(list, comparator);
}
@Override
public SwapList<T> sort() {
//recQuickSort(0, list.size() - 1);
recQuickSort(0, list.size() - 1);
return this.list;
}
protected void recQuickSort(int low, int high) {
if (low < high) {
int p = partition(low, high); // p is split point
recQuickSort(low, p - 1);
recQuickSort(p + 1, high);
}
}
public int partition(int low, int high) {
this.list.swap((low + high) / 2, high);
int pivot = high;
int storeIndex = low;
for (int j = low; j <= high - 1; j++) {
if (list.compare(j, pivot, comparator) <= 0) {
list.swap(j, storeIndex);
storeIndex++;
}
}
list.swap(storeIndex, high);
return storeIndex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment