[4] algorithm path-shell interval of insert sorting and Sedgewick Interval

Source: Internet
Author: User

Question

The insert sorting method extracts a value from the front end of the unordered half and inserts an appropriate position in the front of the sorted half. The concept is simple but the speed is not high.

One of the basic principles for accelerating sorting:

Is to make the last sorting, try to use the results of the previous sorting to speed up the sorting, shell sort is based on this concept to improve the insert sort.

Solution

Shell sorting method was initially proposed by d.l shell in 1959. If there are n elements to be sorted, it takes an interval instead of all elements to be sorted at the same time.

 

Shell sorting algorithm-n/2 Interval

Shell first sets the interval to n/2, then jumps for insertion sorting, then sets the interval to N/4, jumps for sorting, and then sets the interval to N/8, N/16, the last sorting ends after the interval is 1.


Shell sorting algorithm-Sedgewick Interval

 

Setting the interval to n/2 was originally proposed by d.l shell. It is better to use this interval in textbooks. However, the key to shell sorting is the selection of the interval, for example, Sedgewick proves that the following interval can be used to increase the speed of shell sorting:


E. g for an integer array with a length of 10000,

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

8 iterations are required to use the swedge interval (swedge [0] is not used)

13 iterations are required at the interval of a common shell.

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


Later, it was proved that other selection methods can accelerate shell sorting. In addition, the concept of shell sorting can also be used to improve Bubble sorting.


Sourcecodes


Shell sorting between n/2 interval and Sedgewick Interval


int DLShellSort(int a[],int lens){for(int gap=lens/2;gap>0;gap/=2){InsertionSortWithGap(a,lens,gap);}return 0;}// 4*((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 sorting
// Insert the sorting using the specified interval int insertionsortwithgap (int A [], int lens, int gap) {int K, TMP; // control the 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 ;}




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.