Algorithm summary-three big sort (fast, counting, merging)

Source: Internet
Author: User

Quick line:

Applicable conditions: Convenient ... As long as the numbers are not many

Complexity: O (NLOGN) each layer n complexity, LOGN layer

Principle: Use a random number and the last face of a number exchange, then this random number to the last one, and then loop, if the previous number is greater than the last number, then put the number ahead, after a sort, the previous number is greater than the last, and then the 1 to K and k+1 to N to sort, Go down one layer at a to

Template:

#include <cstdio>#include<algorithm>#include<time.h>using namespacestd;voidSort (int*a,intLintR) {intK =0;intLen = L-R;if(Len <=1)return; Srand (Time (NULL));intpos = rand ()%Len;swap (A[r-1],a[pos+l]); for(inti = l; I < R; i++){      if(A[i] >a[r-1]) {Swap (a[i],a[l+K]); K++; }}sort (a,l,l+k-1); Sort (a,l+k,r);}

Count sort

Complexity: O (N+K)

Applicable conditions: Positive

Principle: The basic idea of counting sorting is to determine the number of elements in the sequence that have a value less than x for each element x in the given input sequence. Once you have this information, you can store the x directly in the correct position of the final output sequence. For example, if the value of only 17 elements in the input sequence is less than the value of x, then x can be stored directly in the 18th position of the output sequence. Of course, if there are more than one element with the same value, we cannot put these elements in the same position of the output sequence, so the above scheme should also be modified (from Baidu Encyclopedia)

In layman's terms, a is used to record all the values to be sorted, W to record the number of values below that value, and by counting, W becomes the number of subscripts less than this number, w[a[i], which is less than the current a[i] number, that is, to determine the position, Use an array p to record this value under this position, and then the number of other numbers less than this count is one less, then the position is all determined the direct output of the line

One of the characteristics of counting sorting is to open the same number of nodes, such as 7000, that will open 7,000, obviously if the number of small and large words is not advantageous, but more words count sort than fast six times times faster.

Template:

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =222222;intP[MAXN],W[MAXN],A[MAXN];voidSortint*a,intNintMX) {     for(inti =1; I <= MX; i++) W[i]=0;  for(inti =1; I <= N; i++) W[a[i]]++;  for(inti =1; I <= MX; i++) W[i]+ = w[i-1];  for(inti = n; I >=1; i--) {P[w[a[i] ]=A[i]; W[a[i]]--; }}intMain () {intN,MX =0;  while(~SCANF ("%d",&N)) {         for(inti =1; I <= N; i++) {scanf ("%d",&A[i]); MX=Max (mx,a[i]);        } sort (A,N,MX);  for(inti =1; I < n; i + +) printf ("%d", P[i]); printf ("%d\n", P[n]); }    return 0;}

Ps:orz, originally called Count sort, not called barrel Platoon, drunk ~~~~~~

Merge sort (Merge_sort):

Scope of application: can be used to find the number of reverse order, in the second part of the comparison on the sort of function, here records on the line, the details of the code comment, if the number of words to use, after all, stable, but the memory is also more

Complexity: O (nlogn) merges O (n) into each small block O (logn)

Principle: With the idea of dividing, to make all the strings sorted well, then the whole block is divided into small pieces, until the last few numbers, and then from the bottom up for the whole sequence to compare the sorting, with recursion (divided: the big problem into every small problem to solve), that the algorithm is divided into two parts: 1. Block the entire string 2. Merge the sorting of good blocks by comparison

Template:

#include <cstdio>#include<cstring>using namespacestd;Const intMAX =222222;intA[max],temp[max];intAns =0;voidMergearry (int*a,intFirstintMidintLastint*temp) {    inti = First, J = mid +1; intK =First ;  while(I <= mid && J <=Last ) {        if(A[i] <=A[j]) temp[k+ +] = a[i++]; Else {       //ans + = j-k; the inverse number of the method, because a are all from small to large rows, if A[I]>A[J] explain a[i] After the number is greater than a[j] in front of the number, then the number of reverse order will increase j-ktemp[k++] = a[j++]; }    }     while(I <=mid) Temp[k+ +] = a[i++];  while(J <=Last ) Temp[k+ +] = a[j++];  for(inti = First; I <= last;i++) A[i]=temp[i];}voidMergeSort (int*a,intFirstintLastint*temp) {    if(First <Last ) {            intMid = (first + last) >>1;            MergeSort (a,first,mid,temp); MergeSort (A,mid+1, last,temp);    Mergearry (a,first,mid,last,temp); }}intMain () {intN; scanf ("%d",&N);  for(inti =1; I <= N; i++) scanf ("%d",&A[i]); MergeSort (A,1, n,temp);  for(inti =1; I < n; i++) printf ("%d", A[i]); printf ("%d\n", A[n]); printf ("%d\n", ans); return 0;}

Summary: The top three sorting with the most or fast row and merge, after all, the merger can be used to find the number of reverse order, easy to arrange, counting generally do not use it ~ ~ ~

Algorithm summary-three big sort (fast, counting, merging)

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.