#include<stdio.h>
#define Listsize 50
#define MAXSIZE 20
#define KeyType int
typedef struct{
int r[MAXSIZE+1];
int length;
}Sqlist;
int Init(Sqlist *L){
L->length=0;
}
void create(Sqlist *L){
int i;
printf("input length;");
scanf("%d",&L->length);
for(i=1;i<=L->length;i++){
printf("input data:");
scanf("%d",&L->r[i]);
}
}
int Partition (Sqlist *L,int low,int high){
int pivotkey=L->r[low];
L->r[0]=L->r[low];
while(low<high){
while(low<high&&L->r[high]>=pivotkey)
--high;
L->r[low]=L->r[high];
while(low<high&&L->r[low]<=pivotkey)
++low;
L->r[high]=L->r[low];
}
L->r[low]=L->r[0];
return low;
}
void Qsort(Sqlist *L,int low,int high){
int pivotloc;
if(low<high){
pivotloc=Partition(L,low,high);
Qsort(L,low,pivotloc-1);
Qsort(L,pivotloc+1,high);
}
}
int print(Sqlist L){
int i;
for(i=1;i<=L.length;++i)
printf("%d/t",L.r[i]);
printf("/n");
return 1;
}
void main(){
Sqlist L;
Init(&L);
create(&L);
printf("Before sort:/n");
print(L);
Qsort(&L,1,L.length);
printf("After sort:/n");
print(L);
getch();
}
快速排序
最新推荐文章于 2021-12-04 00:03:02 发布