Classic Algorithm Shell's Sort (Shell's Sort)

Event address: CSDN 21-day learning challenge

The biggest reason for learning is to get rid of mediocrity. One day earlier, there will be more splendor in life; one day later, one day more mediocrity.

1. Concept

希尔排序, also known as 缩小增量排序, is an unstable sorting algorithm. So how does Hill sorting reduce the increment? First, group the sequence to be sorted by a certain increment of the subscript, and sort each group. 直接插入排序算法As the increment gradually decreases, each group contains more and more elements/elements , when 增量it is reduced to 1, the entire file is just divided into one group, so the algorithm ends.
The idea of ​​the algorithm
is to first divide a group of numbers to be sorted into several groups by a certain increment d, and the subscripts recorded in each group differ by d, sort all the elements in each group, and then use a smaller increment to sort them Group and sort within each group. When the increment is reduced to 1, the entire number to be sorted is divided into one group, and the sorting ends.
In general, half of the sequence is initially taken as the increment, and then halved each time until the increment is 1. If there are a total of ten elements, then the values ​​of the incremental sequence are: 5, 2, 1

2. Pseudocode

d = A.length / 2
while d > 0
    for i = 1 to d
        for j = i + d to A.length by d
            tmp = A[j]
            k = j - d
            while k > 0 and A[k] > tmp
                A[k + d] = A[k]
                k = k - d
            A[k + d] = tmp
    d = d / 2

3. Core code implementation

3.1 Java Edition

public static void main(String[] args){
    
    
    int[] array={
    
    49,38,65,97,76,13,27,49,78,34};
    System.out.println("排序之前:");
    for(int i=0;i<array.length;i++){
    
    
        System.out.print(array[i]+" ");
    }
    //希尔排序
    int gap = array.length;
    while (true) {
    
        
        gap /= 2;   //增量每次减半    
        for (int i = 0; i < gap; i++) {
    
            
            for (int j = i + gap; j < array.length; j += gap) {
    
    //这个循环里其实就是一个插入排序                       
                int k = j - gap;            
                while (k >= 0 && array[k] > array[k+gap]) {
    
    
                    int temp = array[k];
                    array[k] = array[k+gap];
                    array[k + gap] = temp;                
                    k -= gap;            
                }                
            }    
        }    
        if (gap == 1)        
            break;
    }
 
    System.out.println();
    System.out.println("排序之后:");
    for(int i=0;i<array.length;i++){
    
    
        System.out.print(array[i]+" ");
    }
}

3.2 Python version

def shell_sort(seq):
    seq_len = len(seq)
    step = seq_len >> 1
    # 增量小于1说明最终排序已完成
    while step:
        for i in range(step, seq_len):
            # j记录元素原始位置,用于查找前面所有小于当前元素的元素
            j = i
            while seq[j] < seq[j-step] and j-step >= 0:
                seq[j], seq[j-step] = seq[j-step], seq[j]
                # 改变下标,查看前面是否还有更小的值
                j -= step
        print(f"增量{
      
      step}的排序:", seq)
        # 每次增长减小一半
        step = step >> 1

4. Algorithm efficiency analysis

The execution time of Hill sort depends on the increment sequence. Common characteristics of good incremental sequences:

  • The final increment must be 1
  • Try to avoid the situation where the values ​​in the sequence are multiples of each other.

空间复杂度For: O(1)
时间复杂度for: O(n^(1.3—2))

Guess you like

Origin blog.csdn.net/weixin_42182599/article/details/126456133