# include <stdio.h>
# define MAXSIZE 20
typedef int KeyType;
typedef struct {
KeyType key;
// InfoType otherinfo;
}RedType;
typedef struct{
RedType r[MAXSIZE+1];
int length ;
}SqList;
void swap(int &a,int &b){
int m;
m=a;
a=b;
b=m;
}
int partition( SqList &L,int low ,int high){
int pivotkey=L.r[low].key;
while(low<high){
while(low<high&&L.r[high].key>=pivotkey)high--;
swap(L.r[low].key,L.r[high].key );
while(low<high&&L.r[low].key<=pivotkey)low++;
swap(L.r[low].key,L.r[high].key );
}
return low;
}
void Qsort(SqList &L,int low,int high){
if(low<high){
int pivotloc=partition(L, low , high);
Qsort(L,low,pivotloc-1);
Qsort(L,pivotloc+1,high);
}
}
main (){
SqList L;
L.r[1].key=5;
L.r[2].key=3;
L.r[3].key=8;
L.r[4].key=6;
L.r[5].key=1;
L.r[6].key=2;
L.r[7].key=4;
L.r[8].key=7;
L.r[9].key=10;
L.r[10].key=9;
L.length=10;
Qsort(L,1,10);
for(int i=1;i<L.length;i++)
printf(" %d ",L.r[i].key);
}
快速排序
最新推荐文章于 2023-07-12 10:59:32 发布
