Algorithm (fourth edition) Java implementation of learning Notes Hill sort

Source: Internet
Author: User

Hill sort idea: the elements that make an arbitrary interval of h in an array are ordered. The hill sort is an optimization of the insertion sort, first sorting the array locally, and then sorting the partially ordered arrays using the insert sort.

The code is as follows:

/** * * @author seabear * */public class Shellsort {public static void sort (comparable[] a) {int N = A.length;int h = 1;wh Ile (H < N/2) {h = 4 * H + 1;} while (H >= 1) {for (int i = h, i < N; i++) {for (int j = i, J >= H && less (A[j],a[j-h]), J-= h) {int N = J- H System.out.println (j + ":" + a[j] + "," + N + ":" + a[j-h]); Exch (a,j,j-h); show (A);}} h = H/4;}} public static Boolean less (comparable v,comparable W) {return V.compareto (W) < 0;} public static void Exch (comparable[] A,int I,int j) {Comparable temp = a[i];a[i] = a[j];a[j] = temp;} public static Boolean Issort (Comparable[] a) {for (int i = 1; i < A.length, i++) {if (less (a[i],a[i-1])) {return false;}} return true;} public static void Show (comparable[] a) {for (int i = 0; i < a.length; i++) {System.out.print (A[i] + "");} System.out.println ();} public static void Main (string[] args) {integer[] a = new Integer[10];for (int i = 0; i <; i++) {a[i] = (int) (Math.rand Om () * 10 + 1); System.out.print (A[i] + "");} System.out.prinTLN (); sort (a); show (A);}} 


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

Algorithm (fourth edition) Java implementation of learning Notes 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.