#include <iostream>using namespacestd;voidPrint (intArr[],intlength) { for(inti =0; i < length; i++) {cout<< Arr[i] <<" "; } cout<<Endl;}intPartitionintA[],intLowintheight) {cout<<"Low :"<< Low <<"Height:"<< Height <<Endl; //when finished, the left side of the A[low] element is all bigger than it, and the right side is smaller than it. while(low<height) { //from the very beginning of the element, looking from behind, finding the first one smaller than it, swapping while(a[low]>a[height]) height--; Swap (A[low], a[height]); //look back from the beginning, find the first one bigger than it, swap while(A[height] <A[low]) Low++; Swap (A[low], a[height]); } Print (A,7); cout<<"Privotloc:"<< low<<Endl; //returns the position of the currently lowest element after sorting returnLow ;}voidQuick (intA[],intLowintheight) { if(Low >=height)return; intPrivotloc =partition (A, low, height); Quick (A, low, Privotloc-1); Quick (A, Privotloc+1, height);}voidBubble (intA[],intlength) { for(inti =0; i < length; i++) { for(intj = i; J < length; J + +) { if(A[i] >A[j]) { intt =A[j]; A[J]=A[i]; A[i]=T; } }} cout<<"Bubbling:"; Print (A,7); cout<<Endl;}//merge Arrays A[i,m-1] and a[m,n] into B[i,n]voidMerge (intA[],intB[],intIintMintN) { intT = i, k = m;//counters for first and second groups intj = i;//counter for array b//traverse the second set of data, if there is less than the current value in the first group, put the first set of numbers in array B, otherwise, put yourself in array b while(k<n+1) { while(A[t] <a[k]&&t<N) b[j+ +] = a[t++]; B[j+ +] = a[k++]; } //traverse the first set of remaining data and put it in array b while(t<m) b[j+ +] = a[t++]; for(; I <= N; i++) {cout<< B[i] <<" "; } cout<<Endl;}voidMergeSort (intA[],intB[],intlength) { intLen =1; while(len<length) { //divide the array into lengths of Len and so on to merge inti =0; while(i +2* len<length) {Merge (A, B, I, I+ Len, i +2* Len-1); I+=2*Len; } //merging groups with unequal endings if(i + Len <=length) {Merge (A, B, I, I+ len, Length-1); } //swap A and B to make sure the next time you merge from A to BSwap (A, b); //Multiply The length of a single groupLen *=2; }}voidSelection (intA[],intlength) { for(inti =0; i < length; i++) { intMax=a[i], key=i; for(intj = i; J < length; J + +) { if(A[j] >max) {Max=A[j]; Key=J; }} swap (A[i], A[key]); } cout<<endl<<"Select:"; Print (A, length);}//I is the child node added to the endvoidMinheapfixup (intA[],inti) { //build binary heap, parent node is always smaller than child node for(intj = (I-1) /2; J >=0&& A[j] < a[i]; i = j, j = (J-1) /2swap (A[i], a[j]);}voidMinheapfixdown (intA[],intlength) { intTemp,i, J; Temp= a[0]; I=0; J=2* i +1; while(j<length) { //1. All the children are looking for, find two children of the big one if(j +1< Length&&a[j +1] >A[j]) J++; if(A[j] <A[i]) Break; A[i]=A[j]; I=J; J=2* i +1; } A[i]=temp;}//The heap can only delete the top node, and it will be repaired after deletionvoidMinheapdelete (intA[],intlength) {Swap (a[0], A[length-1]); Minheapfixdown (A, length-1);}voidHeapsort (intA[],intlength) {cout<<"Build a heap"<<Endl; for(inti =0; i < length; i++) {Minheapfixup (A, I); Print (A, length); } cout<<Endl; cout<<"Sort"<<Endl; for(inti =0; i < length; i++) {Minheapdelete (A, length-i); Print (A, length); } cout<<Endl; Print (A, length);}voidMain () {intarr[7] = {1,5,6, -, the,122, the }; intb[7]; Print (arr,7); ////write a bubble first //Bubble (arr, 7); ////Block Row //Quick (arr, 0,6); //cout << Endl << Endl; ////Merge //MergeSort (arr, B, 7); ////Select //Selection (arr, 7); //before the heap is sorted, first recognize what is the heap and then build the heap,//After the heap is completed, the heap can be sortedHeapsort (arr,7); Print (arr,7); intA =sizeof(arr); CIN>>A;}
Reference:
http://blog.csdn.net/hguisu/article/details/7776068
http://blog.csdn.net/morewindows/article/details/6709644
Http://www.blogjava.net/todayx-org/archive/2012/01/08/368091.html
Cond
[Xiao Ming Learning Algorithm] 5. Common sorting algorithms