关闭

排序算法复习

标签: 排序
337人阅读 评论(0) 收藏 举报
分类:
排序算法复习

作者:vpoet
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);
	}
}


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    《我们终将独自长大》

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

    个人资料
    • 访问:306338次
    • 积分:6157
    • 等级:
    • 排名:第4025名
    • 原创:307篇
    • 转载:8篇
    • 译文:2篇
    • 评论:20条
    个人介绍
    博客专栏
    最新评论