Chapter 7 Sorting Algorithms (2) (Bubble Sort)

7.5 Bubble sort

7.5.1 Basic introduction

The basic idea of ​​bubble sorting (Bubble Sorting) is: by treating the sorting sequence from front to back (starting from the element with a smaller subscript), compare the values ​​​​of adjacent elements in turn, and exchange if the reverse order is found , so that the element with a larger value Gradually move from the front to the back, and gradually rise up like bubbles under the water.

Optimization:
Because during the sorting process, each element is constantly approaching its own position. If there is no exchange after a comparison, it means that the sequence is in order . Therefore, a flag should be set during the sorting process to determine whether the element has been exchanged. Thereby reducing unnecessary comparisons. (The optimization mentioned here can be performed after the bubble sort is written)

7.5.2 Example to demonstrate the bubbling process (diagram)

insert image description here
Summarize the above graphic process:
(1) A total of the size of the array - 1 large loop
(2) The number of sorts in each pass is gradually decreasing
(3) If we find that there is no exchange in a certain pass, Bubble sort can be terminated early. This is optimization

7.5.3 Application examples of bubble sorting

Let us give a specific case to illustrate the bubbling method. We arrange five unordered numbers: 3, 9, -1, 10, -2 into an ordered sequence from small to large using the bubble sort method.

Code:

The original bubble sort:

public static void main(String[] args) {
    
    
        int arr[] = {
    
    3, 9, -1, 10, -2};

        //冒泡排序 的时间复杂度O(n^2)
        int temp = 0;//临时变量
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            for (int j = 0; j < arr.length - 1 - i; j++) {
    
    
                //如果前面的数比后面的数大,则交换
                if (arr[j] > arr[j + 1]) {
    
    
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            System.out.println("第" + (i + 1) + "躺排序后的数组");
            System.out.println(Arrays.toString(arr));
        }
    }

Optimized bubble sorting method:
(Add flag to judge identification)

 public static void main(String[] args) {
    
    
        int arr[] = {
    
    3, 9, -1, 10, -2};

        System.out.println("排序前数组:");
        System.out.println(Arrays.toString(arr));

        bubbleSort(arr);

        System.out.println("排序后数组:");
        System.out.println(Arrays.toString(arr));
    }

    public static void bubbleSort(int[] arr) {
    
    
        //冒泡排序 的时间复杂度O(n^2)
        int temp = 0;//临时变量
        boolean flag = false;//标识变量,表示是否进行过交换
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            for (int j = 0; j < arr.length - 1 - i; j++) {
    
    
                //如果前面的数比后面的数大,则交换
                if (arr[j] > arr[j + 1]) {
    
    
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }

            if (!flag) {
    
    
                break;//在一趟排序中,一次交换都没有发生过
            } else {
    
    
                flag = false;//重置flag,进行下次判断
            }
        }
    }

Test the speed of bubble sort:

public static void main(String[] args) {
    
    
        //测试一下冒泡排序的速度O(n^2), 给80000个数据 测试
        int arr[] = new int[80000];
        for (int i = 0, size = arr.length; i < size; i++) {
    
    
            arr[i] = (int) (Math.random() * 80000);//生成一个【0,80000)数
        }

        long startTime = System.currentTimeMillis();
        bubbleSort(arr);
        long endTime = System.currentTimeMillis();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start = dateFormat.format(new Date(startTime));
        String end = dateFormat.format(new Date(endTime));
        System.out.println("排序前时间:" + start);// 2023-08-20 09:57:17
        System.out.println("排序后时间:" + end);// 2023-08-20 09:57:28
    }
    
public static void bubbleSort(int[] arr) {
    
    
        //冒泡排序 的时间复杂度O(n^2)
        int temp = 0;//临时变量
        boolean flag = false;//标识变量,表示是否进行过交换
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            for (int j = 0; j < arr.length - 1 - i; j++) {
    
    
                //如果前面的数比后面的数大,则交换
                if (arr[j] > arr[j + 1]) {
    
    
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }

            if (!flag) {
    
    
                break;//在一趟排序中,一次交换都没有发生过
            } else {
    
    
                flag = false;//重置flag,进行下次判断
            }
        }
    }

Guess you like

Origin blog.csdn.net/weixin_45828554/article/details/132389138