Bubble sorting and optimization of data structure and algorithm

Bubble sorting and optimization of data structure and algorithm

A basic introduction

(1) 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 , if the reverse order is found, then swapping to make the value more The big elements gradually move from the front to the back, rising up like bubbles under the water.
(2) Optimization:
Because each element is constantly approaching its position during the sorting process, if there is no exchange after a comparison, it means that the sequence is in order. Therefore
, a flag must be set during the sorting process to determine whether the element has been performed. exchange. Thereby reducing unnecessary comparisons. (The optimization mentioned here can be
carried out after the bubble sorting is written)

Two demonstrate the bubbling process

Insert picture description here
Summary of the graphical process above:

(1) Perform a total of -1 large loops (n-1) for the size of the array

(2) The number of sorts in each pass is gradually decreasing (n-1-i)

(3) If we find that there is no exchange in a certain round of sorting, we can end the bubbling sorting early. This is optimization

Demo code implementation

/*

// 第一趟排序,就是将第一大的数排在倒数第一位
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("第二趟排序后的数组");
System.out.println(Arrays.toString(arr));

// 第二趟排序,就是将第二大的数排在倒数第二位
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("第二趟排序后的数组");
System.out.println(Arrays.toString(arr));

// 第三趟排序,就是将第三大的数排在倒数第三
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("第三趟排序后的数组");
ystem.out.println(Arrays.toString(arr));

// 第四趟排序,就是将第 4 大的数排在倒数第 4 位
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("第四趟排序后的数组");
System.out.println(Arrays.toString(arr)); */

}


Three bubble sorting code implementation

package com.datastrucate.sort;

import java.util.Arrays;

/**
 * ClassName:BubbleSort
 * Package:com.datastrucate.sort
 * Description:
 * 冒泡排序及优化
 * 思路:两层循环,相邻两数比较大小,n代表数组容量,i代表外部循环次数
 * 外部循环次数:n-1,对应比较趟数
 * 内部循环次数:n-1-i,对应每趟比较次数
 * 空间复杂度:O(n^2)
 *
 * @Date:2021/3/3 16:08
 * @Author:hm
 */
public class BubbleSort {
    
    

    public static void main(String[] args) {
    
    

        int[] arr = new int [80000];
        //生成8万个随机数
        for (int i = 0;i < 80000;i++){
    
    
            arr[i] = (int)(Math.random()*80000);//范围为[0,80000)
        }
        long start = System.currentTimeMillis();
        System.out.println("排序前时间:"+start+"秒");
        bubbleSort(arr);
        long end = System.currentTimeMillis();
        System.out.println("排序后时间:"+end+"秒");
        System.out.println("用时:"+(end-start)+"秒");
    }

    /**
     * 冒泡排序方法
     * 外部循环次数:n-1,对应比较趟数
     * 内部循环次数:n-1-i,对应每趟比较次数
     * @param arr
     */
    private static void bubbleSort(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;//代表交换了
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
//            System.out.println("第"+(i+1)+"趟比较:");
//            System.out.println(Arrays.toString(arr));
            //每一趟比较后判断flag
            if (!flag){
    
    //没有交换过
                break;
            }else {
    
    //比较过,需重置flag为flase,用于下一趟比较

                flag = false;
            }
        }

    }
}

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/114322079
Recommended