[4] Algorithm path-insert sort shell interval and sedgewick interval

Source: Internet
Author: User

Topic

The Insert Sort method takes a value out of the back end of the unordered half. Inserts the appropriate position in the first half of the sort. The concept is simple but the speed is unpleasant.

sort of to speed up one of the basic principles among :

is to let the next time the sort proceed , try to use the results from the previous sort , to speed up sorting. , Shell The sorting method is based on this concept to improve the insertion sorting method .

Solution

The shell sort method was originally proposed by the D.L Shell in 1959, and if the elements to be sorted have n, then each time the insertion sort is not all elements are taken at the same time, instead, the interval is applied.

Shell Sorting Algorithms –N/2 interval

Shell first set the interval to N/2 , then jump to the insertion sort, then the interval N/4 . Jump for the sort action, then the interval is set to n/8,n/16, until the interval is 1 after the most The last sort terminates.


Shell Sorting Algorithms –sedgewick interval

Setting the interval to N/2 is what the D.L shell originally suggested, and it is better to use this interval in textbooks, but the key to the shell ordering method is the selection of the interval. For example, Sedgewick proves that the use of the following interval can speed up the shell ordering method:


E.g for an integer array of length 10000,

swedge[0]=10000, swedge[1]=2537, swedge[2]=653, swedge[4]=48,swedge[5]=15 ... Swedge[8]=1

Swedge interval needs to be iterated 8 times (swedge[0] not used )

And you need to iterate 13 times with the normal shell interval.

Comsh [0]=10000, Comsh [1]=5000, comshe[2]=2500, comsh[4]=625,..... comshell[8]=39, comshell[13]=1


It was later proved that other interval-selection methods could speed up the order of the shell, and that the concept of shell sequencing could be used to improve the bubble sorting method.


Sourcecodes


N/2 interval vs. Sedgewick interval Shell Ordering


int dlshellsort (int a[],int lens) {for (int gap=lens/2;gap>0;gap/=2) {insertionsortwithgap (A,LENS,GAP);} return 0;} ((2^j) ^2) +3* (2^j) +1<=n//j= log (((( -3+sqrt (16*lens-7.0))/8)/log (2.0) int sedgewickshellsort (int a[],int lens) {int sdwindex= (int) log ((( -3+SQRT (16*lens-7.0)/8))/log (2.0); int sdwpr= (int) POW (2, (double) sdwindex); int sdwpr2= Sdwpr/2;while (true) {int sdwgap=4*sdwpr2*sdwpr2+3*sdwpr2+1;insertionsortwithgap (A,LENS,SDWGAP); Sdwpr2/=2;if ( sdwpr2<=1) break;} return 0;}


See [3] Algorithm path-insert sort
Insert sort int insertionsortwithgap (int a[],int lens,int gap) with specified interval {int k,tmp;//control insert layer for (int m=0;m<gap;m++) {for (int i=) GAP+M;I<LENS;I+=GAP) {int j=i-gap;tmp=a[i];for (K=J;K>=0;K-=GAP) {if (tmp<a[k]) A[k+gap]=a[k];else break;} if (i!= (K+GAP)) a[k+gap]=tmp;}} return 0;}

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdml2axr1zq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">



[4] Algorithm path-insert sort shell interval and sedgewick interval

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.