排序算法复习

原创 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);
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Java基础复习笔记11基本排序算法

  • 2011年05月08日 22:00
  • 234KB
  • 下载

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

======来自N多资料,主要参考http://blog.csdn.net/hguisu/article/details/7776068 以及 http://blog.csdn.net/onedrea...

【复习笔记】手写三种O(n^2)的基础排序算法

1.冒泡排序,稳定排序。 public static void bubbleSort(int [] array){ int len = array.length; for(int...

课本上经典的排序算法复习

冒泡法:   这是最原始,也是众所周知的最慢的算法了。他的名字的由来因为它的工作看来象是冒泡:  复杂度为O(n*n)。当数据为正序,将不会有交换。复杂度为O(0)。  直接插入排序:O(n*n) ...

排序算法复习(Java实现)

为了便于管理,先引入个基础类: package algorithms; public abstract class Sorter> { public abstract void sor...

(2)Java复习冒泡,选择,插入排序算法

import java.io.*; public class MonkeySort { public static void main(String[] args) { // TODO Aut...

排序算法复习笔记

package com.Test.Sort; /* * author:Tammy Pi * function:排序算法总结 */ public class SortTest { ...

复习----常见的排序算法

到目前为止,学习过的排序算法已经有很多了。它们因为时间复杂度以及空间复杂度的差异适用于不同的场合,所以现在来整理一下。 (注:此处的swap()是一个交换函数,只给出具体的实现函数) 待排序数组如下图...

几种常用排序算法的复习

《数据结构》中介绍了好几种排序算法,有时候觉得晕晕的,所以就再拿出来复习下。 插入类排序 插入类排序是将一个记录插入到一个已经排好序的有序表中,使得新表仍然有序,经常接触到的插入类排序的有直接插入排序...

基本排序算法复习

排序算法: 1、冒泡排序 双层循环,内层循环一圈得出一个最小(最大)值。循环和交换复杂度均为n*n。 for(int i=0;i {     for(int j=i+1;j ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:排序算法复习
举报原因:
原因补充:

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