Understand the Hill sorting algorithm

    In this tutorial, you will learn how Hill Sort works. In addition, you will find examples using C language.
    Hill sorting is an algorithm that first sorts the elements that are separated from each other, and then shortens the interval between the elements to be sorted in turn. It is a general version of insertion sort.
    In Hill sorting, the elements are sorted at a specific interval. The spacing between elements gradually decreases according to the order of use. The performance of Hill sorting depends on the type of sequence used for a given input array.
    Some of the best sequences to use are:

  • Hill original sequence: N/2, N/4, …, 1
  • Knuth increment: 1, 4, 13, …, (3k – 1) / 2
  • Sedgewick increment: 1, 8, 23, 77, 281, 1073, 4193, 16577…4j+1+ 3·2j+1
  • Hibbard increment: 1, 3, 7, 15, 31, 63, 127, 255, 511...
  • Papernov and Stasevich increments: 1, 3, 5, 9, 17, 33, 65,...
  • Pratt:1, 2, 3, 4, 6, 9, 8, 12, 18, 27, 16, 24, 36, 54, 81…
How does Hill Sort work?
  1. Suppose we need to sort the following array.
    Insert picture description here
  2. In the algorithm, we use Hill's original sequence (N/2, N/4, ... 1) as the interval.
    In the first loop, if the array size is N = 8, the elements with an interval of N/2 = 4 are compared and exchanged (if they are not in order).
    a. Compare the 0th element with the 4th element.
    b. If the 0th element is greater than the 4th element, the 4th element is first stored in the temp variable, the 0th element (that is, the larger element) is stored in the 4th position, and the element stored in temp is stored In the 0th position.
    Insert picture description here
        For all remaining elements, this process will continue.
    Insert picture description here
  3. In the second loop, take the interval of N/4=8/4=2, and sort the elements at these intervals again.
    Insert picture description here
        You may be confused at this point.
    Insert picture description here
        The 4th and 2nd elements are compared, and the 2nd and 0th elements are also compared. All elements of the current interval in the array are compared.
  4. The same process is applied to the remaining elements.
    Insert picture description here
  5. Finally, when the interval is N/8=8/8=1, the array elements with an interval of 1 are sorted. The array is now sorted.
    Insert picture description here

Note:
The legend for the second sorting of the original text is incorrect.
The first interval is 4, and the array after sorting is:

5 6 3 1 9 8 4 7

correct.
The second interval is 2, sorting 5,3,9,4 and 3,4,5,9 respectively, the array after sorting is:

3 1 4 6 5 7 9 8

The third interval is 1, and the array after sorting is:

1 3 4 5 6 7 8 9
Hill sorting algorithm pseudo code
shellSort(array, size)
  for interval i <- size/2n down to 1
    for each interval "i" in array
        sort all the elements at interval "i"
end shellSort
C example
// Shell Sort in C programming

#include <stdio.h>

// Shell sort
void shellSort(int array[], int n) {
    
    
  // Rearrange elements at each n/2, n/4, n/8, ... intervals
  for (int interval = n / 2; interval > 0; interval /= 2) {
    
    
    for (int i = interval; i < n; i += 1) {
    
    
      int temp = array[i];
      int j;
      for (j = i; j >= interval && array[j - interval] > temp; j -= interval) {
    
    
        array[j] = array[j - interval];
      }
      array[j] = temp;
    }
  }
}

// Print an array
void printArray(int array[], int size) {
    
    
  for (int i = 0; i < size; ++i) {
    
    
    printf("%d  ", array[i]);
  }
  printf("\n");
}

// Driver code
int main() {
    
    
  int data[] = {
    
    9, 8, 3, 7, 5, 6, 4, 1};
  int size = sizeof(data) / sizeof(data[0]);
  shellSort(data, size);
  printf("Sorted array: \n");
  printArray(data, size);
}
the complexity

    Hill sorting is an unstable sorting algorithm because the algorithm does not check the elements between intervals.
    time complexity

  • Worst-case complexity: less than or equal to O( n 2 n^2n2 )
        According to Poonen's theorem, the worst-case complexity of Hill sorting isΘ (N log N) 2 / (loglog N) 2) Θ(Nlog N)^2/(log log N)^2)Θ ( N l o g N )2/(loglogN)2) Θ ( N l o g N ) 2 / l o g l o g N ) Θ(Nlog N)^2/log log N) Θ ( N l o g N )2 /loglogN)Θ (N (log N) 2) Θ (N (log N) ^ 2)Θ ( N ( l o g N )2 )Or somewhere in between.
  • Best-case complexity: O(n*log n)
        When the array is sorted, the total number of comparisons for each interval (or increment) is equal to the size of the array.
  • Average case complexity: O(n*log n) is
        approximately O (n 1.25) O(n^{1.25})O ( n1.25)

    The complexity depends on the selected interval. For different selected increment sequences, the above-mentioned complexity is different. The best incremental sequence is unknown.
    Space complexity: The space complexity of
    Hill sorting is O(1).

Hill sort application

    Use Hill sorting in the following situations:

  • The call stack is the daily overhead. The uClibc library uses this sorting.
  • Recursion exceeds the limit. The bzip2 compressor uses it.
  • When adjacent elements are far apart, the performance of insertion sort is not good. Hill sorting helps to shorten the distance between adjacent elements. Therefore, the number of exchanges to be performed will be smaller.
Reference documents

[1]Parewa Labs Pvt. Ltd.Shell Sort Algorithm[EB/OL].https://www.programiz.com/dsa/shell-sort,2020-01-01.
[2]Baidu. Hill Sort[EB /OL].https://baike.baidu.com/item/Hill sorting, 2020-11-18.

Guess you like

Origin blog.csdn.net/zsx0728/article/details/115040643