PackageKpp.sort;/*** Hill Sort * 1. Set the step d, dividing every D element into a group, inserting a sort within the group; * 2.d/=2, repeat 1 steps until the step is 1 * We know once the insertion sort is stable, but during different insertion sorts, the same elements may move in the respective insert sort, Finally, the stability will be disrupted, so the hill sort is unstable. The time performance of the Hill sort is better than the direct insertion sort, for the following reasons: (1) Fewer comparisons and moves are required when the initial state of the file is basically ordered. (2) When the n value is small, the difference between N and N2 is also small, that is, the best time to insert the order of the complexity O (n) and the worst time complexity 0 (n2) difference is not small. (3) In the beginning of the hill, the increment is larger, more groups, the number of records per group is small, so the direct insertion within the group is faster, the increment di gradually reduced, the number of groups gradually decreased, and the number of records of the group gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the So the new trip sort process is also faster. Therefore, the efficiency of hill sort is better than direct interpolation. The average time complexity for hill sorting is O (Nlogn). * @authorKPP **/ Public classShellsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intArray[] = {49,38,65,97,176,213,227,49,78,34,12,164,11,18,1}; Shellsort (array); for(intK:array) {System.out.println (k); } } Private Static intShellsort (intdata[]) { intLen =data.length; inttemp = 0; for(intincrement = LEN/2; Increment > 0; Increment/= 2) { intj = 0; for(inti = increment; i < Len; i++) {Temp=Data[i]; for(j = i-increment; J >= 0; J-=increment) { if(Temp <Data[j]) {Data[j+increment] =Data[j]; }Else{ Break; }} data[j+increment] =temp; } } return0; }}
Hill sort Java Implementation