#include "stdio.h"
#include "malloc.h"
#include "conio.h"
#define maxsize 5
typedef strUCt{
int key;
}redtype;
typedef struct{
redtype *r;
int length;
}sqlist;
;
;
;
void shellsort(sqlist l,int d)
{
int i,j;
d=l.length/2;
while(d>0)
{
for(i=d+1;i<=l.length;++i)
if(l.r[i].key<l.r[i-d].key)
{
l.r[0]=l.r[i];
for(j=i-d;j>0&&l.r[0].key<l.r[j].key;j-=d)
l.r[j+d]=l.r[j];
l.r[j+d]=l.r[0];}
d=d/2;}
}
;
;
;
void quicksort(sqlist l,int low,int high)
{int i,j;
if(low<high)
{i=low;j=high;l.r[0]=l.r[i];
do
{
while(i<j&&l.r[j].key>l.r[0].key)
--j;
if(i<j)
{l.r[i]=l.r[j];++i;}
while(i<j&&l.r[i].key<=l.r[0].key)
++i;
if(i<j){
l.r[j]=l.r[i];--j;
}
}while(i!=j);
l.r[i]=l.r[0];
quicksort(l,low,i-1);
quicksort(l,i+1,high);
}
}
;
;
;
void heapadjust(sqlist l,int s,int m)
{
int rc,j;
rc=l.r[s].key;
for(j=2*s;j<=m;j*=2)
{
if(j<m&&l.r[j].key<l.r[j+1].key)
j++;
if(rc>l.r[j].key)
break;
l.r[s].key=l.r[j].key;
s=j;
}
l.r[s].key=rc;
}
;
;
;
void heapsort(sqlist l)
{
int i,t;
for(i=l.length/2;i>0;i--)
heapadjust(l,i,l.length);
for(i=l.length;i>1;i--)
{
t=l.r[1].key,l.r[1].key=l.r[i].key,l.r[i].key=t;
heapadjust(l,1,i-1);
}
}
;
;
;
void oesort(sqlist l,int n)
{
int t,i,change;
change=1;
while(change)
{
change=0;
for(i=1;i<n;i+=2)
if(l.r[i].key>l.r[i+1].key)
{
t=l.r[i].key,l.r[i].key=l.r[i+1].key,l.r[i+1].key=t;
change=1;
}
for(i=2;i<n;i+=2)
if(l.r[i].key>l.r[i+1].key)
{
t=l.r[i].key,l.r[i].key=l.r[i+1].key,l.r[i+1].key=t;
change=1;
}
}
}
;
;
;
main()
{
int i,j,low,high,a[maxsize+1];
char ch;
sqlist l;
clrscr();
l.r=(redtype *)malloc(maxsize*sizeof(int));
if(!l.r)
printf("overflow");
l.length=0;
printf("\n\nplease input five elements:\n\n");
for(i=1;i<maxsize+1;i++)
{
scanf("%d",&a[i]);
l.length++;
}
getchar();
do
{
for(j=1,i=1;j<maxsize+1;i++,j++)
l.r[i].key=a[j];
printf("\n\nWelcome to use PanWeiFeng's KeChenSheJi\n\n");
printf("s:shellsort\tq:quicksort\n\n");
printf("h:heapsort\te:oesort\n\n");
printf("o:quit\n\n");
printf("please input the way:");
ch=getchar();
clrscr();
printf("\n\nthe orignal array:");
for(i=1;i<maxsize+1;i++)
printf("%d ",l.r[i].key);
printf("\n\n");
/*printf("please input the way:");
ch=getchar();*/
getchar();
switch(ch)
{
case 's':
shellsort(l,l.length);
printf("\n\nthe odered arry:");
for(i=1;i<maxsize+1;i++)
printf("%d ",l.r[i].key);
printf("\n\n");
break;
case 'q':
low=1;high=l.length;
quicksort(l,low,high);
printf("\n\nthe odered arry:");
for(i=1;i<maxsize+1;i++)
printf("%d ",l.r[i].key);
printf("\n\n");
break;
case 'h':
heapsort(l);
printf("\n\nthe odered arry:");
for(i=1;i<maxsize+1;i++)
printf("%d ",l.r[i].key);
printf("\n\n");
break;
case 'e':
oesort(l,l.length);
printf("\n\nthe odered arry:");
for(i=1;i<maxsize+1;i++)
printf("%d ",l.r[i].key);
printf("\n\n");
break;
case 'o':
exit(0);
default:
printf("\n\nerror!write again!\n");
}
}while(1);
}
点这里下载