各内部排序算法时间复杂度比较

各内部排序算法时间复杂度比较,输入待排序的数字数目,可得到各算法排序所耗费时间,如100、10000、1000000

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
//获取随机序列 
void get_data(int r[],int n)
{int i;
 srand( (unsigned)time( NULL ) );
 for(i=1;i<=n;i++)r[i]=rand()%100;
 }
//获取逆序序列 
void reverse_data(int r[],int n)
{int i;
 for(i=1;i<=n;i++)r[i]=n-i;
} 
//获取正序序列 
void positive_data(int r[],int n)
{int i;
 for(i=1;i<=n;i++)r[i]=i;
}
//输出序列 
 void put_data(int r[],int n)
{int i;
 printf("\n");
 for(i=1;i<=n;i++){printf("%4d",r[i]);if(!(i%10))printf("\n");}
}


//直接插入 
void simple_ins_sort(int r[],int n)
{int i,j;
 for(i=2;i<=n;i++){
	r[0]=r[i];
	for(j=i-1;r[j]>r[0];j--)r[j+1]=r[j];
	r[j+1]=r[0];
	}
}

//折半插入 
void bin_ins_sort(int r[],int n)
{int i,j,low,high,mid;
 for(i=2;i<=n;i++)
  {r[0]=r[i];
   low=1;high=i-1;
   while(low<=high)
    {mid=(low+high)/2;
     if(r[0]>=r[mid])low=mid+1;
     else high=mid-1;
    }
   for(j=i-1;j>=high+1;j--)r[j+1]=r[j];
   r[j+1]=r[0];
  }
 }


//希尔排序 
void shell_sort(int r[],int n){
	int i,j,d=n/2;
	while(d){
		for(i=1+d;i<=n;i++){
			r[0]=r[i];
			for(j=i-d;j>0&&r[j]>r[0];j-=d)r[j+d]=r[j];
			r[j+d]=r[0];
		}
    	d/=2;
    }
}

//冒泡排序 
void bubble_sort(int r[],int n)
{int i,j,flag=1;
 for(i=1;flag&&i<n;i++)
    {flag=0;//x++;
     for(j=1;j<=n-i;j++)
	if(r[j]>r[j+1])
	  {r[0]=r[j];r[j]=r[j+1];r[j+1]=r[0];flag=1;}
    }
}


//快速排序 
int quickpass(int r[],int s,int t)
{int low=s,high=t;
 r[0]=r[s];
 while(low<high)
  {while(low<high&&r[high]>=r[0])high--;
   if(low<high){r[low]=r[high];low++;}
   while(low<high&&r[low]<=r[0])low++;
   if(low<high){r[high]=r[low];high--;}
  }
  r[low]=r[0];
 return low;
} 

void QSort(int r[],int low,int high){
	if(low<high){
		int pivotloc = quickpass(r,low,high);
        QSort(r,low,pivotloc - 1);
        QSort(r,pivotloc + 1,high);
	}
}
//简单选择排序 
void simple_sele_sort(int r[],int n)
{int i,j,pos;
 for(i=1;i<n;i++){
	pos=i;
	for(j=i+1;j<=n;j++)
			if(r[j]<r[pos])pos=j;
		if(i!=pos){r[0]=r[i];r[i]=r[pos];r[pos]=r[0];}
	}
}

//堆选择排序 
void sift(int r[],int low,int high)
{int i,j;
 r[0]=r[low];
 i=low;j=2*i;
 while(j<=high)
 {if(j<high&&r[j]<r[j+1])j++;
  if(r[j]>r[0])
  {r[i]=r[j];i=j;j=2*i;}
  else break;
 }
 r[i]=r[0];
}
void heap_sort(int r[],int n)
{int i;
 for(i=n/2;i>=1;i--)sift(r,i,n);
 for(i=n;i>1;i--)
 {r[0]=r[1];
  r[1]=r[i];
  r[i]=r[0];
  sift(r,1,i-1);
 }
}
//归并 
void merge(int r[],int r1[],int low,int mid,int high)
{int i=low,j=mid+1,k=low;
 while(i<=mid&&j<=high)
	 if(r[i]<=r[j])r1[k++]=r[i++];
	 else r1[k++]=r[j++];
 while(i<=mid)r1[k++]=r[i++];
 while(j<=high)r1[k++]=r[j++];
}
void mergepass(int r[],int r1[],int len,int n)
{int i=1;
 while(i+2*len-1<=n)
 {merge(r,r1,i,i+len-1,i+2*len-1);
 i+=2*len;}
 if(i+len-1<n)merge(r,r1,i,i+len-1,n);
 else
	 while(i<=n){r1[i]=r[i];i++;}
}
void mergesort(int r[],int n)
{int *r1,len=1;
 r1=(int*)malloc(sizeof(int)*(n+1));
 while(len<n)
 {mergepass(r,r1,len,n);
  len*=2;
  mergepass(r1,r,len,n);
  len*=2;}
}



int main(){
double  duration;
clock_t start, finish;
int i,n;
int *r;
printf("请输入待排序的数字数目:"); 
scanf("%d",&n);
r=(int*)malloc(n*sizeof(int));

//直接插入 
reverse_data(r,n);
//put_data(r,n);
start = clock();
simple_ins_sort(r,n);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "直接插入 :%f seconds\n", duration ); 

//折半插入 
reverse_data(r,n);
//put_data(r,n);
start = clock();
bin_ins_sort(r,n);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "折半插入 :%f seconds\n", duration );

//希尔排序 
reverse_data(r,n);
//put_data(r,n);
start = clock();
shell_sort(r,n);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "希尔排序 :%f seconds\n", duration );

//冒泡排序 
reverse_data(r,n);
//put_data(r,n);
start = clock();
bubble_sort(r,n);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "冒泡排序 :%f seconds\n", duration );

//快速排序 
reverse_data(r,n);
//put_data(r,n);
start = clock();
QSort(r,0,n); 
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "快速排序 :%f seconds\n", duration );

//简单选择排序 
reverse_data(r,n);
//put_data(r,n);
start = clock();
simple_sele_sort(r,n);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "简单选择排序 :%f seconds\n", duration ); 

//堆选择排序 
reverse_data(r,n);
//put_data(r,n);
start = clock();
heap_sort(r,n);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "堆选择排序 :%f seconds\n", duration );
//归并排序 
reverse_data(r,n);
//put_data(r,n);
start = clock();
mergesort(r,n);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;//put_data(r,n);
printf( "归并排序 :%f seconds\n", duration );
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值