Skip to content

Instantly share code, notes, and snippets.

@gennad
Created January 23, 2011 10:02
Show Gist options
  • Save gennad/791954 to your computer and use it in GitHub Desktop.
Save gennad/791954 to your computer and use it in GitHub Desktop.
int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
return i;
}
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