程序截图如下:
截图一
截图二
程序的源代码如下:
#include<stdio.h>
#include<malloc.h>
typedef struct{
int key;
int information;
}redtype;
typedef struct{
redtype *pkey;
int length;
}sqlist;
void innitarr(sqlist &L)
{
L.pkey =(redtype*)malloc(200*sizeof(redtype));
if(!L.pkey)printf("未分配存储空间\n");
printf("请输入集合的元素,输入110结束输入\n");
int elem;
scanf("%d",&elem);
int i=1;
for(i=1;i<200&&elem!=110;i++)//输入199个元素数组满,停止输入。输入110终止信息,停止输入
{
L.pkey[i].key=elem;
printf("请输入下一个元素,输入110结束输入\n");
scanf("%d",&elem);
}
L.length=i-1;
}
int patition(sqlist &L,intlow,int hign)
{
intprivotkey;
L.pkey[0]=L.pkey[low];
privotkey=L.pkey[low].key;
while(low<hign)
{
while(low<hign&&L.pkey[hign].key>=privotkey)
--hign;
L.pkey[low]=L.pkey[hign];
while(low<hign&&L.pkey[low].key<=privotkey)
++low;
L.pkey [hign]=L.pkey [low];
}
L.pkey[low]=L.pkey[0];
return low;
}
void qsort(sqlist &L,intlow,int hign)
{
intpivortloc;
if(low<hign)
{
pivortloc=patition(L,low,hign);
qsort(L,low,pivortloc-1);
qsort(L,pivortloc+1,hign);
}
}
void show(sqlist L)
{
for(int i=1;i<=L.length;i++)
{
printf("%d",L.pkey[i].key);
printf("\t");
}
printf("\n");
}
void quicksort(sqlist &L)
{
qsort(L,1,L.length);
}
void main()
{
sqlist L;
innitarr(L);
printf("快速排序前的集合\n");
show(L);
quicksort(L);
printf("快速排序后的集合\n");
show(L);
}