快排

来源:互联网 发布:湖北大学知行学院 编辑:程序博客网 时间:2024/04/27 17:26
#include <stdio.h>


int a[10] = {3, 4, 2, 9, 8, 1, 7, 0, 5};


int partition(int *a, int low, int high)
{
int temp = a[low];
while(low < high)
{
while((low < high) && (a[high] >= temp))
{
--high;
}
a[low] = a[high];
while((low < high) && (a[low] <= temp))
{
++low;
}
a[high] = a[low];
}
a[low] = temp;
return low;
}


void print(int *a)
{
for(int i = 0; i < 9; ++i)
{
printf("%d ", *a++);
}
printf("\n");
}


void QSort(int *a, int low, int high)
{
if(low < high)
{
int pivot = partition(a, low, high);
print(a);
QSort(a, low, pivot - 1);
QSort(a, pivot + 1, high);
}
}


int main()
{
a[9] = '\0';
QSort(a, 0, 8);
print(a);
return 0;
}