First, Hill sort
(1) Introduction
The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959. Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates. (2) The basic idea takes an integer less than n D1 as the first increment, grouping all the records in the file. All records with a multiple of D1 are placed in the same group. First, the direct insertion sort is performed within each group, and then the second increment d2<d1 repeats the above groupings and sorts until the increment =1 (< ... <d2<d1) is taken, that is, all records are placed in the same group for direct insert sorting. The method is essentially a grouping insertion method that compares the number of distant distances (called increments), so that when a number moves across multiple elements, a comparison can eliminate multiple element exchanges. The algorithm first sorts the group of numbers by an increment d into groups, each group of records of the subscript difference D. Sort all the elements in each group, then use a smaller increment to do it, and then sort them in each group. When the increment is reduced to 1 o'clock, the entire number to be sorted is divided into a group, and the sort is completed. The average initial fetch sequence n is half the increment, and then halved each time (try not to be a multiplier of the previous increment, otherwise inefficient) until the increment is 1. Second, the implementation of the algorithm
Packagecn.edu.scau.mk;Importjava.util.Arrays;/** * * @authorMK*/ Public classShellsort {/*** Insert sort per increment *@paramData sequence *@paramDK Increment*/ Private Static voidShellinsert (int[] Data,intDK) { intJ; intTemp//Temporary Space//traversing the DK group sequence for(inti = DK; i < data.length; i++) { //comparison with pre-DK elements if(data[i-dk]>Data[i]) {Temp=data[i];//Temporary Save//until it's smaller than the current element . for(j = i-dk; J >=0&&data[j]>temp; j-=DK) {Data[j+dk]=Data[j]; } data[j+dk]=temp;//Backfill Data } } } /*** Hill Sort *@paramData Sequence*/ Public Static voidShellsort (int[] data) { //Incremental intDk=data.length/2; //The increment is eventually reduced to 1 while(dk>=1) {shellinsert (data, DK); DK/=2; } } Public Static voidMain (string[] args) {int[] data={2,3,1,3}; Shellsort (data); System.out.println (arrays.tostring (data)); }}
Third, the complexity of the algorithm
The best time is O (n), the worst Time O (NS), (1<s<2), average Time O (nlogn), instability, Hibbard increment dk=2t-k+1-1, (1<=k<=t<? LOG2 (n+1)), Hill sort time complexity is O (), Hill sort time complexity of the nether is nlog2n, there is no fast sorting algorithm fast O (NLOGN), so the medium size of the performance is good, the size of the very large data sorting is not the best choice.
Algorithm seven of the hill sort