# 排序算法复习

432人阅读 评论(0)

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;
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<<"**********************************************************************"<<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<<"**********************************************************************"<<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<<"**********************************************************************"<<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<<"**********************************************************************"<<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<<"**********************************************************************"<<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);
}
}


1
0

《我们终将独自长大》

有一天你将破蛹而出，成长得比人们期待的还要美丽，但这个过程会很痛，会很辛苦，有时候还会觉得灰心。面对着汹涌而来的现实觉得自己渺小无力。但这，也是生命的一部分。做好现在你能做的，然后，一切都会好的。我们都将孤独地长大，不要害怕。

个人资料
• 访问：398308次
• 积分：6904
• 等级：
• 排名：第4015名
• 原创：307篇
• 转载：8篇
• 译文：2篇
• 评论：26条
个人介绍
博客专栏
 opencv之tutorial_code学习 文章：11篇 阅读：15647
 汇编语言学习 文章：15篇 阅读：41274
 python小demo分享 文章：20篇 阅读：30116
 windows多线程编程 文章：8篇 阅读：8072
 剑指offer笔记 文章：33篇 阅读：12451
 SQL之简明教程 文章：30篇 阅读：15304
 matlab之数字图像处理 文章：6篇 阅读：9531
阅读排行
最新评论