Algorithm 01 - Analysis and Implementation of Bubble Sort and Selection Sort

Algorithm 01 - Brute Force

An introduction to brute force

The brute force method (also known as the exhaustive method or the enumeration method) is a simple and direct method of solving problems, often directly based on the description of the problem, so the brute force method is also the easiest to apply. However, the time characteristics of algorithms designed by brute force methods are often the lowest, and typical exponential time algorithms are generally obtained through brute force search.

Common brute force methods: bubble sort, selection sort.

2. Bubble sort

1. Basic idea

Bubble Sort is a relatively simple sorting algorithm in the field of computer science . The name of this algorithm comes from the fact that larger elements will slowly "float" to the top of the sequence by swapping, hence the name "bubble sort".

It repeatedly walks through the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until no more exchanges are needed, that is, the sequence has been sorted.

Bubble sort is a stable sorting algorithm, suitable for sorting a small amount of data (within 10 images), such as fried golden flowers.

2. Code implementation

public static <E extends Comparable<E>> void bubbleSort(E[] arr) {
    if (arr == null || arr.length == 0) {
        return;
    }

    //从小到大排序
    for (int i = 0; i < arr.length - 1; i++) {
        // 判断是否排好序:可能在遍历完之前就已经排好序了
        boolean flag = true;
        for (int j = 0; j < arr.length - 1 - i; j++) {
            E temp = arr[j];
            if (arr[j].compareTo(arr[j + 1]) > 0) {
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                flag = false;
            }
        }
        if (flag) {
            return;
        }
    }
}

3. Selection sort

1. Basic idea

Selection sort is a simple and intuitive sorting algorithm . Its working principle is to select the smallest (or largest) element from the data elements to be sorted each time , and store it at the beginning of the sequence until all the data elements to be sorted are exhausted.

Selection sort is an unstable sorting method (for example, the sequence [5, 8, 3] swaps [5] with [3] for the first time, causing 5 to move after 8). Selection sort has fewer moves and is suitable for sorting a small amount of data (10~20).

2. Code implementation

public static <E extends Comparable<E>> void selectSort(E[] arr) {
    if (arr == null || arr.length == 0) {
        return;
    }

    for (int i = 0; i < arr.length - 1; i++) {
        //先找到最值
        int index = i;
        for (int j = i + 1; j < arr.length; j++) {
            E temp = arr[index];
            if (temp.compareTo(arr[j]) > 0) {
                index = j;
            }
        }

        //然后与最值交换
        if (index != i) {
            E temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
    }
}

The demo has been uploaded to gitee, and students who need it can download it!

Previous: Data Structure 09-Graph

Next: Algorithm 02 - Recursion

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325433896&siteId=291194637