3 simple sorting algorithms-bubbling, selection, insertion sort

1. Bubbling sorting
means starting from the left, repeatedly visiting the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. Through the comparison again and again, the larger the element will slowly "float" to the top of the sequence through exchange, hence the name "bubble". It is a stable sorting algorithm.
Implementation:
1. Conventional implementation

public class BubbleSort {

    public static void main(String[] args) {
        int[] target = {
   
   1,7,6,4,5,2,9,4,6};
        //临时存储数据
        int temp = 0;
        for (int i = 0; i < target.length; i++) {
            for (int j = 0; j < target.length-1; j++) {
                //当左边的比右边的数字大则交换数据
                if(target[j] > target[j+1]) {   
                    temp = target[j];
                    target[j] = target[j+1];
                    target[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < target.length; i++) {
        System.out.print(target[i] + " ");
        }
    }
}

We can see that some of the loops above are actually redundant. Why? Because every time it bubbling, the following order is already arranged, so we don’t need to repeat the judgment on them, so the outer loop starts from the end of the array, here every time the loop is looped down, the outer loop is reduced by 1 , Because the latter has been sorted.
2. The optimization is implemented as follows:

public class BubbleSort {

    public static void main(String[] args) {
        int[] target = {
   
   1,7,6,4,5,2,9,4,6};
        //临时存储数据
        int temp = 0;
        //优化后
        for (int i = target.length-1; i > 1; i--) {
            for (int j = 0; j < i; j++) {
                if(target[j] > target[j+1]) {
                    temp = target[j];
                    target[j] = target[j+1];
                    target[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < target.length; i++) {
        System.out.print(target[i] + " ");
        }
    }
}

The time complexity of bubble sort is: O(N²)

2. Selection sorting
Improved bubble sorting, reducing the number of exchanges from O(N²) to O(N), and the number of comparisons is still O(N²). Not a stable sorting algorithm.
Each time the smallest (or largest) element is selected from the data elements to be sorted and stored at the beginning of the sequence until all the data elements to be sorted are arranged.
Overview: Compare the size of the previous element and the next element in the array. If the next element is smaller than the previous element, use a variable k to remember its position, and then the second comparison, the previous "next element" changes Become the "previous element", continue to compare with his "next element" If the latter element is smaller than him, use the variable k to remember its position (subscript) in the array, wait until the end of the loop, and then exchange The new minimum and the previously selected minimum. (Baidu Encyclopedia)
Implementation:

public class SelectSort {
    
    

    public static void main(String[] args) {
        int[] target = {
   
   1,7,6,4,5,2,9,4,6};         
        int temp = 0;
        //暂存最小值的索引
        int index;
        for (int i = 0; i < target.length; i++) {
            index = i;
            for (int j = i+1; j < target.length; j++) {
                if(target[index] > target[j]) {
                    index = j;
                }
            }
            //交换
            temp = target[index];
            target[index] = target[i];
            target[i] = temp;
        }
        for (int i = 0; i < target.length; i++) {
            System.out.print(target[i] + " ");
        }
    }

}

The time complexity of selecting sort is: O(N²)

3. Insertion sorting
Insert a piece of data into the ordered data that has been sorted to obtain a new ordered data whose number is plus one. The algorithm is suitable for sorting a small amount of data. It is a stable sorting method.
The insertion algorithm divides the array to be sorted into two parts: the first part contains all the elements of the array, except for the last element (let the array have one more space for insertion), and the second part contains only this element (The element to be inserted). After the first part is sorted, insert this last element into the sorted first part.
achieve:

public class InsertSort {
    
    

    public static void main(String[] args) {
        int[] target = { 1, 7, 6, 4, 5, 2, 9, 4, 6 };
        //暂存待插数据
        int temp = 0;
        //暂存待插数据下标
        int index = 0;
        //从第二个数开始,因为第一个可以看做有序
        for (int i = 1; i < target.length; i++) {
            temp = target[i];
            index = i;
            while (index > 0 && target[index-1] >= temp) {
                target[index] = target[index - 1];
                index--;
            }
            target[index] = temp;
        }

        for (int i = 0; i < target.length; i++) {
            System.out.print(target[i] + " ");
        }
    }
}

The time complexity of insertion sort is: O(N²), but if the data is basically ordered or ordered, the time complexity of insertion sort is O(N).

Here is just a relatively simple sorting algorithm, suitable for entry learning, there will be more complex algorithms later.

References: Java data structures and algorithms. (Second Edition)

Guess you like

Origin blog.csdn.net/u010857795/article/details/60469874