Bubble sequencing and its algorithm optimization analysis

Source: Internet
Author: User

1. Basic Bubble sort

The basic idea of bubbling sorting: Assuming that the ordered array of records D[1...N] is vertically erected, each record d[i] as a bubble, then the heavy bubbles will sink down and the light bubbles will rise. Each of the adjacent two bubbles D[i] and d[i+1] are compared. If d[i]>d[i+1], then swap two bubbles, then compare d[i+1] and d[i+2], and so on, know that all the bubbles are arranged in an orderly manner. Suppose the sort 20,37,11,42,29.

1th time Bubble: 20. 37,11,42,29    d[0] and d[1] Compare    2nd bubbles: 20,11,37,42,29    d[1] and d[2] Compare 3rd bubbles: 20,11,37,42,29    d[2] and d[3] Compare 4th times bubble: 20,11,37,29,42    d[3] and d[4] Comparison

Then we found the heaviest bubble 42, then the same way to find the second and third heavy ... Bubbles until the sorting is complete. According to the above analysis, we need double cycle. The first loop controls the number of times, and the second loop controls the range of data to be sorted. As shown below

No. 0 Time Compare No. 0 to n-1, subscript control 0~n-2 1th Time compare No. 0 to N-2, subscript control 0~n-3. The No. 0 to the first n-i+1, subscript control 0 to N-i-2. Section n-2 comparison No. 0 to 1th, subscript control 0 to 0

The code for the bubbling sorting algorithm is as follows

int main (void) {    int i, J, TMP;    int N = 5;    int A[n] = {20,37,11,42,29};    For (i=0, i<n; i++) {for        (j=0; j<n-i-1; J + +) {            if (a[j]>a[j+1]) {                tmp = a[j];                A[J] = a[j+1];                A[J+1] = tmp;}}    }    for (i=0; i<n; i++)        printf ("%d\n", A[i]);    return 0; }

The best, worst, and average time complexity for bubble sorting is O (n^2). However, if the exchange of bubble position is not found in a certain sort of order, then all the bubbles in the sorted unordered area are satisfied with the light and the principle of the heavy one, that is the positive sequence, then the bubble sort process can be terminated after this scan. Based on this consideration, the first improved algorithm is proposed

2. Bubble Sorting algorithm optimization One: Do not do each scan to determine whether the sorting is completed

If no data exchange occurs in a loop, the data is sorted. Then the rest of the loop does not need to be executed again. The improved algorithm code is as follows

int main (void) {    int i, J, TMP;    int N = 5;    int issorted = 0;    int a[5] = {20,37,11,42,29};    for (i=0; i<n&& (!issorted); i++) {    //Only in the case of no sorting (!issorted) to continue the loop        issorted = 1;        Set sort flag for        (j=0; j<n-i-1; J + +) {            if (a[j]>a[j+1]) {                issorted = 0;                If there is no sort, re-set the flag                TMP = a[j];                A[J] = a[j+1];                A[J+1] = tmp;}}    }    for (i=0; i<n; i++)        printf ("%d\n", A[i]);    return 0; }

This sort method, if the initial state of the data is a positive order, then scan a trip can be completed. The required comparison and the number of times the data movement is the minimum n-1 and 0, that is, the best time complexity of the algorithm is O (n), if the initial data reverse order, you need to n-1 the order, each order to n-i sub-keyword comparison, and each comparison must move data three times to achieve the Exchange data location, In this case the number of comparisons reached the maximum of n (n-1)/2 and the number of moves reached 3n (n-1)/2, so the worst time complexity was O (n^2). The average time complexity is still O (n^2)

3. Bubble Sorting algorithm optimization two:


Reference http://blog.chinaunix.net/uid-22744029-id-1770037.html

Bubble sequencing and its algorithm optimization analysis

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.