Skip to content

Instantly share code, notes, and snippets.

@ssrlive
Created June 30, 2022 07:22
Show Gist options
  • Save ssrlive/596172a6afc52b85928d349b2c0454bc to your computer and use it in GitHub Desktop.
Save ssrlive/596172a6afc52b85928d349b2c0454bc to your computer and use it in GitHub Desktop.
quick sort in C
// quick sort
void swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
// 快速排序
void quick_sort(int* arr, int left, int right)
{
int i = left, j = right;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(&arr[i], &arr[j]);
i++;
j--;
}
}
if (left < j) {
quick_sort(arr, left, j);
}
if (i < right) {
quick_sort(arr, i, right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment