[Classic] bubble sort algorithm

Bubble algorithm

Bubble sort: start from one end to the other end of the bubbling sequence, sequentially comparing the magnitude of two adjacent numbers.

Set array length is N.

1. Comparison before and after each round of adjacent two data, if the previous data is greater than or less than the back of the data, two data will be exchanged.

2. Thus the first round of data 0 to N-1 array data traversed once after a maximum or minimum of the array to the first data on the N-1 position.

3. Comparative to the first round of index N-1 data (the last one), after each comparison are -1.

#include <stdio.h>

    int main () {
    int list[10] = {5,23,86,21,43,67,45,34,58,23};
    int i, j, temp;
    for (i = 0; i < 10 - 1 ; i++)
        for (j = 1; j < 10 - i; j++)
        if (list[j - 1] > list[j])
    {
        temp = list[j - 1];
        list[j - 1] = list[j];
        list[j] = temp;
    }
    for (i = 0;i < 10; i++)
        printf("%d\n",list[i]);
}

 

optimization

There is a bubbling biggest problem is that regardless of the sequence of ordered sequence or not, double loop of each comparison is performed.

Here optimize it, set a flag, if this round of exchange took place, compared with 1, and 0 otherwise.

If there is a trip to the exchange did not occur, indicating the sort has been completed, the end of the sort.

#include <stdio.h>

    int main () {
    int list[10] = {5,23,86,21,43,67,45,34,58,23};
    int  j, temp;
    int  i = 10;
    int bool = 1;
    while(bool)
    {    
        bool = 0;
        for (j = 1; j < i; j++)
            if (list[j - 1] > list[j])
        {
            temp = list[j - 1];
            list[j - 1] = list[j];
            list[j] = temp;
            bool = 1;
        }
        i--;
    }
    for (i = 0;i < 10; i++)
        printf("%d\n",list[i]);
}

 

 

 

Graphic bubble sort

In [8,2,5,9,7] do exemplary set of numbers:

Bubbling from left to right, right to move the small

The first round bubble:

First, compare the size of the first and the second number is the number, we found 2 is smaller than 8, then stay in place, it has not been altered. Position or 8,2,5,9,7.

A frame pointer moves to the right, then compared:

Comparing the second and the third number size number, 2 to 5 was found to be small, the switching position, the switching array is updated to: [8,5,2,9,7].

A frame pointer moves further to the right, to continue the comparison:

Comparison of the number and size of the third fourth number, 2 to 9 was found to be small, the switching position, the switching array is updated to: [8,5,9,2,7]

Similarly, the pointer moves to the right and then to continue the comparison:

Compare the size of the number 4 and number 5, 7 was found to be less than 2, the position of the exchange, the exchange is updated array: [8,5,9,7,2]

The next step, then move the cursor to the right, found to have been in the end, the end of the round bubble, in 2 is already sorted rightmost digit.

By contrast constant exchange this round, the smallest number in the array to move to the far right.

 

The second round bubble:

 

 

 

 


The right side of the row of numbers 2 is already sorted, it is no longer involved in the comparison, so the end of the current round of bubbling, bubbling the final round to take the top number 5 is attributed to an ordered sequence, the array has changed now become [ 8,9,7,5,2].

 

 

The third round bubble:

Since 8 is greater than 7, so that the same position, now the third round bubble has ended, the final results of the third round is bubbling [9,8,7,5,2]

 

 

The fourth round bubble:

 

 And 8 to 9, the same position, that identified eight into the ordered sequence, then only the last digit 9, placed at the end, since the end of the sort.

 

Reference: https://mp.weixin.qq.com/s/D0-lOLFkfppTnvN9yK_lBg

Guess you like

Origin www.cnblogs.com/-wenli/p/12113523.html