Detailed explanation of bubble sort - easy to learn and get started

definition

Bubble sort, the English name is Bubble Sort. The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the array through exchange (arranged in ascending or descending order), just like the carbon dioxide bubbles in carbonated drinks will eventually float. Same as the top, hence the name "bubble sort".

sorting process

  1. Taking ascending order (sorting from small to large) as an example, in bubble sorting, each round of sorting will compare two adjacent elements. If the previous element is greater than the next element, the positions of the two elements will be exchanged, so that one round of sorting is completed. After that, the largest element will be in the last position.

  2. Then a second round of sorting is performed. The process is similar to step 1, except that the last element is no longer involved in the comparison because it is already the largest and has been sorted.

  3. Suppose there are n elements in total, then after n-1 rounds of sorting, the elements will be sorted from small to large. Why is it n-1? Suppose there are 10 elements. After 9 rounds of sorting, the next 9 elements are already larger than the first element. At this time, the actual sorting has been completed.

  4. Each round of sorting requires several comparisons. For example, if there are 10 elements, 9 comparisons are required in the first round, 9 elements need to be sorted in the second round, and 8 comparisons are required, and so on. The number of comparisons required in the i-th round of sorting is ni.

From the above, we can know
that for an array of n elements, n-1 rounds of sorting are required.
The number of comparisons required in the mth round of sorting is ni.

Case explanation

#include <iostream>

#define N 12

using namespace std;

int main() {
    
    

    int numbers[N] = {
    
    12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    // N - 1轮排序
    for (int i = 1; i <= N - 1; i++) {
    
    
    	// 每一轮比较N - i次,数组元素下标从0开始
        for (int j = 0; j < N - i; j++) {
    
    
            if (numbers[j] > numbers[j+1]) {
    
    
            	// 元素交换
                int tmp = numbers[j];
                numbers[j] = numbers[j+1];
                numbers[j+1] = tmp;
            }
        } 
        cout<<"第"<<i<<"轮排序完成:";
        for (int k = 0; k < N; k++) {
    
    
            cout<<numbers[k]<<" ";
        }
        cout<<endl;
    }

    cout<<"排序完成:";
    for (int i = 0; i < N; i++) {
    
    
        cout<<numbers[i]<<" ";
    }
    
    return 0;
}

The results of running the program are as follows:

第1轮排序完成:11 10 9 8 7 6 5 4 3 2 1 12 
第2轮排序完成:10 9 8 7 6 5 4 3 2 1 11 12 
第3轮排序完成:9 8 7 6 5 4 3 2 1 10 11 12 
第4轮排序完成:8 7 6 5 4 3 2 1 9 10 11 12 
第5轮排序完成:7 6 5 4 3 2 1 8 9 10 11 12 
第6轮排序完成:6 5 4 3 2 1 7 8 9 10 11 12 
第7轮排序完成:5 4 3 2 1 6 7 8 9 10 11 12 
第8轮排序完成:4 3 2 1 5 6 7 8 9 10 11 12 
第9轮排序完成:3 2 1 4 5 6 7 8 9 10 11 12 
第10轮排序完成:2 1 3 4 5 6 7 8 9 10 11 12 
第11轮排序完成:1 2 3 4 5 6 7 8 9 10 11 12 
排序完成:1 2 3 4 5 6 7 8 9 10 11 12 

To make programming easy, welcome to follow the public account: Wuxian.

Guess you like

Origin blog.csdn.net/weixin_41678668/article/details/124906410