Data structure and algorithm-C/C++ realizes Bubble Sort algorithm [Recommended collection]

1. Introduction to Bubble Sort

Bubble Sort, also known as bubble sort or bubble sort. It is a relatively simple sorting algorithm. It will traverse several secondary sequence of numbers. Each time it traverses, it will compare the sizes of two adjacent numbers from the front to the back; if the former is larger than the latter, they will exchange their positions. In this way, after a traversal, the largest element is at the end of the sequence! When traversing again using the same method, the second largest element is arranged before the largest element. Repeat this operation until the entire sequence is in order!

The editor recommends my own linuxC/C++ language technology exchange group: [ 1106675687 ] I have compiled some learning books and video materials that I think are better to share in the group files, and you can add them if you need them!

Two, bubble sorting graphic description

2.1 Bubble sort C implementation one

void bubble_sort1(int a[], int n)
{
    
    
    int i,j;

    for (i=n-1; i>0; i--)
    {
    
    
        // 将a[0...i]中最大的数据放在末尾
        for (j=0; j<i; j++)
        {
    
    
            if (a[j] > a[j+1])
                swap(a[j], a[j+1]);
        }
    }
}

Let’s take the sequence {20,40,30,10,60,50} as an example to demonstrate its bubble sorting process (as shown in the figure below).
Insert picture description here
Let's analyze the first pass sorting.
When i=5, j=0, a[0]<a[1]. At this time, do nothing!
When i=5 and j=1, a[1]>a[2]. At this time, exchange the values ​​of a[1] and a[2]; after the exchange, a[1]=30 and a[2]=40.
When i=5 and j=2, a[2]>a[3]. At this time, exchange the values ​​of a[2] and a[3]; after the exchange, a[2]=10 and a[3]=40.
When i=5, j=3, a[3]<a[4]. At this time, do nothing!
When i=5, j=4, a[4]>a[5]. At this time, exchange the values ​​of a[4] and a[5]; after the exchange, a[4]=50 and a[3]=60.

Therefore, after the first pass is sorted, the sequence {20,40,30,10,60,50} becomes {20,30,10,40,50,60}. At this time, the value at the end of the sequence is the largest.

According to this method: after
the second pass is sorted, a[5...6] in the sequence is ordered.
After the third pass is sorted, a[4...6] in the sequence is ordered.
After the fourth pass is sorted, a[3...6] in the sequence is ordered.
After the fifth pass is sorted, a[1...6] in the sequence is ordered.

After the fifth round of sorting, the entire sequence is ordered.

2.2 Bubble sort C implementation two

Observe the above bubble sorting flow chart, after the third pass sorting, the data is already in order; the fourth pass and the fifth pass are not exchanged. Next, we optimize the bubble sort to make it more efficient: add a marker, if a swap occurs during a traversal, the marker is true, otherwise it is false. If there is no exchange in a certain trip, the sorting has been completed!

void bubble_sort2(int a[], int n)
{
    
    
    int i,j;
    int flag;                 // 标记

    for (i=n-1; i>0; i--)
    {
    
    
        flag = 0;            // 初始化标记为0

        // 将a[0...i]中最大的数据放在末尾
        for (j=0; j<i; j++)
        {
    
    
            if (a[j] > a[j+1])
            {
    
    
                swap(a[j], a[j+1]);
                flag = 1;    // 若发生交换,则设标记为1
            }
        }

        if (flag==0)
            break;            // 若没发生交换,则说明数列已有序。
    }
}

Third, the time complexity and stability of bubble sort

Bubble sort time complexity

The time complexity of bubble sort is O(N2). Suppose there are N numbers in the sequence being sorted. The time complexity of traversal is O(N). How many traversals are needed? N-1 times! Therefore, the time complexity of bubble sort is O(N2).

Bubble sort stability

Bubble sort is a stable algorithm, which satisfies the definition of stable algorithm. Algorithm stability-suppose there is a[i]=a[j] in the sequence, if before sorting, a[i] is before a[j]; and after sorting, a[i] is still before a[j] . Then this sorting algorithm is stable!

The editor recommends my own linuxC/C++ language technology exchange group: [ 1106675687 ] I have compiled some learning books and video materials that I think are better to share in the group files, and you can add them if you need them!
Insert picture description here

Fourth, bubble sort implementation

Bubble sort C implementation
Implementation code (bubble_sort.c)

 View Code

Bubble sort C++ implementation
Implementation code (BubbleSort.cpp)

 View Code

Bubble Sort Java Implementation
Code (BubbleSort.java)

View Code

The principles and output results of the above three implementations are the same. Here is their output:

before sort:20 40 30 10 60 50 
after  sort:10 20 30 40 50 60

Guess you like

Origin blog.csdn.net/m0_50662680/article/details/112949416