Java Hill Sort

Source: Internet
Author: User

Efficiency: O (N*LOGN)


Package Sort;import Utils. util;/** * Hill Sort * In the interval of H, compare. According to a certain formula, we first find the maximum interval h * When the H value is large, the number of elements that need to be moved is less, but the distance traveled long. Interior with an h step element for insertion sort * When H is reduced, the number of elements to be moved in each order is increased, but at this point it is close to their final position after sorting, which is more efficient for insertion sorting * @author Stone * @date 2015-7-22 pm 4:39:41 */p Ublic class Shellsort {public static void main (string[] args) {//int[] ary = Util.generateintarray (ten); int[] ary = {7, 1, 3, 6, 4, 5, 2, 0}; Util.printarray (ary); sort (ary); Util.printarray (ary);} static void sort (int[] ary) {int h = 1;while (Ary.length/3 > h) {h = 3 * H + 1;} while (H > 0) {int J, temp;for (int i = h; i < ary.length; i++) {temp = Ary[i];//j = I;//while (J > H-1 &AMP;&A mp ARY[J-H] >= temp) {//ary[j] = ary[j-h];//j-= h;//}for (j = i; j > h-1 && ary[j-h] >= temp; J-= h) {Ary[j] = ary[j-h];} ARY[J] = temp;//system.out.println ("mobile---------");//util.printarray (ary);} h = (h-1)/3;}} /* * [7, 1, 3, 6, 4, 5, 2, 0] * h=4 * temp = ary[4], i = 4 * 1: [0] ratio [4] [0]>[4] [4]=[0] [0] = temp * 2:temp =ARY[5] [1] than [5] unchanged * 3:TEMP=ARY[6] [2] than [6] change [6]=[2] [2]=temp * 4:temp=ary[7] [3] than [7] change [7]=[3] [3]=temp * Now: * [4, 1, 2, 0, 7, 5, 3, 6] * * [1, 4, 2, 0, 7, 5, 3, 6] 1 shift times * [1, 2, 4, 0, 7, 5, 3, 6] 1 * [0, 1, 2 , 4, 7, 5, 3, 6] 3 * [0, 1, 2, 4, 7, 5, 3, 6] * [0, 1, 2, 4, 5, 7, 3, 6] 1 * [0, 1, 2, 3, 4, 5, 7, 6] 3 * [0, 1, 2, 3, 4, 5, 6, 7] 1 * * In the interval of H, according to a certain formula, first find the maximum interval h * while (h>0) {* for (i=h;i<len;i++) {* for (loop comparison [h] and [i-h] values, and move BIT) *} * H = ... The last round is h=1. At this time i=n I ratio (i-1), (i-1) than ((i-1)-1) from backward forward two compared to *} */}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.