Java advanced sort of hill sort _java

Source: Internet
Author: User

Hill sort performed well for a medium-size array of up to thousands of data items, and hill sorting is not as fast as the fast sort and other time complexity O (N*LOGN) sorting algorithms, so sorting very large files is not the best choice, But Hill sort is a lot quicker than choosing sort and insert sort, and the time complexity of O (n²) is much faster, and it's very easy to implement, code short

Hill sort is also a sort of insertion, in the insert sort, if the smallest number is at the end, the number of copies is too many, and Hill solves the problem, and it is an n-increment sort, with the idea of inserting the elements in the sort by increasing the interval between them, and inserting the sort in these spaced elements, When these data items are queued, the hill sorting algorithm reduces the interval of data items and then sorts them. The spacing between data items is called increment when these sorts are performed, and is customarily represented by the letter H.

For an array that is about to be sorted by Hill, the start interval should be greater, and then the interval will not decrease until the interval becomes 1.

Interval sequence:

The quality of the numbers in the interval sequence is often considered important-except for 1 they do not have a convention number, and this constraint makes it more likely that the order of the previous row has been sorted, and that there is an absolute condition for the different interval sequences, Is that the gradual reduction of the interval finally must be equal to 1. So the last trip was a normal insertion sort.

The examples listed below are derived from the laws of H=h*3+1:

 package com.jll.sort; public class Shellsort {int[] arr;
  
  int size;
  Public Shellsort () {super ();
    public shellsort (int size) {this.size = size;
  arr = new Int[size];
    }/** * @param args */public static void main (string[] args) {shellsort ss = new Shellsort (10);
      for (int i=0;i<10;i++) {Ss.arr[i] = (int) (Math.random () *100) +1);
    System.out.print (ss.arr[i]+ "");
    } ss.shellsort ();
    System.out.println ();
    System.out.println ("after sort:");
    for (int i=0;i<10;i++) {System.out.print (ss.arr[i]+ "");
    } public void Shellsort () {int h = 1;
    while (H<=SIZE/3) {h = h*3+1;
        for (; h>0;h= (h-1)/3) {for (int i=h;i<size;i++) {int temp = Arr[i];
          int j = i;
            while (j>h-1&&arr[j-h]>temp) {arr[j]=arr[j-h];
          J-=h;
        } arr[j]=temp; }
      }
    }
  }

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.