Quick sort, bubble sort, insert sort, select sort for common sort

Source: Internet
Author: User

On the sorting method, the common sorting methods are insert sort, bubble sort, quick sort, select sort, merge sort, also have heap sort, bucket sort, base sort, base sort.

1. Quick Sort

The operation process is: Randomly select a keyword (the general selection of the first), so that all the keywords and it is compared once, small on the left side, large on its right, and then recursively to the left and right to sort.

Quick sorting has a feature, the more the order of the sequence is near disorder, the higher the efficiency of the algorithm, that is, in the basic order of time Complexity of O (n^2), the best case is O (nlog2n), the average complexity of O (nlog2n), from all within the order, the fast row is the average complexity of all the best, The spatial complexity is also O (nlog2n).

void Quick_sort1 (int s[],int l, int r) {
    int i=l;
    int j=r;
    int x=s[i];//First Pit
    if (i>=j) return;
    while (i!=j) {
    //from right to left to find less than X number to fill s[i];
        while (i<j&&s[j]>=x) {
        j--;
        }
        S[I]=S[J];
        Find a number greater than or equal to x from left to right to fill s[j];
        while (i<j&&s[i]<x) {
        i++;
        }
        S[j]=s[i];
    }
    s[i]=x;
    Quick_sort1 (S, l, i-1);
    Quick_sort1 (S, I+1, j);
}

2. Bubble sort

The basic idea is to repeat the order of the entire sequence, comparing two elements at a time (22 sort), if they do not conform in order to exchange, repeat this until the sequence of numbers no longer need to exchange (end condition). The time complexity is O (n^2), if order is ordered, that is, do not need to exchange elements, but need to scan, so still need O (n) time complexity, the average time complexity of O (n^2).

void Bubble_sort (int s[],int len) {
    int i,j,flag;
    for (i=len-1; i>=1; i--) {
        flag=1;
        for (j=i-1; j>=0; j--) {
            if (S[i]<s[j]) {
            swap (s[i], s[j]);
            flag=0;
            }
        }
        if (1==flag) {//indicates that the sort has completed break;}}}

3. Insert sort (binary inserted here)

The basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, so as to get a new sequential data with a number plus one, the algorithm is suitable for the ordering of a small amount of data, and the time complexity is O (n^2).

void Halfinsert_sort (int s[], int len) {
int temp, low, High, I, J, mid;
for (I=1; i<len; ++i)
{
    temp = s[i];
    Low = 0;
    High = i-1;

    while (high >= low)
    {
    mid = (low + high)/2;

        if (temp < S[mid])
        {high
        = mid-1;
        }

        else
        {Low
        = mid + 1;
        }
    }

    for (j=i-1; j>=low;--j)
    {
    s[j+1] = s[j];
    }

    S[low] = temp;
    }
}

4. Select sort

Select sort (Selection sort) is a simple and intuitive sorting algorithm. It works as follows. The smallest element is first found in the unordered sequence, placed at the beginning of the sort sequence, and then continues to look for the smallest element from the remaining unsorted elements and then to the end of the sort sequence (the sequence that is currently sorted).

void Select_sort (int s[], int len) {
int i,j,min;
for (i=0; i<len-1; i++) {
    min=i;
    for (j=i+1; j<len; J + +) {
        if (S[min]>s[j]) {
        min=j;//mark down the lowest value
        }
    }}
        if (min!=i) {
        Swap (S[i], s[min]);}}

The individual commonly used above four kinds of sorts, makes the simple analysis:

1. The insertion sort is not suitable for sorting applications with large data volumes. The magnitude is less than thousand, then the insertion sort is a good choice.

2. Quick sorting and binary insertion sorting have the same similarities, the difference is that the binary insert sort insert sequence itself is an ordered sequence, when the middle keyword is selected, both sides are ordered. The quick sort is that it is not necessarily orderly.

3. Quick sort (Quicksort) is an improvement to the bubbling sort. By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

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.