使用递归快排
# include<stdio.h>
void Quicksort(int *, int, int);
int Findpos(int *, int, int);
int main(void)
{
int a[8] = {1, 4, 5, 9, 3, 0, 12, -5};
Quicksort(a, 0, 7);
for(int i = 0; i < 8; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
void Quicksort(int *a, int low, int high)
{
if(low < high)
{
int pos = Findpos(a, low, high);
Quicksort(a, low, pos-1);
Quicksort(a, pos+1, high);
}
return;
}
int Findpos(int *a, int low, int high)
{
int val = a[low];
while(low < high)
{
while(low < high && a[high] >= val)
high--;
a[low] = a[high];
while(low < high && a[low] <= val)
low++;
a[high] = a[low];
}
a[low] = val;
return high;
}
运行结果