排序算法中希尔排序和冒泡排序问题
#include<stdio.h>#include<math.h>
#include<stdlib.h>
#include<time.h>
#define M 30001
randnum(float a[])
{
int i;
for(i=1;i<M;i++)
a[i]=rand()%M;
}
int change1(char a[81])
{
int b=0,n,i;
for(i=0;a[i]!=0;i++);
n=i-1;
for(;i>1;i--)
b+=((int)pow(10,n+1-i))*(a[i-1]-48);
if(a[0]=='-')
b=b*(-1);
else
b+=((int)pow(10,n))*(a[0]-48);
return b;
}
insort(int a[30001])
{
int i,j,temp,temp1,n;
int count=0;
n=30001;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)
{
count=0;
if(a[j]>temp)
{
temp1=a[j+1];
a[j+1]=a[j];
a[j]=temp1;
count++;
}
if(count==0)
break;
}
}
}
selsort(int a[30001])
{
int i,j,temp;
for(i=1;i<30000;i++)
for(j=i+1;j<30001;j++)
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
bubsort(int a[30001])
{
int i,j,temp;
for(i=1;i<30001;i++)
for(j=30000;j>i;j--)
{
if(a[j-1]>a[j])
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
}
int partition(int a[30001],int low,int high)
{
int pr;
a[0]=a[low];
pr=a[low];
while(low<high)
{
while(low<high&&a[high]>=pr)
--high;
a[low]=a[high];
while(low<high&&a[low]<=pr)
++low;
a[high]=a[low];
}
a[low]=a[0];
return low;
}
quicksort(int a[M],int low,int high)
{
int pr;
if(low<high)
{
pr=partition(a,low,high);
quicksort(a,low,pr-1);
quicksort(a,pr+1,high);
}
}
quicksort(int a[30001])
{
quicksort(a,1,30000);
}
work1(char timep[2][30])
{
int A,a,B,b,margin;
char newmin[3],newsec[3],oldmin[3],oldsec[3];
newmin[0]=timep[0][14];
newmin[1]=timep[0][15];
newsec[0]=timep[0][17];
newsec[1]=timep[0][18];
oldmin[0]=timep[1][14];
oldmin[1]=timep[1][15];
oldsec[0]=timep[1][17];
oldsec[1]=timep[1][18];
newmin[2]=0;
newsec[2]=0;
oldmin[2]=0;
oldsec[2]=0;
A=change1(newmin);
a=change1(newsec);
B=change1(oldmin);
b=change1(oldsec);
margin=(B-A)*60+b-a;
return margin;
}
void mark(char timep[30])
{
time_t timep1;
int i;
time(&timep1);
for(i=0;ctime(&timep1)[i]!=0;i++)
timep[i]=ctime(&timep1)[i];
timep[i]=0;
}
main()
{
int a[30001],num,i,margin,n;
float t;
char timep[2][30];
time_t timep2;
printf("1:cha ru pai xu\n\n");
printf("2:xuan ze pai xu \n\n");
printf("3:mao pao pai xu \n\n");
printf("4:kuai su pai xu \n\n");
loop:printf("qing xuan ze xu hao \n");
scanf("%d",&num);
switch(num)
{
case 1:
mark(timep[0]);
randnum(a);
n=1;
insort(a);
break;
case 2:
mark(timep[0]);
randnum(a);
n=1;
selsort(a);
break;
case 3:
mark(timep[0]);
randnum(a);
n=1;
bubsort(a);
break;
case 4:
mark(timep[0]);
n=99;
for(i=0;i<n;i++)
{
randnum(a);
quicksort(a);
}
break;
default:
{
printf("ERROR,chong xin shu ru ");
goto loop;
}
}
time(&timep2);
for(i=0;ctime(&timep2)[i]!=0;i++)
timep[1][i]=ctime(&timep2)[i];
timep[1][i]=0;
margin=work1(timep);
for(i=1;i<30001;i++)
printf("%d ",a[i]);
printf("\n\n\nsuo xu de shi jian wei%.3f miao ",((float)margin)/n);
}
最后出现的 有下列问题:1.
猜疑属实的指针转换中的主要功能
2.时参数太少“排序”中的主要功能