This article talks about Hill sorting in JavaScript. If you don’t know about Hill sorting in JavaScript or are interested in Hill sorting in JavaScript, let’s take a look at this article. Okay. Stop talking nonsense and get to the point!
Hill sort in JavaScript
Hill sort is a more efficient implementation of insertion sort. It differs from insertion sort in that it compares elements that are farther away first. The core of Hill sorting lies in the setting of interval sequence. The interval sequence can be set in advance or dynamically defined. The algorithm for dynamically defining interval sequences was proposed by Robert Sedgewick, co-author of "Algorithm (4th Edition)". Here, I used this method.
JavaScript code implementation
function shellSort(arr) { var len = arr.length, temp, gap = 1; while(gap < len/3) { //动态定义间隔序列 gap =gap*3+1; } for (gap; gap > 0; gap = Math.floor(gap/3)) { for (var i = gap; i < len; i++) { temp = arr[i]; for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) { arr[j+gap] = arr[j]; } arr[j+gap] = temp; } } return arr;}
Above That’s all the content of this article. If you don’t know much about it, you can easily master both sides by yourself!
Related recommendations:
How to implement JS Hill sorting and quick sorting
The above is the detailed content of Detailed explanation of Hill sorting in JavaScript. For more information, please follow other related articles on the PHP Chinese website!