#include <stdio.h>
int partition(int *arr, int start, int end)
{
int p = start - 1;
int x = arr[end];
int temp = 0;
int i = 0;
for(i = start; i < end; i++)
{
if(arr[i] < x)
{
p = p + 1;
if(p != i)
{
temp = arr[i];
arr[i] = arr[p];
arr[p] = temp;
}
}
}
p = p + 1;
temp = arr[p];
arr[p] = x;
arr[end] = temp;
return p;
}
void qsort(int * arr, int start, int end)
{
if(start < end)
{
int p = partition(arr, start, end);
qsort(arr, start, p - 1);
qsort(arr, p + 1, end);
}
return;
}
void prt(int * arrNum, int size)
{
int i = 0;
for(i = 0; i< size; i++)
{
printf("%d,", arrNum[i]);
}
}
int main(void *arg, int nargs)
{
int arrNum[6] = {15,4,3,22,7,99};
int size = 6;
qsort(arrNum, 0, size -1);
printf("len=%d, ", size);
prt(arrNum, size);
printf("\n");
}
快速排序
最新推荐文章于 2021-07-24 23:27:25 发布