sorting algorithm (c + + implementation)

Source: Internet
Author: User

1#include <vector>2 usingstd::vector;3 4Template<typename t>5 voidSwap (vector<t> &a,intIintj)6 {7T tmp =A[i];8A[i] =A[j];9A[J] =tmp;Ten } One  A //Simple Insert Sort -Template<typename t> - voidInsertionsort (Vector<t> &a) the { -      for(intp=1; P!=a.size (); ++p) -     { -         intTMP =A[p], J; +          for(j = p; j>0&&a[j-1]>tmp; --j) -A[J] = a[j-1]; +A[J] =tmp; A     } at } -  - //Heap Sort - //The elements that will be sorted into the heap, followed by a pop-up, get the ordered sequence of elements.  - //C + + implementations of the heap:http://www.cnblogs.com/pczhou/p/4648146.html -  in //Merge Sort -Template<typename t> to voidMergeSort (Vector<t> &a) + { -Vector<t>Tmpa (A.size ()); theMergeSort (A, Tmpa,0, A.size ()-1); * } $Template<typename t>Panax Notoginseng voidMergeSort (vector<t> &a, vector<t> &tmpa,intLeftintRight ) - { the     if(left<Right ) +     { A         intMid = (left+right)/2; the MergeSort (A, Tmpa, left, mid); +MergeSort (A, Tmpa, mid+1, right); -Merge (A, Tmpa, left, mid+1, right); $     } $ } -Template<typename t> - voidMerge (Vector<t> &a, vector<t> &tmpa,intLeftPos,intRightpos,intrightend) the { -     intLeftend = rightpos-1, Tmppos =LeftPos;Wuyi     intNumelements = rightend-leftpos+1; the  -      while(leftpos<=leftend&&rightpos<=rightend) Wu     { -         if(a[leftpos]<A[rightpos]) Abouttmpa[tmppos++] = a[leftpos++]; $         Else -tmpa[tmppos++] = a[rightpos++]; -     } -      while(leftpos<=leftend) Atmpa[tmppos++] = a[leftpos++]; +      while(rightpos<=rightend) thetmpa[tmppos++] = a[rightpos++]; -      for(; numelements;--numelements,--rightend) $A[rightend] =Tmpa[rightend]; the } the  the //Quick Sort the //(using three-digit median segmentation method to determine the pivot element) -Template<typename t> in voidQuickSort (Vector<t> &a) the { theQuickSort (A,0, A.size ()-1); About } theTemplate<typename t> the Constt& median3 (vector<t> &a,intLeftintRight ) the { +     intMid = (left+right)/2; -     if(a[left]<a[mid]&&a[mid]<a[right]| | a[right]<a[mid]&&a[mid]<A[left]) the swap (A, Mid, right);Bayi     Else if(a[mid]<a[left]&&a[left]<a[right]| | a[right]<a[left]&&a[left]<A[mid]) the swap (A, left, right); the     returnA[right]; - } -Template<typename t> the voidQuickSort (vector<t> &a,intLeftintRight ) the { the     if(left<Right ) the     { -T pivot =Median3 (A, left, right); the         inti=left-1, j=Left ; the          for(; j<right; + +j) the         {94             if(a[j]<pivot) the             { the++i; the swap (A, I, j);98             } About         } -Swap (A, i+1, right);101 102 QuickSort (A, left, I);103QuickSort (A, i+2, right);104     } the }106 107 //Bucket Sort108 //assumption: All input data is non-negative and no more than M109 Const intM = -; the voidBucketsort (vector<int> &a)111 { thevector<int> Count (m+1);113      for(intI=0; I!=count.size (); ++i) Count[i] =0; the      for(intI=0; I!=a.size (); ++i) + +Count[a[i]]; the     intK =0; the      for(intI=0; I!=count.size (); ++i)117          for(intj=0; j<count[i]; ++J) a[k++] =i;118}

Test code:

1vector<int>A1, A2, A3, A4;2      for(intI=0; i<Ten; ++i)3     {4         inttmp;5cout<<"Please input:";6Cin>>tmp;7 A1.push_back (TMP);8 A2.push_back (TMP);9 A3.push_back (TMP);Ten A4.push_back (TMP); One     } A Insertionsort (A1); - MergeSort (A2); - QuickSort (A3); the Bucketsort (A4); -cout<<"Insertion Sort:"<<Endl; -      for(intI=0; I!=a1.size (); ++i) -cout<<" "<<A1[i]; +cout<<Endl; -cout<<"Merge Sort:"<<Endl; +      for(intI=0; I!=a2.size (); ++i) Acout<<" "<<A2[i]; atcout<<Endl; -cout<<"Quick Sort:"<<Endl; -      for(intI=0; I!=a3.size (); ++i) -cout<<" "<<A3[i]; -cout<<Endl; -cout<<"Bucket sort:"<<Endl; in      for(intI=0; I!=a4.size (); ++i) -cout<<" "<<A4[i]; tocout<<endl;

Test results:

sorting algorithm (c + + implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.