Algorithm Description: The hill sort is a fast sorting algorithm based on the insertion sort, which is more suitable for the sorting of large-scale random ordinal groups than the traditional adjacent inserts. As with the insertion algorithm, we can also optimize the insertion and movement process to further enhance the efficiency of the algorithm.
Algorithm diagram:
The essence of the hill sorting algorithm is to first change a large random ordinal group into several small ordered arrays, and then gradually adjust the array length. The final step is still to make a traditional insertion sort, except that the elements that need to be moved are not too far from the current distance after the previous adjustment.
The following is the dynamics of the Hill sorting algorithm:
Java code example:
Packagealgorithms.sorting;Importalgorithms.common.Arrays;ImportAlgorithms.common.ArraysGenerator;Importalgorithms. sortable;Importjava.io.IOException;/*** Created by Learnhow on 2018/8/13.*/ Public classShellextendsArrays<integer>ImplementsSortable<integer>{@Override Public voidsort (integer[] array) {intH = 1; while(H < ARRAY.LENGTH/3) {h= 3 * H + 1; } while(H >= 1) { for(inti = h; i < Array.Length; ++i) { for(intj = i; J >= H; J-=h) {if(Array[j] < array[j-h]) {Exchange (array, J, J-h); } }} H= H/3; } } Public Static voidMain (String arg[])throwsIOException {integer[] arr= Arraysgenerator.generatedesc (1000, 5000); Shell Shell=NewShell (); Shell.sort (arr); System.out.println (java.util.Arrays.toString (arr)); System.out.println (arr, Arraysgenerator.issort"ASC")); }}
Qt/c++ code Example:
voidShell::sort (int* Arr,intLen) { inth =1; while(H < len/3) {h=3* H +1; } while(H >=1) { for(inti = h; i < Len; ++i) { for(intj = i; J >= H; J-=h) {if(Arr[j] < arr[j-h]) {intTMP =Arr[j]; ARR[J]= Arr[j-h]; Arr[j-H] =tmp; } }} H= h/3; }}
Algorithm improvements: In fact, the main problem with traditional insertion sorting and hill sorting affecting performance is that for sorting purposes, an element may need to be exchanged multiple times (3 assignment per swap action). So the key to improvement is how to reduce the element exchange or reduce the assignment operation. This, of course, increases the difficulty of the algorithm.
Java code example:
Packagealgorithms.sorting;Importalgorithms. sortable;ImportAlgorithms.common.ArraysGenerator;Importjava.util.Arrays;/*** Created by Learnhow on 2018/8/16.*/ Public classShelltrImplementsSortable<integer>{@Override Public voidsort (integer[] array) {intH = 1; while(H < ARRAY.LENGTH/3) {h= 3 * H + 1; } while(H > 0) { for(inti = h; i < Array.Length; i++) { inttemp =Array[i]; intj =i; while(J >= H && array[j-h] >temp) {Array[j]= Array[j-h]; J-=h; } Array[j]=temp; } h= H/3; } } Public Static voidMain (string[] args) {integer[] arr= Arraysgenerator.generatedesc (100, 100); Shelltr shelltr=Newshelltr (); Shelltr.sort (arr); System.out.println (arrays.tostring (arr)); System.out.println (arr, Arraysgenerator.issort"ASC")); }}
Qt/c++ code example (slightly)
RELATED links:
Algorithms for Java
Algorithms for Qt
Little Orange Book Reading Guide (iv)--hill sort and improved algorithm