排序算法(源代码)

#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <conio.h>
#include <iomanip>
using namespace std;

void GetData(vector<int> &vec);
void Sort(vector<int> &vec);

int main()
{
    srand( (int)time(NULL) );

	vector<int> vec;
	GetData(vec);

	Sort(vec);
	
	return 0;
}

//生成随机函数
double random(double beg, double end)
{
	return beg + (end-beg)*rand()/(RAND_MAX+1.0);
}

//交换
void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

//打印所有数据
void print(const vector<int> &vec)
{
	for(int i=0; i<vec.size(); ++i)
	{
		cout<<setw(2)<<left<<vec[i]<<" ";
	//	if( (i+1)%10 == 0 )
	//		cout<<endl;
	}
	cout<<endl;
}

//导入数据
void GetData(vector<int> &vec)
{
	for(int i=0; i<20; ++i)
	{
		vec.push_back((int)random(0,100));
	}
}

//功能列表
void FirstPage()
{
	cout<<"★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl
		<<"※                                                            ※" <<endl
		<<"※                          功能列表                          ※" <<endl
		<<"※                                                            ※" <<endl
	    <<"※       1---初始序列     2---冒泡排序     3---选择排序       ※" <<endl
		<<"※                                                            ※" <<endl
		<<"※       4---插入排序     5---快速排序     6---归并排序       ※" <<endl
		<<"※                                                            ※" <<endl
		<<"※       7---堆排序       8---希尔排序     9---退出系统       ※" <<endl
		<<"※                                                            ※" <<endl
		<<"★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl<<endl;
}

//冒泡排序
void BubbleSort(vector<int> &vec)
{
	for(int i=0; i<vec.size(); i++)
	{
		for(int j=vec.size()-1; j>i; j--)
		{
			if(vec[j-1] > vec[j])
			{
				swap(vec[j-1], vec[j]);
			}
		}
	}
}

//插入排序
void InsertSort(vector<int> &vec)
{
	for(int i=1; i<vec.size(); ++i)
	{
		int key = vec[i];
		for(int j=i-1; j>=0;j--)
		{
			if(vec[j]>key)
				vec[j+1] = vec[j];
			else
				break;
		}
		vec[j+1] = key;
	}
}

//选择排序
void SelectSort(vector<int> &vec)
{
	for(int i=0; i<vec.size(); ++i)
	{
		int min = vec[i];
		for(int j=i+1; j<vec.size(); ++j)
		{
			if(vec[j] < min)
				min = vec[j];
		}
		swap(vec[i], min);
	}
}


//归并排序
void Merge(vector<int> &vec, int p, int q, int r)
{
	int i=0;
	int j=0;
	vector<int> left( vec.begin()+p, vec.begin()+q+1 );
	vector<int> right( vec.begin()+q+1, vec.begin()+r+1 );
	while(i<left.size() && j<right.size())
	{
		if(left[i] < right[j])
			vec[p++] = left[i++];
		else
			vec[p++] = right[j++];
	}
	while(i<left.size())
		vec[p++] = left[i++];
	while(i<right.size())
		vec[p++] = right[j++];
	left.clear();
	right.clear();
}
void MergeSort(vector<int> &vec, int p, int r)
{
	if(p<r)
	{
		int q = (p+r)/2;
		MergeSort(vec, p, q);
		MergeSort(vec, q+1, r);
		Merge(vec, p, q, r);
	}
}

//快速排序
int Partition(vector<int> &vec, int low, int high)
{
	int pivot = vec[low];
	while(low<high)
	{
		while(low<high && pivot <= vec[high])
			--high;
		vec[low] = vec[high];
		while(low<high && pivot >= vec[low])
			++low;
		vec[high] = vec[low];
	}
	vec[low] = pivot;
	return low;
}

//随机选取pivote
int Rondom_Partition(vector<int> &vec, int low, int high)
{
	int i=random(low,high);
	swap(vec[i], vec[high]);
	return Partition(vec, low, high);
}

void Quick(vector<int> &vec, int low, int high)
{
	int p;
	if(low<high)
	{
		p = Rondom_Partition(vec, low, high);
		Quick(vec, low, p-1);
		Quick(vec, p+1, high);
	}
}

void QuickSort(vector<int> &vec)
{
	Quick(vec, 0, vec.size()-1);
}

