[Basic ideas]
Group records that originally had a large number of records. Cut into a number of sub-sequences, at this time each sub-sequence to sort the number of records is less, and then in these sub-sequences are directly inserted in the order, when the whole sequence is basically ordered. Then make a direct insert sort on the entire record.
The so-called basic order, is the small keyword basic in front, large basic in the back, medium-sized 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)
The shell sort is not a stable sorting algorithm.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Java sorting algorithm (iv): Shell sort