Summary of C + + sorting algorithms

Source: Internet
Author: User

Recently came to idle, sorting out some of the more common sorting algorithms, are written in C + +, including: Direct insert sort, binary insert sort, bubble sort, select sort, quick sort, heap sort, merge sort, Hill sort, cardinal sort, count sort and bucket sort, total 11 kinds of algorithms, The time complexity is O (n^2) is the first 4 kinds, the time complexity in the Middle 4 is O (NLGN), the last 3 kinds of time complexity is O (n). Below we are divided into 3 columns to introduce: n^2 sorting, nlgn sorting and linear sorting.

Note: A is a global variable and is a one-dimensional int array

n^2 Sort Direct Insert Sort

Similar to the principle of the sort of poker, when the landlord, every time you touch a card inserted into the corresponding position, the direct insertion sort is the principle. From the first number of arrays, compare the size of the number and the preceding number, and if the previous number is smaller than the number, swap the position with the previous digit, and then continue the comparison until a number is smaller than the number.

The core code is as follows:

1 voidInsertsort ()2 {3     inti,j;4      for(i =1; I <= n;i++)5     {6j =i;7          while(J >0&& A[j] < a[j-1])8         {9Swap (a[j],a[j-1]);TenJ--; One         } A     }     -}
View CodeBinary Insert Sort

In fact, this algorithm is an optimization of direct insertion sorting, when a number is compared with the number in front of it, the number of the first number in the array and the number of the previous number is the median of the interval, similar to the dichotomy.

The core code is as follows:

1 voidHalfsort ()2 {3     intI, J, high, Low, mid;4      for(i =2; I <= N; i++)5     {6a[0] =A[i];7Low =1;8High = i-1;9          while(Low <=High )Ten         { OneMid = (low + high)/2; A             if(a[0] <A[mid]) -High = mid-1; -             Else theLow = mid +1; -         } -          for(j = i-1; J >= High +1; j--) -A[j +1] =A[j]; +A[high +1] = a[0]; -     } +}
View CodeBubble sort

This sort algorithm is my earliest contact algorithm, the principle is that each time within the cycle can be the smallest number of rows to the front, so that the outer loop after the row.

The core algorithms are as follows:

1 voidBubblesort ()2 {3      for(inti =1; I <= N-1; i++)4     {5          for(intj = N;j >= i+1; j--)6         {7             if(A[j] < a[j-1])8Swap (a[j],a[j-1]);9         }Ten     } One}
View CodeSelect sort

Similar to bubble sort, except to find the lowest number of subscript, and then replace to the corresponding position.

The core code is as follows:

1 voidSelectsort ()2 {3     intI,j,lowkey,lowindex;4      for(i =1; I <= N-1; i++)5     {6Lowindex =i;7Lowkey =A[i];8          for(j = i +1; J <= n;j++)9         {Ten             if(a[j]<Lowkey) One             { ALowkey =A[j]; -Lowindex =J; -             } the         } - swap (A[i],a[lowindex]); -     } -}
View CodeNLGN Sort Quick Sort

This sort algorithm is a more common algorithm in NLGN sorting algorithm, which is fast and can be optimized in different degree.

The principle of the algorithm is summed up in 2 parts:

• Decomposition: Array a[i. J] is divided into two (possibly empty) sub-arrays of a[i. Q-1] and A[Q+1..J], making a[i. No element in Q-1] is less than or equal to A[q], and A[q] is less than or equal to each element in A[Q+1..J]. Among them, the calculation of subscript Q is also part of the partitioning process;

• Workaround: Fast Sort by recursive call, A[i to sub-array. Q-1] and A[Q+1..J] are sorted;

Returns the appropriate main-element subscript, here is the main element with an array of two different numbers from left to right:

1 intFindpivot (intIintj)2 {3     intFirstkey =A[i];4     intK;5      for(k = i +1; K <= J; k++)6     {7         if(A[k] >Firstkey)8             returnK;9         Else if(A[k] <Firstkey)Ten             returni; One     } A     return 0; -}
View Code

Dividing part of the code:

1 voidPatition (intIintJintq)2 {3     intPivot =A[q];4     intL =i;5     intR =J;6      Do7     {8 swap (a[l],a[r]);9      while(A[l] < pivot) l++;Ten      while(A[r] >= pivot) r--;  One} while(L <=R); A}
View Code

Sort code:

1 voidQuickSort (intIintj)2 {3     intQ,l,r,pivot;4Q =Findpivot (i,j);5     if(Q! =0)6     {7 patition (i,j,q);8QuickSort (i,l-1);9 QuickSort (l,j);Ten     } One}
View Code

Summary of C + + sorting algorithms

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.