Bubble Sort Algorithm (BubbleSort)

Bubble Sort Algorithm (BubbleSort)

Bubble sort is named after its "adjacent elements are constantly exchanged during the sorting process, and some elements are slowly replaced to the end, which looks like elements are bubbling". It is the simplest sorting algorithm. It is usually used to explain the introductory algorithm of computer programming.

Definition

The implementation of the algorithm is divided into the following steps:

  1. Compare two adjacent elements from the beginning each time
  2. If the latter element is smaller than the previous one, it means that the order is wrong, then swap them
  3. After the loop is completed, scan from the beginning again until there is no element exchange in a certain scan, and repeat the above two processes continuously until everything is in order.

For example, there is an array that needs to be sorted: 5,8,6,3,9,2,1,7 (sort from small to large):

The first round of sorting:

1. First exchange 5 and 8, and find that 5 is smaller than 8, so skip it.

Comparing 2, 8 and 6, find that 8 is greater than 6, exchange positions.

Insert picture description here

3, 8 is compared with 3, 8 is greater than 3, and the positions are exchanged.

Insert picture description here

4, 8 and 9, 8 is smaller than 9, skip

Compare 5, 9 and 2, 9 is greater than 2, exchange
Insert picture description here

Compare 6, 9 and 1, find that 9 is greater than 1, exchange positions.
Insert picture description here

7. Finally, let 9 and 7 swap positions

Insert picture description here

In this way, the element 9 as the largest element of the sequence is already sorted.

Insert picture description here
You can see that the largest number has been sorted to the end, and you don't need to traverse the sorted elements next time.

Implementation

// 普通冒泡算法
    public static void bubbleSort(int[] arr){
    
    
        int i,j,temp,len = arr.length;
        for (i = 0; i < len -1; i++) {
    
    
            for (j = 0; j < len - 1- i; j++) {
    
    
                if(arr[j] > arr[j+1]){
    
    
                     temp = arr[j+1];
                     arr[j+1] = arr[j];
                     arr[j] = temp;
                }
            }
        }
    }

    // 改进的冒泡算法,利用flag做标记。如果在本轮排序中,元素有交换,则说明数列无序,如果没有交换,则说明数列已然有序,直接返回。
    public static void bubbleSort1(int[] arr){
    
    
        boolean flag = true;
        int temp,j = 0; //j的作用是不再遍历已经排序好的数
        while(flag){
    
    
            flag = false;
            for (int i = 0; i< arr.length -1-j;i++){
    
    
                if(arr[i] > arr[i+1]){
    
    
                    temp = arr[i+1];
                    arr[i+1] = arr[i];
                    arr[i] = temp;
                    flag = true;
                }
            }
            j++;
        }
    }

test:

public static void main(String[] args) {
    
    
        int[] arr = new int[]{
    
    1,2,5,6,4,2,3,1,7};
        bubbleSort1(arr);
        System.out.println(Arrays.toString(arr));
    }

result:

[1, 1, 2, 2, 3, 4, 5, 6, 7]

Picture source: lu_1079776757 (forgive me for not making pictures)

Guess you like

Origin blog.csdn.net/weixin_43957211/article/details/114537830