vpoet

行到水穷处,坐看云起时。

排序算法复习

排序算法复习

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


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013018721/article/details/46786819
文章标签: 排序
个人分类: 算法|ACM
上一篇自己写的一点福利代码(四)
下一篇单链表复习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