排序的各种方法
#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].key 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 {i=low;j=high;l.r[0]=l.r[i]; do { while(i --j; if(i {l.r[i]=l.r[j];++i;} while(i ++i; if(i 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 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 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 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(" please input five elements: "); for(i=1;i { scanf("%d",&a[i]); l.length++; } getchar(); do { for(j=1,i=1;j l.r[i].key=a[j]; printf(" Welcome to use PanWeiFeng's KeChenSheJi "); printf("s:shellsortq:quicksort "); printf("h:heapsorte:oesort "); printf("o:quit "); printf("please input the way:"); ch=getchar(); clrscr(); printf(" the orignal array:"); for(i=1;i printf("%d ",l.r[i].key); printf(" "); /*printf("please input the way:"); ch=getchar();*/ getchar(); switch(ch) { case 's': shellsort(l,l.length); printf(" the odered arry:"); for(i=1;i printf("%d ",l.r[i].key); printf(" "); break; case 'q': low=1;high=l.length; quicksort(l,low,high); printf(" the odered arry:"); for(i=1;i printf("%d ",l.r[i].key); printf(" "); break; case 'h': heapsort(l); printf(" the odered arry:"); for(i=1;i printf("%d ",l.r[i].key); printf(" "); break; case 'e': oesort(l,l.length); printf(" the odered arry:"); for(i=1;i printf("%d ",l.r[i].key); printf(" "); break; case 'o': exit(0); default: printf(" error!write again! "); } }while(1); } 点这里下载





