Java explanation-shell sorting

Source: Internet
Author: User

I recently looked for a job. I took a pen test to get the Sorting Algorithm and reviewed the hill sorting. I didn't even understand it when I checked it all over again !!! ⊙ B Khan, So I sorted out the problems I encountered when I checked the code for the first time. In case I forgot again, I must not forget it !!

 

Hill sorting (downgrading incremental method) is a sort of insertion class, which divides the entire unordered column into several small subsequences for insertion sorting respectively. The hill sorting is not stable. The extra space of O (1) is the time complexity of O (N * (logn) ^ 2 ). In the worst case, the execution efficiency is similar to the average execution efficiency.

H = H * 3 + 1

The reason why Hill sorting is much faster than insert sorting: When H is large, the number of elements in each sort of data items is small, but the moving distance is very long, which is very efficient; when the H value is reduced by an hour, the number of elements in each sort increases, but the data items are close to the position after their final sorting, and insertion sorting can be more effective.

Directly Add code

 

Public class shellsort {static void sort (INT [] array) {int out, in, TMP; int Len = array. length; int H = 1; while (H <Len/3) // calculate the maximum H = H * 3 + 1; while (h> 0) {// can I continue to narrow down the interval h to split the data column?/** out why does it start with H? The first subsequence you split should be such a sequence, 0, h, 2 h, 3 h ,... * The while loop of insertion sorting starts from 1. Because the first number is always ordered and no comparison is required, you need to know the insertion sorting algorithm. Therefore, the comparison is from the second data line, it is the H subscript of the array. Why is * out determined by * Out? <Len? * Control the array subscript. The following example describes ** The following example is used to explain * assume that there is an array of 10 data items, and the array subscript ranges from 0 ~ 9 indicates the sub-sequence condition when H = 4. The following mark indicates * (0 4 8) (1 5 9) (2 6) (3 7) * I understood this for the first time. I really sorted each group separately (of course, this can also be done, but the subscript is not easy to control), but this is a wrong understanding of the following code. * The correct process is as follows: the outer for loop sorts the first two data items in each group each time, then the first three data items, and then the first four data items... this is related to the number of sub-sequences * the sorting process is performed only in the square brackets * When out = 4 ([0 4] 8) * When out = 5 ([1 5] 9) * When out = 6 ([2 6]) * When out = 7 ([3 7]) * When out = 8 ([0 4 8]) * When out = 9 ([1 5 9]) * H = 4, the execution is complete, then H = (h-1)/3 = 1 start the new for loop * H = 1 execution process is the same as H = 4, however, the subsequence is the original sequence, which is transformed into a simple insertion sorting. This is the basic order of the array, and the number of moving data items will be greatly reduced **/For (out = h; out <Len; out ++) {// determine the second data item of insertion sorting in each group through the outer layer. // the following code is the insertion Sorting Algorithm TMP = array [out]; In = out; /** compare the writing of the insert sort while loop. The while loop here is related to H, so the judgment is related to H, including the in-= H Statement * While (in> 0 & array [in-1]> TMP) {* array [in] = array [in-1]; * In --; *} * array [in] = TMP; **/while (in> h-1 & array [in-H]> = TMP) {array [in] = array [in-H]; In-= H;} array [in] = TMP; // For (INT I = 0; I <Len; I ++) // system. out. print (array [I] + ""); // system. out. println () ;}// reduce the interval H = (h-1)/3 ;}}}

 

 

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.