Java common sorting algorithm

Overview

Sorting includes internal sorting and external sorting. Internal sorting is the sorting of data records in memory, while external sorting is because the data to be sorted is large and cannot accommodate all sorted records at one time, and external memory needs to be accessed during the sorting process.

Here we talk about the eight sorting is the internal sorting.

write picture description here

direct insertion sort

Code

public static void insertSort(int[] a) {
    // 有序序列、无序序列
    // 第一个元素当然是有序的(a[0])
    for (int i = 1; i < a.length; i++) {
        int temp = a[i];
        int j = i - 1;
        for (; j >= 0 && a[j] > temp; j--) {
            // 将大数往后移动一位
            a[j + 1] = a[j];
        }
        a[j + 1] = temp;
    }

}

Shell's Sort

Hill sorting was proposed by DLShell in 1959, and it has a great improvement over direct sorting. Hill sort is also called shrinking incremental sort

Basic idea:

First, the entire sequence of records to be sorted is divided into several sub-sequences for direct insertion sorting, and when the records in the entire sequence are "basically ordered", direct insertion sorting is performed on all records in turn.

Operation method:

  1. Choose an incremental sequence t1, t2, ..., tk, where ti>tj, tk=1;
  2. Sort the sequence by k times according to the number of incremental sequences k;
  3. For each sorting, according to the corresponding increment ti, the sequence to be sorted is divided into several sub-sequences of length m, and each sub-table is directly inserted and sorted. Only when the increment factor is 1, the entire sequence is treated as a table, and the table length is the length of the entire sequence.

Code

public static void ShellSort(int[] arr) {
    int gap = arr.length / 2;

    while (1 <= gap) {
        // 把距离为 gap 的元素编为一个组,扫描所有组
        for (int i = gap; i < arr.length; i++) {
            int j = 0;
            int temp = arr[i];

            // 对距离为 gap的元素组进行插入排序
            for (j = i - gap; j >= 0 && temp < arr[j]; j = j - gap) {
                arr[j + gap] = arr[j];
            }
            arr[j + gap] = temp;
        }

        gap = gap / 2; // 减小增量
    }
}

Bubble Sort

Algorithmic thinking

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.

The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence through the exchange, hence the name.

Suppose there is an unordered sequence of size N. Bubble sort is to find the i-th small (large) element through pairwise comparison in each sorting process, and arrange it up.

Code

public static void bubbleSort(int[] array) {
    int temp = 0; // 用来交换的临时数

    // length-1趟遍历
    for (int i = 0; i < array.length - 1; i++) {
        // 从后向前依次的比较相邻两个数的大小,遍历一次后,把数组中第i小的数放在第i个位置上
        // 每趟 length-i-1次
        for (int j = array.length - 1; j > i; j--) {
            // 比较相邻的元素,如果前面的数大于后面的数,则交换
            if (array[j - 1] > array[j]) {
                temp = array[j - 1];
                array[j - 1] = array[j];
                array[j] = temp;
            }

        }
    }

}

quicksort

The basic idea of ​​quick sort

1. First, take a number from the sequence as the reference number.
2. During the partitioning process, put all the numbers larger than this number on the right side, and put all the numbers less than or equal to it on the left side
. 3. Repeat for the left and right intervals. The second step, until each interval has only one number

Generally speaking, it is digging and filling + divide-and-conquer method

Summarize the number of pits and fills

1. i =L; j = R; Dig the base number to form the first pit a[i].
2. j – Find a number smaller than it from the back to the front, and after finding it, dig out the number and fill in the previous pit a[i].
3. i++ finds a number larger than it from front to back, and after finding it, digs out this number and fills it in the previous pit a[j].
4. Repeat steps 2 and 3 until i==j, and fill in the reference number into a[i].

Code

public static void quick_sort(int s[], int l, int r) {
    if (l < r) {
        // Swap(s[l], s[(l + r) / 2]); //将中间的这个数和第一个数交换 参见注1
        int i = l, j = r, x = s[l];
        while (i < j) {
            while (i < j && s[j] >= x) // 从右向左找第一个小于x的数
                j--;
            if (i < j)
                s[i++] = s[j];

            while (i < j && s[i] < x) // 从左向右找第一个大于等于x的数
                i++;
            if (i < j)
                s[j--] = s[i];
        }
        s[i] = x;
        quick_sort(s, l, i - 1); // 递归调用
        quick_sort(s, i + 1, r);
    }
}

Summarize

Summary of stability, time complexity and space complexity of various sorting:
write picture description here


Reference documents:
1. https://blog.csdn.net/happy_wu/article/details/51841244
2. https://blog.csdn.net/qy1387/article/details/7752973

Guess you like

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