Advanced sorting algorithm--Hill sort (interval 1 o'clock is insert sort)

Source: Internet
Author: User

The Hill sort is an upgraded version of the Insert sort, which makes a great improvement on the basis of the insertion sort.

    functionCArray (numelements) { This. DataStore = [];  This. numelements =numelements;  This. Prints =prints;  This. SetData =SetData;  This. gaps = [1750, 701, 301, 132, 57, 23, 10, 4, 1 ];  This. Swap =swap;  This. Shellsort =Shellsort;  This. Shellsort1 =Shellsort1; }    //static interval sequence, Ciura sequence, the better interval sequence presented in the paper in 2001    functionSetData () { for(vari = 0; I < This. numelements; ++i) { This. datastore[i] =Math.floor (Math.random ()* ( This. numelements + 1)); }    }    functionswap (arr, index1, index2) {vartemp =ARR[INDEX1]; ARR[INDEX1]=Arr[index2]; ARR[INDEX2]=temp; }    functionprints () { for(vari = 0; I < This. datastore.length; ++i) {document.write ( This. Datastore[i] + ""); if(i > 0 & i% 10 = = 0) {document.write ("<br/>"); }        }    }    functionShellsort () { for(varg = 0; G < This. gaps.length; ++g) {varh = This. Gaps[g];  for(vari = h; I < This. datastore.length; ++i) {vartemp = This. Datastore[i];  for(varj = i; J >=h&& This. datastore[j-h] > This. Datastore[j]; J-=h) { This. datastore[j] = This. DATASTORE[J-h]; }                 This. datastore[j] =temp; }        }    }    functionShellsort1 () {varN = This. Datastore.length; varH = 1;  while(H < N/3) {//dynamically generating interval sequencesH = 3 * H + 1; }         while(H >= 1) {             for(vari = h; i < N; i++) {                vartemp = This. Datastore[i];  for(varj = i; J >=h&& This. Datastore[j] < This. Datastore[j-h]; J-=h) { This. datastore[j] = This. DATASTORE[J-h]; }                 This. datastore[j] =temp; } h= (h-1)/3; }    }    varNumelements = 10000; varNums =NewCArray (numelements);    Nums.setdata (); varStart =NewDate (). GetTime ();    Nums.shellsort (); varStop =NewDate (). GetTime (); varElapsed = stop-start; document.write ("The hard coded interval sequence of the hill sort consumes the time:" + elapsed + "milliseconds. "); document.write ("<br/>"); varStart =NewDate (). GetTime ();    Nums.shellsort1 (); varStop =NewDate (). GetTime (); varElapsed = stop-start; document.write ("The hard coded interval sequence of the hill sort consumes the time:" + elapsed + "milliseconds. ");

Advanced sorting algorithm--Hill sort (interval 1 o'clock is insert sort)

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.