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