Hark data structure and algorithm practice Hill sort

Source: Internet
Author: User

Algorithm description

The hill sort is an optimized version of the insert sort.

The worst time complexity for inserting a sort is O (N2), but if the array to be sorted is an almost ordered sequence, it reduces the effective time-to-reduce complexity.

The purpose of the hill sort is to sort the sequence of numbers by a increment (increment), eventually making the sequence almost orderly, and finally performing the insertion sort and counting the results.

By INCREMENT=N/2, that is, if the number is 9, the increment is 4,2,1.  If it is 20 numbers, the increment is 10,5,2,1. When increment is 1 o'clock, the order of the almost ordered sequence is inserted.

Complexity of Time

O (N2/3)

Complexity of space

O (1)

Code

is using the Java

/* * Hill sort */public class Shellsort {public static void main (string[] args) {int[] Arraydata = {5, 9, 6, 7, 4, 1, 2, 3, 8 }; Shellsortmethod (Arraydata); for (int integer:arraydata) {System.out.print (integer); System.out.print ("");}} public static void Shellsortmethod (int[] arraydata) {int I, j, temp = 0;int increment = arraydata.length;do {increment = i NCREMENT/2;  Increment for (i = increment; i < arraydata.length; i++) {if (Arraydata[i] > Arraydata[i-increment]) {   //determine if you want to insert sort temp = Arraydata[i];  The value to be inserted is stored in a temporary variable//What is actually done here is the insertion sort, which moves backwards in increments of step.   //temp > Arraydata[j] This is to note that only the number that is smaller than the value to be inserted is moved for (j = i-increment; J >= 0 && temp > arraydata[j]; j -= increment) {arraydata[j + increment] = Arraydata[j];} Arraydata[j + increment] = temp;}}} while (Increment > 0);}}

  

 

Results

Hark data structure and algorithm practice 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.