排序算法复习

原创 2015年07月07日 11:14:12
排序算法复习

作者: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.  冒泡排序,冒泡排序属于简单排序的一种,也是大多数人学会的第一个排序算法,其基本思想是,从左到右扫描每个数据,跟它相邻的比较,如果大小顺序不对,就交换两个数据的位置,这种算法的最坏时间复杂度为n...

网上找到的,常用的排序算法总结,可以复习

冒泡排序   冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序...

基础排序算法复习

1.计数排序 计数排序的四个步骤: a.初始化c[0…k]为0; b.对于每个元素a[i], c[ a[i] ]++,c记录a中各个元素出现的次数,比如{1, 1, 3}中,c[1] = 2, ...

各种排序算法的复习

内容不深,全是数据结构书上的,需要更深入了解的请参阅算法导论。         希望今天能把这个知识点复习完。Doing! 排序算法总结 一、内部排序算法       ...
  • dscyw
  • dscyw
  • 2013-04-06 12:26
  • 1058

这两天没什么事,复习一下排序算法

说实话,本人半路出家从事web开发,计算机方面的许多知识都算是小白,算法这种东东,以前是完全没有什么概念。 后来学习JS的时候总感觉编写程序很不靠谱,所以有段时间就猛补了一下数据结构啊算法啊,设计模式...

《数据结构》复习之排序算法

排序算法 1直接插入排序 2希尔排序 3冒泡排序 4快速排序 5简单选择排序 6堆排序 7二路归并排序 8基数排序 排序算法总结 1排序算法的稳定性 2复杂度总结 3稳定性总结 4其他1.排序算法1....

[数据结构复习]八大内排序算法总结

======来自N多资料,主要参考http://blog.csdn.net/hguisu/article/details/7776068 以及 http://blog.csdn.net/onedrea...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)