Hill sorting of javascript algorithm sorting


Event address: CSDN 21-day learning challenge

foreword

Many people have heard of the classic sorting algorithm, and many people may have used it, but there are also many people who have never heard of it. why? Now that we have more and more frameworks and dependent packages, we will be able to use the actual scene of sorting and encapsulate it into a function as a business. Therefore, some people only know the function but not its operating logic.

Based on the above, in order to better understand the operation logic of the function, I sorted out some operation rules of the basic sorting method, as well as some personal understanding, hoping to give you some help.

This article will describe Hill sorting. Simply speaking, Hill sorting is to split the array and sort it separately;

Hill sort

Hill sort is a quick sort algorithm based on insertion sort, also known as shrinking incremental sort. Simple insertion sort is slow for large out-of-order arrays because elements can only be moved from one end of the array to the other bit by bit. Hillsort simply improved insertion sort for speed, and it was one of the first algorithms to break O(n2).

Implementation principle of Hill sorting

  • Group the array according to a certain increment, and use the direct insertion sorting algorithm to sort each group. The spacing suggested by Hill is N/2, and each sorting is divided into two halves. That is to say, for an array with N=10, the increment The spacer sequence is 5, 2, 1;
  • Then shrink the increment and continue grouping and sorting; as the increment gradually decreases, each group contains more and more keywords. When the increment decreases to 1, the entire array is divided into one group, and sorted again, the entire array can be completed. sort;

image.png

the code


function shellSort(array) {
    
    
  //每次向下得到一半的增量
  for (let gap = Math.floor(array.length / 2); gap > 0; gap = Math.floor(gap / 2)) {
    
    
    //每个分组进行插入排序
    for (let i = gap; i < array.length; i++) {
    
    
      let j = i;
      let tmp = array[j];
      // 如果同一组中 前数大于后数,则交换他们
      if (array[j] < array[j - gap]) {
    
    
        while (j - gap >= 0 && array[j - gap] > tmp) {
    
    
          array[j] = array[j - gap];
          j = j - gap;
        }
        array[j] = tmp;
      }
    }
  }
  console.log('shellSort result', array);
}
shellSort([2, 4, 7, 9, 0, 1, 2, 3, 5, 6, 8]);

return value
image.png

the complexity

time complexity:

  • Best case: the sequence is arranged in positive order, in this case, the comparison operation needs to be performed (n-1) times. The shift-back assignment operation is 0 times. That is O(n)
  • Worst case: O(nlog2n).
  • Asymptotic time complexity (average time complexity): O(nlog2n)

Space complexity: O(2n)

message

Hill sort is the most unstable overall, but in the best case, it is a very good sorting method! The pros and cons are equally obvious. Use with caution!

Guess you like

Origin blog.csdn.net/Long861774/article/details/126406164