Summary of the sorting algorithm for "C/C + +"

Source: Internet
Author: User

1. Sort by count

If the upper and lower bounds are given, and the interval is small, it is most applicable.

For example, the English alphabet array to sort.

Time complexity O (n), spatial complexity O (n)

voidCountsort (intA[],intNintLowintHigh ) {    intSize = high-low+1; Vector<int> Count (Size,0);//Count[i] Represents low+i appears count[i] times in A     for(inti =0; I < n; i + +) {Count[a[i]-low] + +; }    intIND =0;  for(inti =0; i < size; i + +)    {         while(Count[i]) {A[ind+ +] = low+i; Count[i]--; }    }}

2. Bubble sort (Basic version)

The most basic sorting algorithm, adjacent element 22 is compared and exchanged.

Time complexity O (N2), Spatial complexity O (1). Stable sorting.

void bubblesort (intint  n) {    for (int0; i < n; i + +)    {        for (int0; j < n-i-1; j + +)        {             if(A[j] > a[j+1])                swap (A[j], a[j+1]);     }}}

3. Bubble sort (Accelerated version)

If the intermediate result is ordered, that is, there is no adjacent element that needs to be exchanged during a single traversal, the return is ended.

Time complexity O (N2), Spatial complexity O (1). Stable sorting.

voidBubblesortac (intA[],intN) {    BOOLExchange;  for(inti =0; I < n; i + +) {Exchange=false;  for(intj =0; J < n-i-1; J + +)        {            if(A[j] > a[j+1]) {Exchange=true; Swap (A[j], a[j+1]); }        }        if(Exchange = =false)            return; }}

4. Select sort

During traversal, select the largest element and swap with the end element.

Time complexity O (N2), Spatial complexity O (1). Unstable sort.

voidSelectsort (intA[],intN) {     for(inti = n1; I >=0; I--)    {        intmax = a[0]; intIND =0;  for(intj =1; J <= I; J + +)        {            if(A[j] >max) {Max=A[j]; IND=J; }        }        if(Ind! =i) swap (A[ind], a[i]); }}

5. Insert Sort

Inserts the current element into a locally ordered array.

Time complexity O (N2), Spatial complexity O (1). Stable sorting.

voidInsertsort (intA[],intN) {     for(inti =1; I < n; i + +)    {//Insert A[i] into the right place        intcur = i1; intValue =A[i];  while(cur >=0&& A[cur] >value) {A[cur+1] =A[cur]; Cur--; } a[cur+1] =value; }}

6. Quick Sort

The most commonly used sort, uses pivot to divide the array into two segments, recursively completing the sort.

Time complexity average O (NLOGN), worst case (sorted or reversed) O (N2), Spatial complexity O (1). Unstable sort.

intPartitionintA[],intLowintHigh ) {    intPivot =A[low]; intVacant =Low ;  while(Low <High ) {         while(High > Low && A[high] >=pivot) High--; //A[high] < pivot        if(High >Low ) {A[vacant]=A[high]; Vacant=High ; }        Else             Break;  while(Low < High && A[low] <=pivot) Low++; //A[low] > Pivot        if(Low <High ) {A[vacant]=A[low]; Vacant=Low ; }        Else             Break; } A[low]=pivot; returnLow ;}voidQuickSort (intA[],intLowintHigh ) {    if(Low <High ) {        intpos =partition (A, Low, high); QuickSort (A, Low, POS-1); QuickSort (A, POS+1, high); }}

7. Heap Sequencing

It is divided into two processes of building and exchanging.

Typically used in arrays to find the maximum/small k elements.

Time complexity O (NLOGN), Spatial complexity O (1). Unstable sort.

voidSiftdown (intA[],intStartintend) {    inttemp =A[start]; inti =start; intj =2*i +1;// Left Child     while(J <=end) {        if(j+1<= End && a[j]<a[j+1]) J= j+1;//Choose the bigger        if(Temp >=A[j]) Break; Else{A[i]=A[j]; I=J; J=2*i +1; }} A[i]=temp;}voidHeapsort (intA[],intN) {    //Build Max Heap     for(inti = (n2)/2; I >=0; I--) Siftdown (A, I, n-1); //Sort     for(inti = n1; I >=0; I--) {Swap (a[0], a[i]); Siftdown (A,0, I-1);//Not include the i_th node    }}

8. Merge sort

It is divided into local sort and orderly merging.

Typically used for low memory, requires a sort of interaction with the hard disk.

Time complexity O (NLOGN), Spatial complexity O (n). Stable sorting.

voidMergeintA[],intStartintMidintend) {    intSize1 = mid-start+1; int* L =New int[SIZE1];  for(inti =0; i < size1; i + +) L[i]= a[start+i]; intSize2 = end-mid; int* R =New int[Size2];  for(inti =0; i < size2; i + +) R[i]= a[mid+1+i]; inti =0; intj =0; intIND =start;  while(I < size1 && J <size2) {        if(L[i] <=R[j]) {A[ind]=L[i]; IND++; I++; }        Else{A[ind]=R[j]; IND++; J++; }    }     while(I <size1) {A[ind]=L[i]; IND++; I++; }     while(J <size2) {A[ind]=R[j]; IND++; J++; }}voidMergeSort (intA[],intStartintend) {    if(Start <end) {        intMid = (start+end)/2;        MergeSort (A, start, mid); MergeSort (A, Mid+1, end);    Merge (A, start, mid, end); }}

A summary of the sorting algorithm for C + +

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.