Created
July 5, 2013 21:28
-
-
Save cocodrips/5937375 to your computer and use it in GitHub Desktop.
quickSort in Java
This file contains hidden or 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
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