JavaScript sort algorithm Hill sort of 2 Examples _ basics

Source: Internet
Author: User

The insertion sort is efficient in the operation of data that is almost already sorted out, that is, the efficiency of linear sorting can be achieved.
However, the insertion sort is generally inefficient because the insertion sort can only move data one bit at a time.
The hill sort was named after its designer, Donald Shell, which was unveiled in 1959. Some old editions of textbooks and reference manuals named the algorithm Shell-metzner, which included Marlene Metzner Norton's name, but according to Metzner, "I don't do anything for this algorithm, my name should not appear in the algorithm's name." ”



Hill sort basic idea: First take an integer D1 less than n as the first increment, the entire record of the file into D1 groups. All records with a multiple distance of D1 are placed in the same group. The direct insertion sort is done in each group first, then the second increment D2 < D1 repeats the above grouping and sorting until the increment dt=1 (dt < dt-l< ... < D2 < D1) is taken, that is, all records are placed in the same group for direct insertion sort.

The method is essentially a grouping insertion method.

Example 1:

Copy Code code as follows:

/**
* Hill sort, also called descending incremental sorting algorithm, is a more efficient version of the insertion sort. Hill sort is a non stable sort algorithm.
*
* Hill sort is based on the following two points of insertion of the nature of the proposed improvement methods:
*
* The insertion order is efficient in the operation of almost-ordered data, that is, the efficiency of a linear sort can be achieved
* But insertion sort is generally inefficient because the insertion sort can only move data one at a time
*
*/

function Shellsort (list) {
var gap = Math.floor (LIST.LENGTH/2);

while (Gap > 0) {

for (i = gap; i < list.length; i++) {
temp = List[i];

for (j = i; J >= Gap && List[j-gap] > Temp. J-= Gap) {
LIST[J] = List[j-gap];
}
LIST[J] = temp;
}

Gap = Math.floor (GAP/2);
}

return list;
};

Test
var arr = [2, 1, 3, 12, 5, 66, 23, 87, 15, 32];

Shellsort (arr);

Example 2:

Copy Code code as follows:

<script type= "Text/javascript" >
document.write ("----------Hill sort, insert sort upgrade, 1959 shell out------When the increment is correct, the time complexity is N 1.3 times-------");
document.write ("<br/><br/>")
var array = new Array (12, 25, 32, 16, 18, 27, 59, 69, 36);
function Shellsort (array) {
var j, I, V, h=1, s=3, k,n = Array.Length;
var result = "";
var count = 0;
while (H < n)
h=s*h+1;

while (H > 1) {
H= (h-1)/s;
for (k=0; kFor (i=k+h,j=i i<n; i+=h, j=i) {
V=array[i];
while (true)
if ((j-=h) >= 0 && array[j] > V)
ARRAY[J+H]=ARRAY[J];
Else
Break
Array[j+h]=v;

}
count++;
Result + = "<br/>" + Count + "all over the order of the results are:";
for (var n = 0; n < array.length; n++) {
result = + Array[n] + ",";
}
}
return result;
}
Shallsort (array);
document.write ("<br/><br/>");
</script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.