Sorting algorithm of Java Hill sorting algorithm

Source: Internet
Author: User

Package Net.qh.test.sort;import java.util.arraylist;import Java.util.calendar;import java.util.list;/** * Created by Administrator on 2016/03/01.  */public class Shell {public int[] sort (int[] arr) {if (arr = = NULL | | arr.length <= 1) {return        Arr        } int length = Arr.length;            /** h Sort: Select a record every H record to be repeatedly divided into alternating records.            Describes the hill sort according to the sequence of selected H.            This method of generating H will select a well-realizable h-value sequence.         This h sequence is important and the outermost loop count.        */int h;        for (h = 1; h < length/9; h = 3 * H + 1);            for (; h > 0; h = h/3) {int i = h;                for (; i < length; i++) {int temp = Arr[i];                int k = i-h;                    while (k >= 0) {if (Arr[k] > Temp) {arr[k + h] = arr[k];                    }else{break;                    } Arr[k] = temp;                K = k-h;    }            }    } return arr;        } public static void Main (string[] args) {list<integer> intlist = new arraylist<integer> ();        for (int i = 0; i < 100000; i++) {intlist.add (i);        } int[] arr = new int[intlist.size ()];        for (int j = 0; J < Arr.length; J + +) {Arr[j] = Intlist.get (j);        } for (int temp:arr) {System.out.print (temp + ",");        } System.out.println ("");        Long start = Calendar.getinstance (). Gettimeinmillis ();        arr = new Shell (). sort (arr);        Long end = Calendar.getinstance (). Gettimeinmillis ();        System.out.println ("milliseconds required:" + (End-start));        for (int temp:arr) {System.out.print (temp + ","); }}/** 1.        The sorting algorithm is the hill sort algorithm.                2. Reason: He is a smart variant of the insertion sorting algorithm. The insertion order is slow because it calls only one item at a time as an ordered record.                If the record is far from the target location, we need to do it multiple times to get there.        We can consider, in the early stages of sorting, to allow records to jump larger distances.       3. Sorting ideas: Divide the records into several alternating groups and insert a sort on each group. 4. The H sequence is important.        5. The hill algorithm is a more complex sorting algorithm and relies on the H sequence.        6. Hill sort order: O (n^1.25), not a stable sorting algorithm, is much faster than bubbling and simple insertion sorting. 7. The ordering of objects is not sensitive.        Can be used in any case the sort, very good. 8. Real-Combat testing: 4-core CPU and 8G of memory, 100,000 integer elements of the array sort, reverse order requires 8 milliseconds, ordered: 5 milliseconds */}

  

Sorting algorithm of Java Hill sorting algorithm

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.