内部排序的程序如何修改?
下面这个程序运行时正确的,如何修改才能使输出的随机数至少有5组?望会的不吝赐教。谢谢了#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#include "limits.h"
#define MAXITEM 1000
typedef int KeyType,ElemType;
int count1=0,count2=0,count3=0,count4=0,count5=0;
int swap1=0,swap2=0,swap3=0,swap4=0,swap5=0;
typedef struct rec
{
KeyType key;
ElemType data;
}sqlist[MAXITEM];
void gennum(sqlist r,sqlist t,int n)
{
int i;
srand((unsigned)time(NULL));
for(i=1;i<=n;i++)
{ t[i].key=rand()%100;
r[i].key=t[i].key;
}
}
void ini(sqlist r,sqlist t,int n)
{
int i;
for(i=1;i<=n;i++)
r[i].key=t[i].key;
}
void QiPaoFa(sqlist r,int n)//冒炮法r[1]~r[n]
{
int i,j;
struct rec w;
for(i=1;i<=n-1;i++)
for(j=n;j>=i+1;j--)
{
if(r[j].key<r[j-1].key)
{
w=r[j];
r[j]=r[j-1];
r[j-1]=w;
swap1++;
}
count1++;
}
}
void ZhiJieChaRu(sqlist r,int n)//直接插入排序r[1]~r[n]
{
int i,j;
for(i=2;i<=n;i++)
{
count2++;
r[0]=r[i];
j=i-1;
while(r[0].key<r[j].key)
{
r[j+1]=r[j];
j--;
count2++;
swap2++;
}
r[j+1]=r[0];
swap2++;
}
}
void JianDanXuanZe(sqlist r,int n)//简单选择排序r[1]~r[n]
{
int i,j,k;
struct rec temp;
for(i=1;i<=n-1;i++)
{
k=i;
for(j=i+1;j<=n;j++)
if(r[j].key<r[k].key){k=j;count3++;}
if(i!=k)
{
temp=r[i];
r[i]=r[k];
r[k]=temp;
swap3++;
}
}
}
void KuaiSu(sqlist r,int s,int t)//快速排序r[s]~r[t],r[0]空出
{
int i=s,j=t;
if(s<t)
{
r[0]=r[s];swap4++;
do
{
while(j>i&&r[j].key>=r[0].key){j--;count4++;}
if(i<j)
{
r[i]=r[j];
i++;
swap4++;
}
while(i<j&&r[i].key<=r[0].key){i++;count4++;}
if(i<j)
{
r[j]=r[i];
j--;
swap4++;
}
}while(i<j);
r[i]=r[0];
swap4++;
KuaiSu(r,s,j-1);
KuaiSu(r,j+1,t);
}
}
void sift(sqlist r,int l,int m)
{
int i,j;
struct rec x;
i=l;
j=2*i;
x=r[i];
while(j<=m)
{
if(j<m&&r[j].key<r[j+1].key){j++;count5++;}
if(x.key<r[j].key)
{
r[i]=r[j];
i=j;
j=2*i;
count5++;
swap5++;
}
else {j=m+1;count5++;}
}
r[i]=x;
}
void DuiPaiXv(sqlist r,int n)//堆排序r[1]~r[n]
{
int i;
struct rec m;
for(i=n/2;i>=1;i--)sift(r,i,n);
for(i=n;i>=2;i--)
{
m=r[i];
r[i]=r[1];
r[1]=m;
swap5++;
sift(r,1,i-1);
}
}
main()
{
int k,n;
sqlist r,t;
printf("请输入要排序的数据的个数:");
scanf("%d",&n);
gennum(r,t,n);
printf("排序前的数:\n");
for(k=1;k<=n;k++)
{ printf("%3d",t[k].key);
if(k%20==0)
printf("\n");
}
QiPaoFa(r,n);
printf("\n 排序后的数:\n");
for(k=1;k<=n;k++)
{ printf("%3d",r[k].key);
if(k%20==0)
printf("\n");
}
printf("\n");
printf("冒泡排序:\n");
printf("比较次数= %d,移动次数= %d\n\n",count1,swap1);
ini(r,t,n);
ZhiJieChaRu(r,n);
printf("直接插入:\n");
printf("比较次数= %d,移动次数= %d\n\n",count2,swap2);
ini(r,t,n);
JianDanXuanZe(r, n);
printf("简单选择排序:\n");
printf("比较次数= %d,移动次数= %d\n\n",count3,swap3);
ini(r,t,n);
KuaiSu(r,1,n);
printf("快速排序:\n");
printf("比较次数= %d,移动次数= %d\n\n",count4,swap4);
ini(r,t,n);
DuiPaiXv(r,n);
printf("堆排序:\n");
printf("比较次数= %d,移动次数= %d\n\n",count5,swap5);
}