《c程序设计语言》读书笔记-递归实现快速排序算法

时间:2024-07-28 11:34:20
#include <stdio.h>

void swap(int v[],int i,int j)
{
int temp; temp = v[i];
v[i] = v[j];
v[j] = temp;
} void qsort(int v[],int left,int right)
{
int i,last; if(left >= right)
return;
swap(v,left,(left + right) / 2);
last = left;
for(i = left + 1;i <= right;i++)
if(v[i] < v[left])
swap(v,++last,i);
swap(v,left,last); qsort(v,left,last - 1);
qsort(v,last + 1,right);
}
int main()
{ int v[10] = {10,9,8,7,6,5,4,3,2,1};
int i; qsort(v,0,9); for(i = 0;i < 10;i++)
printf("%d ",v[i]); return 0;
}