一聚教程网:一个值得你收藏的教程网站

热门教程

排序的各种方法

时间:2022-07-02 11:01:06 编辑:袖梨 来源:一聚教程网


#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[0]=l.r[i];
for(j=i-d;j>0&&l.r[0].keyl.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{i=low;j=high;l.r[0]=l.r[i];
do
{
while(il.r[0].key)
--j;
if(i{l.r[i]=l.r[j];++i;}
while(i++i;
if(il.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(jj++;
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;iif(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;iif(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("nnplease input five elements:nn");
for(i=1;i{
scanf("%d",&a[i]);
l.length++;
}
getchar();
do
{
for(j=1,i=1;jl.r[i].key=a[j];
printf("nnWelcome to u