13- bubble sort

1. Basic Introduction

The basic idea bubble sort (Bubble Sorting) is: by treating the collating sequence from front to back (from the smaller index of the element), successively comparing the value of adjacent elements, then the switch if the reverse was found that the larger the value of the element gradually moved toward the rear of the past, like the bubbles under the water gradually take up the same

2 illustrates

3. code implementation

public class BubbleSort {
    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4, 5};
        finalVersion(arr);
        System.out.println("------------------------");
        int arr2[] = new int[80000];
        for(int i = 0; i < 80000; i++)
            arr2[i] = (int) (Math.random() * 800000);
        
        Date date1 = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str1 = sdf.format(date1);
        System.out.println(str1);
        bubbleSort(arr2); // 测试:8w个数据
        Date date2 = new Date();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str2 = sdf2.format(date2);
        System.out.println(str2);
    }
    
    public static void finalVersion(int[] arr) {
        int temp;
        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;
                }
            System.out.printf("第 %d 趟排序之后: %s\n", i+1, Arrays.toString(arr));
            
            if(flag) // 重置flag, 以便下次判断
                flag = false;
            else // 在一趟排序中, 一次交换都没有发生过, 说明已经有序了
                break;
        }
    }

    public static void bubbleSort(int[] arr) {
        int temp;
        // 每经过1次大循环, 即可确定1个元素的最终位置
        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;
                }
    }
    
    public static void detail(int[] arr) {
        int temp;
        // [第1趟] 5个元素, 相邻元素两两进行比较, 一共是比较[0, 3]→4回, 确定了最大元素
        for(int j = 0; j < arr.length-1 -0; j++)
            if(arr[j] > arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        System.out.println("第1趟之后:" + Arrays.toString(arr));
        
        // [第2趟] ∵最大元素的位置已经确定了 ∴只需比较前4个元素, 也就是比较[0, 2]→3回
        for(int j = 0; j < arr.length-1 -1; j++) 
            if(arr[j] > arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        System.out.println("第2趟之后:" + Arrays.toString(arr));
        
        // [第3趟] ∵最大和次大元素的位置都确定了 ∴只需比较前3个元素, 也就是比较[0, 1]→2回
        for(int j = 0; j < arr.length-1 -2; j++) 
            if(arr[j] > arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        System.out.println("第3趟之后:" + Arrays.toString(arr));
        
        // [第4趟] ∵后3个元素的位置都确定了 ∴只需比较前2个元素, 也就是比较[0, 0]→1回
        for(int j = 0; j < arr.length-1 -3; j++) 
            if(arr[j] > arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        System.out.println("第4趟之后:" + Arrays.toString(arr));
        
        // n个元素, 只需确定后n-1个元素的最终位置即可
    }
}

Guess you like

Origin www.cnblogs.com/liujiaqi1101/p/12327592.html