The birth of the hill sort is due to the fact that the insertion sort will encounter the problem of moving too many elements when dealing with large arrays. The idea of the hill sort is to divide a large array of "divide and conquer" into small arrays of gaps, such as arrays [1, 2, 3, 4, 5, 6, 7, 8], which, if divided by gap = Two, can be divided into [1, 3, 5, 7] and [2, 4, 6, 8] Two arrays (corresponding, such as Gap = 3, the array is divided into: [1, 4, 7], [2, 5, 8], [3, 6]) and then separate the divided array into the sorting, after each sub-array is sorted and then reduce the gap value repeat the previous steps until Gap = 1 , which is the insertion sort of the entire array, at which point the array is almost ready to order, so the elements that need to be moved are small and small, solving the problem of more movement of the insertion sort in handling large arrays.
For specific instances, refer to insert sort.
Hill sort is an improved version of the insertion sort, which helps to improve efficiency when the volume of data is large, and it is advisable to use the insertion sort directly when the data volume is small.
Implementation code:
Public<textendsComparable<t>>voidSort (t[] array,Booleanascend) { intLength =Array.Length; intGap = 1; while(Gap < LENGTH/3) {Gap= Gap * 3 + 1; } while(Gap >= 1) { for(inti = gap; i < length; i++) {T next=Array[i]; intj =i; while(J >=Gap) { intCompare = Array[j-Gap].compareto (next); if(compare = = 0 | | Compare < 0 = =ascend) { Break; } Array[j]= Array[j-Gap]; J-=Gap; } if(J! =i) {array[j]=Next; }} Gap/= 3; } }
Example 2:
PackageOrg.rut.util.algorithm.support;ImportOrg.rut.util.algorithm.SortUtil; Public classShellsortImplementssortutil.sort{/*(non-javadoc) * @see Org.rut.util.algorithm.sortutil.sort#sort (int[])*/ Public voidSortint[] data) { for(inti=data.length/2;i>2;i/=2){ for(intj=0;j<i;j++) {insertsort (data,j,i); }} insertsort (data,0,1); } /** * @paramData *@paramJ *@paramI*/ Private voidInsertsort (int[] Data,intStartintInc) { inttemp; for(inti=start+inc;i<data.length;i+=Inc) { for(intJ=i (j>=inc) && (Data[j]<data[j-inc]) j-=Inc) {Sortutil.swap (data,j,j-Inc); } } }}
Sortutil: PackageOrg.rut.util.algorithm;ImportOrg.rut.util.algorithm.support.BubbleSort;ImportOrg.rut.util.algorithm.support.HeapSort;ImportOrg.rut.util.algorithm.support.ImprovedMergeSort;ImportOrg.rut.util.algorithm.support.ImprovedQuickSort;ImportOrg.rut.util.algorithm.support.InsertSort;ImportOrg.rut.util.algorithm.support.MergeSort;ImportOrg.rut.util.algorithm.support.QuickSort;ImportOrg.rut.util.algorithm.support.SelectionSort;ImportOrg.rut.util.algorithm.support.ShellSort; Public classSortutil { Public Final Static intINSERT = 1; Public Final Static intBUBBLE = 2; Public Final Static intSELECTION = 3; Public Final Static intSHELL = 4; Public Final Static intQUICK = 5; Public Final Static intImproved_quick = 6; Public Final Static intMERGE = 7; Public Final Static intImproved_merge = 8; Public Final Static intHEAP = 9; Public Static voidSortint[] data) {Sort (data, improved_quick); } Private StaticString[] Name={ "Insert", "bubble", "selection", "shell", "quick", "Improved_quick", "merge", "Improved_merge", "Heap" }; Private StaticSort[] Impl=Newsort[]{NewInsertsort (),NewBubblesort (),NewSelectionsort (),NewShellsort (),NewQuickSort (),NewImprovedquicksort (),Newmergesort (),NewImprovedmergesort (),Newheapsort ()}; Public StaticString toString (intalgorithm) { returnName[algorithm-1]; } Public Static voidSortint[] Data,intalgorithm) {Impl[algorithm-1].sort (data); } Public Static InterfaceSort { Public voidSortint[] data); } Public Static voidSwapint[] Data,intIintj) {inttemp = data<i>; Data<i> =Data[j]; DATA[J]=temp; }}
Java algorithm-Hill sort