With the same complexity, why is insertion sort more popular than bubble sort?

Earlier I learned about bubble sorting and insert sorting. The time complexity and space complexity are the same:

  • Best case time complexity: O (n)
  • Worst-case time complexity: O (n2)
  • Average time complexity: O (n2)
  • Space complexity: O (1), stable sorting algorithm

 

But why is insert sort used more in actual development?

The reasons are as follows:

  • For the same array, bubble sorting and insert sorting, under optimal conditions, the number of times that the data needs to be exchanged is the same (ie, the original array has the same reverse order degree)
  • Each time data is exchanged, the mobile data in bubble sort is more complicated than insert sort. Bubble sort has been assigned 3 times and insert sort has been assigned once


Code comparison:

//冒泡排序
int temp = array[j + 1];
array[j+1] = array[j];
array[j] = temp;
hasSwitch = true;//有数据交换

//插入排序
if (array[j] > value) {
    array[j+1] = array[j];
} else {
    break;
}


Test code:

package constxiong.interview.algorithm;

import java.util.Random;

/**
 * 测试冒泡排序
 * @author ConstXiong
 * @date 2020-04-10 09:36:54
 */
public class CompareBubbleAndInsertionSort {
    
    public static void main(String[] args) {
        //生成两个一样长度的随机数组
        int length = 10000;
        int[] array_1 = generateArray(length);
        int[] array_2 = new int[length]; 
        System.arraycopy(array_1, 0, array_2, 0, length);
        print(array_1);
        print(array_2);
        
        //比较冒泡排序与插入排序的耗时
        long array_1_start = System.currentTimeMillis();
        bubbleSort(array_1);
        System.out.println("bubbleSort cost time : " + (System.currentTimeMillis() - array_1_start));
        long array_2_start = System.currentTimeMillis();
        insertionSort(array_2);
        System.out.println("insertionSort cost time : " + (System.currentTimeMillis() - array_2_start));
        
        //打印排序后的两个数组,看看结果是否正确
        print(array_1);
        print(array_2);
    }
    
    /**
     * 生成随机数组
     * @param length
     * @return
     */
    private static int[] generateArray(int length) {
        Random r = new Random();
        int[] array = new int[length];
        for (int i = 0; i < array.length; i++) {
            array[i] = r.nextInt(length);
        }
        return array;
    }
    
    /**
     * 冒泡排序
     * @param array
     */
    private static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            //提前退出冒泡循环的标志
            boolean hasSwitch = false;
            //因为使用 j 和 j+1 的下标进行比较,所以 j 的最大值为数组长度 - 2
            for (int j = 0; j < array.length - (i+1); j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j + 1];
                    array[j+1] = array[j];
                    array[j] = temp;
                    hasSwitch = true;//有数据交换
                }
            }
            //没有数据交换退出循环
            if (!hasSwitch) {
                break;
            }
        }
    }
    
    /**
     * 插入排序
     */
    private static void insertionSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            int value = array[i];
            for (; j >= 0; j--) {
                if (array[j] > value) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = value;
        }
    }
    
    /**
     * 打印数组
     * @param array
     */
    private static void print(int[] array) {
        for(int i : array) {
            System.out.print(i);
        }
        System.out.println();
    }

}

 

Print the result:

 

As the length of the array increases, bubble sorting takes more time than insert sorting.

 

 


[Java interview questions and answers] sorting recommendations

 

Published 562 original articles · praised 1543 · 1.65 million views +

Guess you like

Origin blog.csdn.net/meism5/article/details/105444672