一:插入排序
二:选择排序
三:冒泡排序
四:归并排序
五:快速排序
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<windows.h>
#include<sys\timeb.h>
using namespace std;
long long getSystemTime(){
timeb t;
ftime(&t);
return t.time * 1000 + t.millitm;
}
void Insert_sort(int nums[],int n)
{
int temp;
for (int i = 1; i<n; i++)
{
for (int k = i; k>0; k--)
{
if (nums[k]<nums[k - 1])
{
temp=nums[k];
nums[k]=nums[k-1];
nums[k-1]=temp;
}
else
break;
}
}
}
void Bubble_sort(int nums[],int n)
{
int flag,temp;
for(int i=n-1;i>0;i--)
{
flag=1;
for(int k=0;k<i;k++)
{
if(nums[k]>nums[k+1])
{
temp=nums[k];
nums[k]=nums[k+1];
nums[k+1]=temp;
flag=0;
}
}
if(flag==1)
break;
}
}
void Select_sort(int nums[],int n)
{
int flag,temp;
for(int i=0;i<n;i++)
{
flag=i;
for(int k=i;k<n-1;k++)
{
if(nums[flag]>nums[k+1])
flag=k+1;
}
temp=nums[i];
nums[i]=nums[flag];
nums[flag]=temp;
}
}
void Merge_sort(int nums[],int start_loc,int end_loc)
{
int temp1;
if(end_loc-start_loc>1)
{
Merge_sort(nums,start_loc,(start_loc+end_loc)/2);
Merge_sort(nums,((start_loc+end_loc)/2)+1,end_loc);
//Merge
int *temp=new int[end_loc-start_loc+1];
int *p1=nums+start_loc,*p2=nums+(end_loc+start_loc)/2+1,*p3=temp;
while(1)
{
if(p1 >= (nums+(end_loc+start_loc)/2+1))
{
while(p2<nums+end_loc+1)
{
*p3=*p2;
p2++;
p3++;
}
break;
}
if(p2 >= (nums+end_loc+1))
{
while(p1<nums+(end_loc+start_loc)/2+1)
{
*p3=*p1;
p1++;
p3++;
}
break;
}
if(*p1<*p2)
{
*p3=*p1;
p3++;
p1++;
}
else
{
*p3=*p2;
p3++;
p2++;
}
}
for(int i=0;i<end_loc-start_loc+1;i++)
{
nums[start_loc+i]=temp[i];
}
delete [] temp;
}
else
{
if(((end_loc-start_loc)!=0) && nums[start_loc]>nums[end_loc])
{
temp1=nums[start_loc];
nums[start_loc]=nums[end_loc];
nums[end_loc]=temp1;
}
}
}
void Quick_sort(int nums[],int start_loc,int end_loc)
{
int temp;
if(end_loc-start_loc>0)
{
int i=start_loc,j=end_loc,flag=0;
while(i<j)
{
if(nums[i]>nums[j])
{
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
if(flag==0)
{
i++;
flag=1;
}
else
{
j--;
flag=0;
}
}
else
{
if(flag==0)
{
j--;
}
else
{
i++;
}
}
}
Quick_sort(nums,start_loc,i-1);
Quick_sort(nums,i+1,end_loc);
}
}
void test()
{
int contec=100;
int *a=new int[contec];
for(int i=0;i<contec;i++)
{
a[i]=rand()%10000;
cout<<a[i]<<" ";
}
cout<<endl;
Merge_sort(a,0,contec-1);
for(int i=0;i<contec;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int main()
{
int n,situation,a[5]={10000,20000,30000,40000,50000},t=20;
double time=0,total_time=0;
int *nums=new int[100000001];
while(1)
{
cout<<"请选择要排序的方式: 1:插入排序 2:直接选择排序 3:冒泡排序 4:归并排序 5:快速排序\n";
cin>>situation;
for(int i=0;i<5;i++)
{
t=20;
total_time=0;
cout<<"当数据规模为"<<a[i]<<"时,20组样例的时间(单位:ms)分别为:\n";
while(t--)
{
for(int k=0;k<a[i];k++)
{
nums[k]=(rand()%a[i]);
}
switch(situation)
{
case 1:
{
time=getSystemTime();
Insert_sort(nums,a[i]);
time=getSystemTime()-time;
cout<<time<<" ";
total_time+=time;
break;
}
case 2:
{
time=getSystemTime();
Select_sort(nums,a[i]);
time=getSystemTime()-time;
cout<<time<<" ";
total_time+=time;
break;
}
case 3:
{
time=getSystemTime();
Bubble_sort(nums,a[i]);
time=getSystemTime()-time;
cout<<time<<" ";
total_time+=time;
break;
}
case 4:
{
time=getSystemTime();
Merge_sort(nums,0,a[i]-1);
time=getSystemTime()-time;
cout<<time<<" ";
total_time+=time;
break;
}
case 5:
{
time=getSystemTime();
Quick_sort(nums,0,a[i]-1);
time=getSystemTime()-time;
cout<<time<<" ";
total_time+=time;
break;
}
}
}
cout<<endl<<"平均时间为:"<<total_time/20<<"ms"<<endl;
}
cout<<endl;
}
delete []nums;
}