Bubble sorting algorithm, C language bubble sorting algorithm in detail

Bubble sorting is the simplest sorting method and easy to understand. Although it has many calculation steps and is not the fastest, it is the most basic and must be mastered by beginners.

The principle of bubble sorting is: from left to right, adjacent elements are compared. Each time you compare a round, you will find the largest or smallest one in the sequence. This number will pop up from the far right of the sequence.

Taking the sorting from small to large as an example, after the first round of comparison, the largest number among all numbers will float to the far right; after the second round of comparison, the second largest number among all numbers will float to the second to last Positions ... just compare them round by round, and finally sort from small to large.

For example, sort the following sequence from small to large: 90 21 132 -58 34

The first round:
1) 90 and 21 ratio, 90> 21, then they exchange positions: 21 90 132 -58 34

2) The ratio of 90 to 132, 90 <132, there is no need to exchange positions.
3) The ratio of 132 and –58, 132> –58, then they exchange positions: 21 90 -58 132 34

4) The ratio of 132 to 34, 132> 34, then they exchange positions: 21 90 -58 34 132

At this point the first round is relatively complete. The result of the first round is to find the largest number in the sequence and float to the far right.

When comparing, the nth comparison in each round is a comparison between the nth element and the n + 1th element in the new sequence (if n starts at 1).

The second round:
1) 21 to 90 ratio, 21 <90, there is no need to exchange positions.
2) The ratio of 90 and –58, 90> –58, then they exchange positions: 21 -58 90 34 132

3) The ratio of 90 to 34, 90> 34, then they exchange positions: 21 -58 34 90 132

This is the second round. The result of the second round was to find the second largest number in the sequence and float to the second position on the far right.

Third round:
1) 21 and –58 ratio, 21> –58, then they exchange positions: -58 21 34 90 132

2) 21 to 34 ratio, 21 <34, there is no need to exchange positions.

This is the third round. The result of the third round is to find the third largest number in the sequence and float to the third position on the far right.

Round 4:
1) –58 to 21 ratio, –58 <21, there is no need to exchange positions.

At this point, the entire sequence is sorted. The sequence from small to large is "–58 21 34 90 132". It can also be concluded from this example that if there are n data, then only n–1 rounds need to be compared. In addition to the first round, each round does not need to compare all. Because after the comparison of the previous round, the round that has been compared has found the largest number in the round and floated to the right, so the number on the right does not need to be compared to know that it is large.

Write a program below:

# include <stdio.h>
int main(void)
{
    int a[] = {900, 2, 3, -58, 34, 76, 32, 43, 56, -70, 35, -234, 532, 543, 2500};
    int n;  //存放数组a中元素的个数
    int i;  //比较的轮数
    int j;  //每轮比较的次数
    int buf;  //交换数据时用于存放中间数据
    n = sizeof(a) / sizeof(a[0]);  /*a[0]是int型, 占4字节, 所以总的字节数除以4等于元素的个数*/
    for (i=0; i<n-1; ++i)  //比较n-1轮
    {
        for (j=0; j<n-1-i; ++j)  //每轮比较n-1-i次,
        {
            if (a[j] < a[j+1])
            {
                buf = a[j];
                a[j] = a[j+1];
                a[j+1] = buf;
            }
        }
    }
    for (i=0; i<n; ++i)
    {
        printf("%d\x20", a[i]);
    }
    printf("\n");
    return 0;
}

The output is:
2500 900 543 532 76 56 43 35 34 32 3 2 -58 -70 -234

program, why the number of comparisons per round is j <n–1–i instead of j <n–1?

Because there is a characteristic of bubble sorting, this program is sorted from large to small, so after the first round of sorting, the smallest number will float to the far right; after the second round of sorting, the second smallest number will float to the bottom Two positions; after the third round of sorting, the third smallest number will float to the penultimate position ... That is to say, as many rounds as there are sorts, as many numbers have been sorted according to sorting requirements, they do not need Compare. It is fine to write j <n–1, but the program does a lot of useless work when it is executed.

 

Published 42 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/qq_37659294/article/details/102760464