//堆排序
void Max_heap(vector<int> &vec, int i, int heap_size)
{
	int l = 2*i ;
	int r = 2*i + 1 ;
	int largest = i ;
	if(l <= heap_size && vec[l-1] > vec[largest-1])
		largest = l ;
	if(r <= heap_size && vec[r-1] > vec[largest-1])
		largest = r ;
	if(largest != i)
	{
		swap(vec[largest-1], vec[i-1]);
		Max_heap(vec, largest, heap_size);
	}
}

void HeapSort(vector<int> &vec)
{
	int heap_size = vec.size();
	for(int i=vec.size()/2; i>=1; i--)
		Max_heap( vec, i, vec.size() );
	for(i=heap_size; i>0; i--)
	{
		swap(vec[0], vec[i-1]);
		heap_size--;
		Max_heap(vec, 1, heap_size);
	}
}

//shell排序
void ShellSort(vector<int> &vec)
{
	int i,j;
	int increment = vec.size()/2;
	while(increment > 1)
	{		
		for(i=increment+1; increment<=vec.size(); increment++)
		{
			int key = vec[i];
			for(j=i-increment; j>0 && key<vec[j]; j-=increment)
			{
				vec[j+increment] = vec[j];
			}
			vec[j+increment] = key;
		}
		increment /= 2;
	}

}

void Sort(vector<int> &vec)
{
	clock_t begin, end;

	vector<int> bubble(vec);
	vector<int> insert(vec);
	vector<int> select(vec);
	vector<int> quick(vec);
	vector<int> merge(vec);
	vector<int> heap(vec);
	vector<int> shell(vec);

	while(1)
	{
		system("cls");
		FirstPage();
		cout<<"选择种类(1-9)"<<endl;

		char ch = getch();	
	    switch(ch)
		{
		    case '1':
				cout<<"初始序列数目 : "<<vec.size()<<endl;
				print(vec);
				system("pause");
				break;
			case '2':
				begin = clock();
				
				cout<<"初始序列 : "<<endl;
				print(vec);
	            BubbleSort(bubble);
				cout<<"冒泡排序后:"<<endl;
			    print(bubble);
				end = clock();
				cout<<"运行时间:"<<endl;
				cout<<(double)(end - begin)<<endl;
	
				system("pause");
				break;
			case '3':
				begin = clock();
				
				cout<<"初始序列 : "<<endl;
				print(vec);
			    SelectSort(select);
			    cout<<"选择排序后:"<<endl;
			    print(select);

				end = clock();
				cout<<"运行时间:"<<endl;
				cout<<(double)(end - begin)<<endl;
				system("pause");
				break;
			case '4':
				begin = clock();
				
				cout<<"初始序列 : "<<endl;
				print(vec);
			    InsertSort(insert);
			    cout<<"插入排序后:"<<endl;
			    print(insert);
				
				end = clock();
				cout<<"运行时间:"<<endl;
				cout<<(double)(end - begin)<<endl;
				system("pause");
				break;
			case '5':
				begin = clock();
				
				cout<<"初始序列 : "<<endl;
				print(vec);
			    QuickSort(quick);
			    cout<<"快速排序后:"<<endl;
			    print(quick);
				
				end = clock();
				cout<<"运行时间:"<<endl;
				cout<<(double)(end - begin)<<endl;
				
				system("pause");
				break;
			case '6':
				begin = clock();
				
				cout<<"初始序列 : "<<endl;
				print(vec);
			    MergeSort(merge, 0, merge.size()-1);
			    cout<<"归并排序后:"<<endl;
			    print(merge);
				
				end = clock();
				cout<<"运行时间:"<<endl;
				cout<<(double)(end - begin)<<endl;
				
				system("pause");
				break;
			case '7':
				begin = clock();
				
				cout<<"初始序列 : "<<endl;
				print(vec);
			    HeapSort(heap);
			    cout<<"堆排序后:"<<endl;
			    print(heap);
				
				end = clock();
				cout<<"运行时间:"<<endl;
				cout<<(double)(end - begin)<<endl;
				
				system("pause");
				break;
			case '8':
				begin = clock();
				
				cout<<"初始序列 : "<<endl;
				print(vec);
			    HeapSort(shell);
			    cout<<"堆排序后:"<<endl;
			    print(shell);
				
				end = clock();
				cout<<"运行时间:"<<endl;
				cout<<(double)(end - begin)<<endl;
				
				system("pause");
				break;
			case '9':
				cout<<endl<<"谢谢使用,再见!"<<endl<<endl;
				return;
			default:
				cout<<"输入错误,请重新输入!"<<endl<<endl;
				system("pause");
		}
	}
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值