Java programming: sorting algorithm-bubble sort

basic introduction

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

optimization

Because each element is constantly approaching its position during the sorting process, if there is no exchange in a comparison, the sequence is in order. Therefore, a flag must be set during the sorting process to determine whether the element has been exchanged. Thereby reducing unnecessary comparisons.

Example to demonstrate the bubbling process (illustration)

Insert picture description here

Selective sorting is also an internal sorting method, which is to select an element from the data to be sorted according to the specified rules, and then exchange positions according to the regulations to achieve the purpose of sorting.

Code

package sort;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class BubbleSort {
    
    
    public static void main(String[] args) {
    
    
        //int[] arr = {3, 9, -1, 10, -2};
        // 为了容易理解,将冒泡排序的演变过程展示出来
        // 第一趟排序,将最大的数排到最后
        /*int temp = 0;   // 临时变量
        for (int i = 0; i < arr.length - 1; i++) {
            // 如果前面的数比后面的数大,则交换
            if (arr[i] > arr[i + 1]) {
                temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        System.out.println("第一趟排序后的数组:");
        System.out.println(Arrays.toString(arr));
        // 第二趟排序,就是把第二大的数排在倒数第二位
        for (int i = 0; i < arr.length - 1 - 1; i++) {
            // 如果前面的数比后面的数大,则交换
            if (arr[i] > arr[i + 1]) {
                temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        System.out.println("第二趟排序后的数组:");
        System.out.println(Arrays.toString(arr));
        // 第三趟排序,就是把第三大的数排在倒数第三位
        for (int i = 0; i < arr.length - 1 - 1 - 1; i++) {
            // 如果前面的数比后面的数大,则交换
            if (arr[i] > arr[i + 1]) {
                temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        System.out.println("第三趟排序后的数组:");
        System.out.println(Arrays.toString(arr));
        // 第四趟排序,就是把第三大的数排在倒数第四位
        for (int i = 0; i < arr.length - 1 - 1 - 1 - 1; i++) {
            // 如果前面的数比后面的数大,则交换
            if (arr[i] > arr[i + 1]) {
                temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        System.out.println("第四趟排序后的数组:");
        System.out.println(Arrays.toString(arr));*/
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
    
    
            arr[i] = (int) (Math.random() * 80000);// 生成一个0-80000的数据
        }
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1Str = simpleDateFormat.format(date1);
        System.out.println("排序前的时间为:" + date1Str);
        BetterBubbleSortByAsc(arr);
        Date date2 = new Date();
        String date2Str = simpleDateFormat.format(date2);
        System.out.println("排序后的时间为:" + date2Str);
    }

    /**
     * 冒泡排序的事件复杂度 O(n²)
     *
     * @param arr 待排序数组
     */
    public static void BubbleSortByAsc(int[] arr) {
    
    
        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;
                }
            }
        }
    }

    /**
     * 冒泡排序优化
     *
     * @param arr 待优化数组
     */
    public static void BetterBubbleSortByAsc(int[] arr) {
    
    
        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;    // 只要在排序中发生了交换,就将flag置为true
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            if (!flag) {
    
    
                // 如果flag人为false,即一次都没发生
                break;
            } else {
    
    
                flag = false;   // 重置flag,进行下次判断
            }
        }
    }
}

in conclusion

80000 data takes about 15-17 seconds.

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108773843