Common sorting algorithm - Hill sort

Hill sorting is a variant of insertion sorting. Its basic idea is to divide the entire record sequence to be sorted into several subsequences, and perform direct insertion sorting respectively. Then gradually reduce the interval, and perform insertion sorting again, until finally the entire sequence becomes an ordered sequence. Hill sort is unstable.

The process of Hill sorting is as follows:

1 Select an incremental sequence, usually gap = n/2, n is the length of the array
2 Group the array according to the incremental sequence
3 Insert and sort the elements in each group directly
4 Repeat steps 2~3 until the incremental sequence becomes If it is 1, the entire array is sorted.

The c language implementation is as follows:

void shell_sort(int arr[], int n) {
    
    
    int i, j, gap;
    for (gap = n / 2; gap > 0; gap /= 2) {
    
    
        for (i = gap; i < n; i++) {
    
    
            int temp = arr[i];
            for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
    
    
                arr[j] = arr[j - gap];
            }
            arr[j] = temp;
        }
    }
}

The time complexity of Hill sorting is not easy to determine, it is generally considered to be O(n^1.3), and the space complexity is O(1). The advantage of Hill sorting is that the number of inversions in the sequence can be determined in advance, thereby reducing the number of comparisons required for sorting.

The advantage of Hill sorting is to find the reverse order in the sequence in advance, reduce the number of comparisons, and improve the sorting efficiency. The space complexity of Hill sort is constant level, and the space overhead is small.

The disadvantage of Hill sorting is instability, and the time complexity is not easy to determine. The time complexity of Hill sorting depends on the selection of the incremental sequence. If the incremental sequence is unreasonable, the time complexity of Hill sorting may decrease. Degenerates to O(n^2), or even worse.

Hill sorting is suitable for most normal data. When the amount of data is relatively large, the advantage of Hill sorting is more obvious, because it can find the reverse order in the sequence in advance and reduce the number of comparisons.

It should be noted that the selection of the incremental sequence has a great influence on the time complexity of Hill sorting. In general, the incremental sequence is 2k -1, which guarantees that the worst time complexity of Hill sort is O(n 1.3).

In addition, Hill sort is not as stable as quick sort or merge sort, so Hill sort is not very suitable for scenarios with special requirements for sort stability.

In short, Hill sorting is a more effective sorting algorithm. It is a variant of insertion sorting. It can find the reverse order in the sequence in advance, reduce the number of comparisons, and improve sorting efficiency. However, Hill sorting is not as stable as fast sorting or merge sorting. In scenarios where sorting stability has special requirements, Hill sorting is not very suitable.

Summary: Hill sorting is an unstable sorting algorithm. Its time complexity is not easy to determine, but its space complexity is at a constant level. It is suitable for most normal data. When the amount of data is relatively large, Hill sorting advantage is more obvious.

Guess you like

Origin blog.csdn.net/sorcererr/article/details/128700852