2019-06-11 16:26:24 +08:00
|
|
|
/* mbinary
|
|
|
|
#########################################################################
|
|
|
|
# File : quickSort.c
|
|
|
|
# Author: mbinary
|
|
|
|
# Mail: zhuheqin1@gmail.com
|
|
|
|
# Blog: https://mbinary.xyz
|
|
|
|
# Github: https://github.com/mbinary
|
|
|
|
# Created Time: 2019-04-16 09:41
|
|
|
|
# Description:
|
|
|
|
#########################################################################
|
|
|
|
*/
|
2020-04-15 12:28:20 +08:00
|
|
|
int partition(int *arr, int i, int j)
|
2018-12-29 19:30:21 +08:00
|
|
|
{
|
2020-04-15 12:28:20 +08:00
|
|
|
int pivot = arr[j], p = i, q = j;
|
|
|
|
|
|
|
|
while (p < q) {
|
|
|
|
while (p < q && arr[p] <= pivot)++p;
|
|
|
|
|
|
|
|
if (p < q)arr[q--] = arr[p];
|
|
|
|
|
|
|
|
while (p < q && arr[q] > pivot)--q;
|
|
|
|
|
|
|
|
if (p < q)arr[p++] = arr[q];
|
2018-12-29 19:30:21 +08:00
|
|
|
}
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
arr[p] = pivot;
|
2018-12-29 19:30:21 +08:00
|
|
|
return p;
|
|
|
|
}
|
2020-04-15 12:28:20 +08:00
|
|
|
void quickSort(int *arr, int i, int j)
|
2018-12-29 19:30:21 +08:00
|
|
|
{
|
2020-04-15 12:28:20 +08:00
|
|
|
if (i < j) {
|
|
|
|
int p = partition(arr, i, j);
|
|
|
|
quickSort(arr, i, p - 1);
|
|
|
|
quickSort(arr, p + 1, j);
|
2018-12-29 19:30:21 +08:00
|
|
|
}
|
|
|
|
}
|