Skip to content

Instantly share code, notes, and snippets.

@gtke
Created October 25, 2012 03:09
Show Gist options
  • Select an option

  • Save gtke/3950225 to your computer and use it in GitHub Desktop.

Select an option

Save gtke/3950225 to your computer and use it in GitHub Desktop.
QuickSort java implementation
public static int partition(int arr[], int left, int right){
int i = left, j = right;
int temp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
};
return i;
}
public static void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSort(arr, left, index - 1);
if (index < right)
quickSort(arr, index, right);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment