# 排序算法复习

mails:18200268879@163.com

#include <iostream>
#include <windows.h>
using namespace std;

void Bubble_Sort(int UnSort[],int length);
void Select_Sort(int UnSort[],int length);
void Insert_Sort(int UnSort[],int length);
void Merge_Sort(int UnSort[],int length);
void Quick_Sort(int UnSort[],int length);

int main()
{
int UnSort[10]={5,4,7,12,31,3,8,9,10,2};
cout<<"*****************************Vpoet************************************"<<endl;
cout<<"The Unsort Array is: ";
for(int i=0;i<10;i++)
{
cout<<UnSort[i]<<"  ";
}
cout<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"\t\t\t*********1.Bubble*********\t\t\t"<<endl;
cout<<"\t\t\t*********2.Select*********\t\t\t"<<endl;
cout<<"\t\t\t*********3.Insert*********\t\t\t"<<endl;
cout<<"\t\t\t*********4.Merge *********\t\t\t"<<endl;
cout<<"\t\t\t*********5.Quick *********\t\t\t"<<endl;
cout<<"\t\t\t*********0.Exit*********\t\t\t"<<endl;
cout<<"**********************************************************************"<<endl;
int ChooseIndex;
cout<<"Please choose the sort Algo:";
cin>>ChooseIndex;

switch(ChooseIndex)
{
case 1:
Bubble_Sort(UnSort,10);
break;
case 2:
Select_Sort(UnSort,10);
break;
case 3:
Insert_Sort(UnSort,10);
break;
case 4:
Merge_Sort(UnSort,10);
break;
case 5:
LARGE_INTEGER BegainTime ;
LARGE_INTEGER EndTime ;
LARGE_INTEGER Frequency ;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&BegainTime) ;
Quick_Sort(UnSort,10);
QueryPerformanceCounter(&EndTime);
cout<<"**********************************************************************"<<endl;
cout<<"You choosed Quick sort!"<<endl;
cout<<"The Sort Array is:  ";
for(i=0;i<10;i++)
{
cout<<UnSort[i]<<"  ";
}
cout<<endl;
cout << "Run Time："<<(double)( EndTime.QuadPart - BegainTime.QuadPart )/ Frequency.QuadPart <<endl;
cout<<"**********************************************************************"<<endl;
break;
case 0:
cout<<"You Choose To exit,GoodBye!"<<endl;
break;

}
return 1;
}

void Bubble_Sort(int UnSort[],int length)
{
LARGE_INTEGER BegainTime ;
LARGE_INTEGER EndTime ;
LARGE_INTEGER Frequency ;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&BegainTime) ;
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(UnSort[i]>UnSort[j])
{
int temp=UnSort[i];
UnSort[i]=UnSort[j];
UnSort[j]=temp;
}
}
}
QueryPerformanceCounter(&EndTime);
cout<<"**********************************************************************"<<endl;
cout<<"You choosed Buuble sort!"<<endl;
cout<<"The Sort Array is:  ";
for(i=0;i<10;i++)
{
cout<<UnSort[i]<<"  ";
}
cout<<endl;
cout << "Run Time："<<(double)( EndTime.QuadPart - BegainTime.QuadPart )/ Frequency.QuadPart <<endl;
cout<<"**********************************************************************"<<endl;

}

void Select_Sort(int UnSort[],int length)
{
LARGE_INTEGER BegainTime ;
LARGE_INTEGER EndTime ;
LARGE_INTEGER Frequency ;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&BegainTime) ;
int Min;
for(int i=0;i<10;i++)
{
Min=UnSort[i];
int index=i;
for(int j=i+1;j<10;j++)
{
if(Min>UnSort[j])
{
Min=UnSort[j];
index=j;
}
}
int temp=UnSort[i];
UnSort[i]=UnSort[index];
UnSort[index]=temp;
}
QueryPerformanceCounter(&EndTime);
cout<<"**********************************************************************"<<endl;
cout<<"You choosed Select sort!"<<endl;
cout<<"The Sort Array is:  ";
for(i=0;i<10;i++)
{
cout<<UnSort[i]<<"  ";
}
cout<<endl;
cout << "Run Time："<<(double)( EndTime.QuadPart - BegainTime.QuadPart )/ Frequency.QuadPart <<endl;
cout<<"**********************************************************************"<<endl;
}

