Home > Web Front-end > JS Tutorial > Detailed explanation of Hill sorting in JavaScript

Detailed explanation of Hill sorting in JavaScript

韦小宝
Release: 2018-03-14 14:08:45
Original
1868 people have browsed it

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;}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template