Java-hill sorting

Source: Internet
Author: User

Java-hill sorting

Hill sorting is also called grouping insertion sorting and narrowing down incremental sorting.

It works by comparing elements with a certain interval. The distance used for comparison decreases with the algorithm until only the last comparison of adjacent elements is compared.

Simply put, the whole sequence is first divided into several sub-sequences (composed of elements separated by an increment) for insertion sorting. Then, the incremental data is reduced in sequence and then sorted. When the elements in the entire sequence are basically ordered, the data is inserted and sorted once again (because the whole sequence is basically ordered at this time, insert sorting is more efficient. This is also true when sorting sub-sequences ).

 

 

Public static void shellSort (int [] a) {if (a = null) {return;} int n =. length; // determine the incremental sequence as: n/4 ,..., 1for (int gap = n/2; gap> 0; gap/= 2) {for (int I = 0; I

 
  
= 0 and a [k]> temp. If k> 0 is written, the first element is not sorted. for (int k = j-gap; k> = 0 & a [k]> temp; k-= gap) {a [k + gap] = a [k]; a [k] = temp ;}}}}}

 

 

The time complexity of hill sorting is better than O (N ^ 2), because it exchanges some elements that are far away from each other during a round of sorting.

Although the sorting of each sub-sequence is insert sorting and insertion sorting is stable, the elements of the same size may move in the insertion sorting of different sub-sequences, as a result, the overall stability is broken, so the hill sorting is also unstable.

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.