Created
June 30, 2022 07:22
-
-
Save ssrlive/596172a6afc52b85928d349b2c0454bc to your computer and use it in GitHub Desktop.
quick sort in C
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
// 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