[Basic idea]
The records that originally have a large number of records are grouped into several sub-sequences, at which time the number of records to be sorted in each subsequence is less, and then the sequence of direct insertion in these subsequence sequence, when the entire series is basically ordered, and then a direct insertion of the whole record to sort.
The so-called basic order, is the small keyword basic in front, large basic in the back, the basic in the middle, like {2, 1, 3, 6, 4, 7, 5, 8, 9} This can be called basic order.
[Java Implementation]
public class Shellsort {public static void main (string[] args) {int[] arr = {6, 5, 3, 1, 8, 7, 2, 4}; System.out.println ("Before sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");} Hill sort Shellsort (arr); System.out.println (); System.out.println ("After sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");}} /** * Hill sort */private static void Shellsort (int[] arr) {int j;for (int gap = ARR.LENGTH/2; gap > 0; gap = gap/2) {fo R (int i = gap; i < arr.length; i++) {int tmp = arr[i];for (j = i; J >= Gap && tmp < ARR[J-GAP]; j = J -gap) {Arr[j] = Arr[j-gap];} ARR[J] = tmp;}}}}
[Algorithm description]
Hill sort time Complexity:O (NLOGN)
Hill sort is not a stable sorting algorithm.
Java sorting algorithm (iv): Hill sort