Skip to content

Instantly share code, notes, and snippets.

@codinko
Created October 30, 2016 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/fe9dae90c2d7d3727c002da1d8c0e4d8 to your computer and use it in GitHub Desktop.
Save codinko/fe9dae90c2d7d3727c002da1d8c0e4d8 to your computer and use it in GitHub Desktop.
// Sort an array using a simple but inefficient quicksort.
public int[] quicksortSimple(int[] data) {
if (data.length < 2) {
return data;
}
int pivotIndex = data.length / 2;
int pivotValue = data[pivotIndex];
int leftCount = 0;
// Count how many are less than the pivot
for (int i = 0; i < data.length; ++i) {
if (data[i] < pivotValue) ++leftCount;
}
// Allocate the arrays and create the subsets
int[] left = new int[leftCount];
int[] right = new int[data.length - leftCount - 1];
int l = 0;
int r = 0;
for (int i = 0; i < data.length; ++i) {
if (i == pivotIndex) continue;
int val = data[i];
if (val < pivotValue) {
left[l++] = val;
} else {
right[r++] = val;
}
}
// Sort the subsets
left = quicksortSimple(left);
right = quicksortSimple(right);
// Combine the sorted arrays and the pivot back into the original array
System.arraycopy(left, 0, data, 0, left.length);
data[left.length] = pivotValue;
System.arraycopy(right, 0, data, left.length + 1, right.length);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment