1 #include<stdio.h>
2  #define MAXSIZE 20 //顺序表的最大长度
3  #define LT(a,b) ((a)< (b))
4 typedef int KeyType;
5 typedef struct{
6 KeyType key; //关键字项
7 //InfoType otherinfo;//其他数据项
8 }RedType; //记录类型
9 typedef struct{
10 RedType r[MAXSIZE+1]; //r[0]闲置或用作哨兵单元
11 int length; //顺序表长度
12 }SqList; //顺序表类型
13
14 /*void InsertSort(SqList &L){
15 for(int i=2;i<=L.length;++i)
16 {
17 if(LT(L.r[i].key,L.r[i-1].key)){ //将L.r[i]插入到有序子表;
18 L.r[0]=L.r[i]; //复制为哨兵;
19 L.r[i]=L.r[i-1];
20 for(int j=i-2; LT(L.r[0].key,L.r[j].key); --j)
21 L.r[j+1]=L.r[j]; //记录后移
22 L.r[j+1]=L.r[0];
23 }
24
25 }
26
27 }//InsertSort*/
28
29 int Partition(SqList &L,int low,int high){
30
31 L.r[0]=L.r[low];
32 int pivotkey=L.r[low].key;
33 while(low<high){
34 while(low<high&&L.r[high].key>=pivotkey) --high;
35 L.r[low]=L.r[high];
36 while(low<high&&L.r[low].key<=pivotkey) ++low;
37 L.r[high]=L.r[low];
38 }
39 L.r[high]=L.r[0];
40 for(int k=1;k<=L.length;k++)
41 {
42 printf( "%d ",L.r[k].key);
43 }
44 printf("\n");//插入到正确位置
45 return low;
46 }
47
48 void QSort(SqList &L,int low,int high){
49
50 if(low<high){
51 int pivotloc=Partition(L,low,high);
52 QSort(L,low,pivotloc-1);
53 QSort(L,pivotloc+1,high);
54 }
55 }//QSort
56
57 int main()
58 {
59 int n;
60 SqList L;
61 scanf("%d",&n);
62 L.length=n;
63 for(int i=1;i<=n;i++)
64 scanf("%d",&L.r[i].key);
65 QSort(L,1,n);
66 return 1;
67

 

posted on 2010-04-13 21:37  KuSiuloong  阅读(221)  评论(0编辑  收藏  举报