顺道练习了一下快速排序,^_^,以前还没写过,今天总算是看明白了。。。
程序代码:
#include<stdio.h>
#define N 10
int quicksort(int a[], int left, int right);
void main()
{
int i;
int a[N];
for(i = 0;i < N; i++)
scanf("%d", &a[i]);
quicksort(a, 0, N-1);
for(i = 0;i < N; i++)
printf("%3d", a[i]);
}
int quicksort(int a[], int left, int right)
{
int i,j,temp;
i = left;
j = right;
temp = a[left];
if(left > right)
return;
while(i != j)
{
while(i < j && a[j] >= temp)
j--;
if(i < j)
a[i++] = a[j];
while(i < j && a[i] <= temp)
i++;
if(i < j)
a[j--] = a[i];
}
a[i] = temp;
quicksort(a, left, i-1);
quicksort(a, i+1, right);
}