编程!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!急!!!!!!!!!!!!!!!!!

2024-11-30 23:53:01
推荐回答(2个)
回答1:

给你抄了段排序的
#include "stdio.h "
#include "malloc.h "
#include "conio.h "
#define maxsize 10
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) //快速排序,该方式可以直接调用库函数qsort
{int i,j;
if(low {i=low;j=high;l.r[0]=l.r[i];
do
{
while(i l.r[0].key)
--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( "\n\nplease input five elements:\n\n ");
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( "\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 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 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 printf( "%d ",l.r[i].key);
printf( "\n\n ");
break;
case 'h ':
heapsort(l);
printf( "\n\nthe odered arry: ");
for(i=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 printf( "%d ",l.r[i].key);
printf( "\n\n ");
break;
case 'o ':
exit(0);
default:
printf( "\n\nerror!write again!\n ");
}
}while(1);
}

回答2: