Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created July 5, 2013 21:28
Show Gist options
  • Save cocodrips/5937375 to your computer and use it in GitHub Desktop.
Save cocodrips/5937375 to your computer and use it in GitHub Desktop.
quickSort in Java
void quickSort(int[] array, int left, int right){
int index = partition(array, left, right);
if(left < index - 1){
quickSort(array, left, index - 1);
}
if(index < right){
quickSort(array, index, right);
}
}
int partition(int[] array, int left, int right){
int pivot = array[(left + right)/2];
while (left < right) {
while (array[left] < pivot) left++;
while(array[right] > pivot) right --;
if(left <= right){
int tmp = array[left];
array[left] = array[right];
array[right] = tmp;
left ++;
right --;
}
}
return left;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment