原理:
#include <stdio.h>
2 void display(int p[]);
3 int sort(int p[],int low,int high);
4 void swap(int p[],int l,int h);
5 int main()
6 {
7 int s[10] = {2,1,5,4,3,6,9,8,7,10};
8 display(s);
9 sort(s,0,9);
10 display(s);
11 }
12
13 int sort(int p[],int low,int high)
14 {
15 int piv = low;
16 int l= low+1;
17 int h = high;
18 if(low < high)
19 {
20 while(l < h)
21 {
22 while(l < h && p[piv] <= p[h])
23 h--;
24 swap(p,l,h);
25 while(l < h && p[piv] >= p[l])
26 l++;
27 swap(p,l,h);
28 }
29 if(p[l] >= p[piv])
30 {
l--;
32 }
33 swap(p,piv,l);
34 sort(p,piv,l-1);
35 sort(p,l+1,high);
36 }
37 return l;
38
39 }
40
41 void swap(int p[],int l,int h)
42 {
43 int t = p[l];
44 p[l] = p[h];
45 p[h] = t;
46 return;
47 }
48 void display(int p[])
49 {
50 int i = 0;
51 for(;i < 10;i++)
52 printf("%-3d",p[i]);
53 printf("\n");
54 }