java-Hill Sort

Source: Internet
Author: User

Hill sort is also called grouping insert sort, narrow incremental sort.

It works by comparing the elements that are spaced apart; the distance used for each comparison decreases as the algorithm progresses until the last comparison of adjacent elements is compared.

Simply put, the entire sequence is divided into several sub-sequences (consisting of elements that are separated by an increment), and the insertion sort is performed separately. Then, in order to reduce the increment and then to sort, the whole sequence of elements in the basic order, and then the entire element on the line once inserted sort (because this time the whole sequence is basically ordered, with the insertion sort efficiency is higher, sub-sequence is also the same reason).


public static void Shellsort (int[] a) {if (a==null) {return;} int n = a.length;//determines the increment sequence: N/2,2/4,..., 1for (int gap=n/2; gap>0; gap/=2) {for (int i=0; i<gap; i++) {//Insert sort for each sub-sequence for ( int j=i+gap; j<n; J+=GAP) {int temp = a[j];////Note the condition, k>=0 and A[k]>temp, if written as k>0 then the first element is not in the sort 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 sequencing is better than O (n^2), as it is exchanged for some distant elements in a round order.

Although the ordering of each subsequence is an insertion sort, and the insertion sort is secure, the same size elements may move in the respective insertion sort during the insertion ordering of different sub-sequences, causing the overall stability to be broken, so the hill sort is also unstable.

java-Hill 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.