void Insert_Sort(int UnSort[],int length)
{
LARGE_INTEGER BegainTime ;
LARGE_INTEGER EndTime ;
LARGE_INTEGER Frequency ;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&BegainTime) ;
for(int i=1;i<10;i++)
{
int j=i;
int temp=UnSort[i];
while(j>0&&temp<UnSort[j-1])
{
UnSort[j]=UnSort[j-1];
j--;
}
UnSort[j]=temp;
}
QueryPerformanceCounter(&EndTime);
cout<<"**********************************************************************"<<endl;
cout<<"You choosed Select sort!"<<endl;
cout<<"The Sort Array is:  ";
for(i=0;i<10;i++)
{
cout<<UnSort[i]<<"  ";
}
cout<<endl;
cout << "Run Time："<<(double)( EndTime.QuadPart - BegainTime.QuadPart )/ Frequency.QuadPart <<endl;
cout<<"**********************************************************************"<<endl;
}

void mergesortEnd(int Unsort[],int first,int mid,int last,int temp[])
{
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;

while (i <= m && j <= n)
{
if (Unsort[i] <= Unsort[j])
temp[k++] = Unsort[i++];
else
temp[k++] = Unsort[j++];
}

while (i <= m)
temp[k++] = Unsort[i++];

while (j <= n)
temp[k++] = Unsort[j++];

for (i = 0; i < k; ++i)
Unsort[first + i] = temp[i];
}

void mergesortfun(int UnSort[],int first,int last,int temp[])
{
if (first < last)
{
int mid = (first + last) / 2;
mergesortfun(UnSort, first, mid, temp);     //左边有序
mergesortfun(UnSort, mid + 1, last, temp);  //右边有序
mergesortEnd(UnSort, first, mid, last, temp); //再将两个有序数列合并
}
}

void Merge_Sort(int UnSort[],int length)
{
int *p = new int[length];
if (p == NULL)
return ;

LARGE_INTEGER BegainTime ;
LARGE_INTEGER EndTime ;
LARGE_INTEGER Frequency ;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&BegainTime) ;
mergesortfun(UnSort, 0, length - 1, p);
QueryPerformanceCounter(&EndTime);
delete[] p;

cout<<"**********************************************************************"<<endl;
cout<<"You choosed Merge sort!"<<endl;
cout<<"The Sort Array is:  ";
for(int i=0;i<10;i++)
{
cout<<UnSort[i]<<"  ";
}
cout<<endl;
cout << "Run Time："<<(double)( EndTime.QuadPart - BegainTime.QuadPart )/ Frequency.QuadPart <<endl;
cout<<"**********************************************************************"<<endl;
}

void Quick_Sort(int UnSort[],int length)
{
int i=0;
int j=length-1;
int key=UnSort[0];

if(length>1)
{
while(i!=j)
{
for(;j>i;j--)
{
if(UnSort[j]<key)
{
UnSort[i]=UnSort[j];
break;
}
}

for(;i<j;i++)
{
if(UnSort[i]>key)
{
UnSort[j]=UnSort[i];
break;
}
}

UnSort[i]=key;
}

Quick_Sort(UnSort,i);
Quick_Sort(UnSort+i+1,length-i-1);
}
}


04-11 2588

10-12 95

07-06 1231

07-30 143

09-14 2555

#### 七种常见的VB排序算法示例程序

2016年12月31日 12KB 下载

03-11 857

06-21 1101

#### 排序算法总结(常见算法总结分析)

2011年05月12日 151KB 下载

07-19 486