排序算法C++实现

来源:互联网 发布:回龙观矩阵托管 编辑:程序博客网 时间:2024/06/05 18:36

实现了5个排序算法:插入,选择,快速排序,归并排序和堆排序:


1)插入排序: 对于当前元素,将其插入前面已经排好序的数组中:

2)选择排序:选从当前元素之后中最小的元素,放入数组中。

3)快速排序:选定一个pivot,将数组分为左数组:都小于pivot,右数组:都大于pivot, 然后对于左右数组,继续调用快速排序,最终得到排序结果。

(在分割数组的时候,要计算分割数组的位置,对于该位置的计算,采用2个下标:small和large:

small 表示该元素小于pivot, large表示该元素大于pivot。

当找到第一个small表示的元素大于pivot和第一个large小于pivot的时候,交换两元素,直至两元素下标相互交叉。

最后得到的large元素下标就是pivot的下标。,最后,交换pivot和large元素即可。

4)归并排序:将一个数组均分为左右两个数组,分别对左右两数组调用归并排序,然后对排好序的左右两数组进行合并。

5)堆排序:首先对数组建立一个最小堆,然后每次取出堆顶元素,调整堆,直至堆为空。我在实现时,利用了C++中STL库中的优先队列结构实现。

6) 堆排序one:该算法用了数组实现的,与优先队列类似,步骤:建堆,取头元素,调整堆。(编程时有一个细节:在调整堆的时候,要和左右两个孩子中较大的交换)


将上述五种算法实现在了一个cpp中,其中,main函数为测试函数,这里指写了一个对于整数的排序算法,如果需要更大的兼容性,可以写一个template的排序算法。


<pre name="code" class="cpp">// OOD.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream> // this is system header file, so use <>#include<vector>#include<queue>using namespace std;/*this is the sort program*/void print(vector<int>&v){for(int i = 0; i < v.size(); i++)cout << v[i] << " ";cout<<endl;}void insertsort(vector<int> v){/*insert sort: for each element, find position where it can be insert*/vector<int> result;int i,j;for(i = 1; i < v.size(); i++){int cur = v[i];j = i-1;while(j >= 0 && v[j] > cur){v[j+1] = v[j];j--;}v[j+1] = cur;}cout<<"the result for insert sort is: "<<endl;print(v);}void selectsort(vector<int> v){/*select then min number in the remaining array and insert it*/for(int i=0; i < v.size(); i++){int curmin = v[i];int minindex =i;// select min numberfor(int j = i+1; j < v.size(); j++){if(v[j] < curmin){curmin = v[j];minindex = j;}}swap(v[i], v[minindex]);}cout<<"the result for selection sorting is: "<<endl;print(v);}//quick sort beginint firstsort(vector<int> &v, int startindex, int endindex){/*idea:small indicate the first number small than pivot   large indicate the first number large than pivot      then if we find such index, swap them and continue until small > large*/    int pivot = v[startindex];int small = startindex+1;int large = endindex;while(small <= large){while(small <= endindex && v[small] <= pivot)small++;while(large > startindex && v[large] >= pivot)large--;if(small < large)swap(v[small],v[large]);}swap(v[large],v[startindex]);return large;}void quicksort(vector<int>&v, int startindex, int endindex){if(endindex <= startindex)return;int cutindex = firstsort(v,startindex,endindex);quicksort(v,startindex,cutindex-1);quicksort(v,cutindex+1,endindex);}void quicksortalgorithm(vector<int> v){    quicksort(v, 0, v.size()-1);cout<<"the result for quick sort is: "<<endl;print(v);    }//quick sort end// merge sort beginvoid merge(vector<int>& v, int startindex, int midindex, int endindex){if(endindex <= startindex)return;vector<int> result;int i = startindex;int j = midindex+1;while(i <= midindex && j<= endindex){if(v[i] < v[j]){result.push_back(v[i++]);}else{result.push_back(v[j++]);}}while(i <= midindex)result.push_back(v[i++]);while(j <= endindex)result.push_back(v[j++]);for(i = startindex; i <= endindex; i++)v[i] = result[i-startindex];}void mergesort(vector<int>& v, int startindex, int endindex){if(endindex <= startindex) // for empty array and one element array, just returnreturn; int divideindex = startindex + (endindex - startindex)/2;mergesort(v,startindex, divideindex);mergesort(v,divideindex+1, endindex);merge(v,startindex,divideindex,endindex);}void mergesortalgorithm(vector<int> v){mergesort(v,0,v.size()-1);cout<<"the result for mergesort is: "<<endl;print(v);}//merge sort end//heap sort beginvoid heapsort(vector<int> v){// using a priority queue to implenment such algorithmpriority_queue<int,vector<int>,greater<int>> pq;// build a heap;for(int i= 0; i < v.size(); i++)pq.push(v[i]);for(int i=0; i < v.size(); i++){v[i] = pq.top();pq.pop();}cout<<"the result for heap sort is: " <<endl;print(v);}//heap sort end//another heap sort beginint parent(int childindex){// given the current index, return the parent indexreturn (childindex-1)/2;}void heapinsert(vector<int>& v, int value){// first insert at the end of v, then reshape vv.push_back(value);int curindex = v.size()-1;int parentindex = parent(curindex);while(parentindex >= 0 && v[parentindex] < value){swap(v[parentindex], v[curindex]);curindex = parentindex;parentindex = parent(curindex);}}int heaptop(vector<int>&V,int startindex, int endindex){int value = V[startindex];swap(V[startindex],V[endindex]);int curindex = startindex;int leftchildren = 2*curindex+1;int rightchildren = 2*curindex+2;while(curindex < endindex &&leftchildren < endindex && rightchildren < endindex){leftchildren = 2*curindex+1;rightchildren = 2*curindex+2;if(leftchildren < endindex && rightchildren < endindex){if(V[leftchildren] > V[curindex]){if(V[rightchildren] > V[leftchildren]){swap(V[rightchildren],V[curindex]);curindex = rightchildren;}else{swap(V[leftchildren], V[curindex]);curindex = leftchildren;}}else{if(V[rightchildren] > V[curindex]){swap(V[rightchildren],V[curindex]);curindex = rightchildren;}elsereturn value; // this means root > leftchildren root > rightchildren}}}return value;}void heapsort_one(vector<int> v){vector<int> B;for(int i = 0; i < v.size(); i++)heapinsert(B,v[i]);for(int i = 0; i < B.size(); i++){heaptop(B,0,B.size()-i-1);}cout<<"another heap sort: "<<endl;print(B);}//another heap sort endint main(){/*int A[] = {6,5,4,3,2,1};vector<int> V;for(int i = 0; i < sizeof(A)/sizeof(int); i++)V.push_back(A[i]);*/vector<int> V;for(int i = 0; i < 10; i++)V.push_back(rand()%100);cout<<"original input array is:  ";print(V);// insert sortinsertsort(V);    //select sortselectsort(V);//quick sortquicksortalgorithm(V);    //merge sort    mergesortalgorithm(V);    //heap sortheapsort(V);heapsort_one(V);system("pause");return 0;}


                                             
0 0