1 Overview
2 Sorting ideas
One day, Yichen held poker and played there by himself, and was just seen by the master.
First, it divides the larger data set into several groups (logically grouped), and then inserts and sorts each group separately. At this time, the amount of data that is inserted and sorted is relatively small (each group), and the efficiency of insertion Relatively high
It can be seen that he is a group with the subscripts separated by a distance of 4 points, that is to say, the subscripts with a difference of 4 are divided into a group. For example, in this example a [0] and a [4] are a group 1] and a [5] are a group ..., where the difference (distance) is called an increment
After each group is inserted and sorted, each group becomes ordered (the whole is not necessarily ordered)
At this point, the entire array becomes partly ordered (the degree of ordering may not be very high)
Then reduce the increment to half of the previous increment: 2, continue to divide the grouping, at this time, the number of elements in each grouping is more, but the part of the array becomes ordered, and the insertion sorting efficiency is also higher
Similarly, sort each group (insert sort) to make each group order separately
Finally, set the increment to half of the previous increment: 1, the entire array is divided into a group, at this time, the entire array is close to order, the insertion sort efficiency is high
Similarly, sort the only set of data, the sorting is completed
3 Time complexity
4 Hill sorting derivation
// Hill sorting derivation public static void main (String [] args) { int [] arr = {5,7,8,3,1,2,4,6 }; shellSort (arr); } // use step by step Method of derivation public static void shellSort ( int [] arr) { // Hill sort the first round of int temp; // Because of the first round of sorting, 8 data are divided into 4 groups // span is 4 for ( int i = 4; i <arr.length; i ++ ) { // traverse all elements in a variety (a total of 5 groups, 2 elements in each group) step size 5 for ( int j = i-4; j> = 0; j -= 4) { // If the current element is greater than the one after the step size, it means exchange if (arr [j]> arr [j + 4 ]) { temp = arr [j]; arr [j] = arr [j + 4 ] ; arr [j + 4] = temp; } } } System.out.println ( "The first sorting result is:" ); System.out.println (Arrays.toString (arr)); // Hill sorting 2 rounds // Because of the first round of sorting, 8 data are divided into 4/2 = 2 groups // span is 2 for ( int i = 2; i <arr.length; i ++) { // Traverse all elements in all (a total of 2 groups) Step 2 for ( int j = i-2; j> = 0; j-= 2 ) { // If the current element is greater than the step That element explains the exchange if (arr [j]> arr [j + 2 ]) { temp = arr [j]; arr [j] = arr [j + 2 ]; arr [j + 2] = temp; } } } System.out.println ( "The second sorting result is:" ); System.out.println (Arrays.toString (arr)); // Hill sorting round 3 //Because of the first round of sorting, the 10 data are divided into 2/2 = 1 groups for ( int i = 1; i <arr.length; i ++ ) { // Traverse all elements in a variety (a total of 5 groups, each group 2 elements) Step size 5 for ( int j = i-1; j> = 0; j-= 1 ) { // If the current element is greater than the element after the step size, then exchange if (arr [j]> arr [j + 1 ]) { temp = arr [j]; arr [j] = arr [j + 1 ]; arr [j + 1] = temp; } } } System.out.println ( "The third sorting result for:" ); System.out.println(Arrays.toString(arr)); }
Derivation results:
5 Exchange method to achieve Hill sorting (code is easy to understand and slow)
// ********************** Exchange Method ************************ ******** // According to the previous step-by-step analysis, use the loop to process public static void shellSort2 ( int [] arr) { int temp; for ( int gap = arr.length / 2; gap> 0; gap / = 2 ) { for ( int i = gap; i <arr.length; i ++ ) { // traverse all elements in a variety (a total of 5 groups, 2 elements in each group) step size 5 for ( int j = i- gap; j> = 0; j-= gap) { // If the current element is larger than the element after the step size, it means exchange if (arr [j]> arr [j + gap]) { temp = arr[j]; arr[j] = arr[j + gap]; arr[j + gap] = temp; } } } } }
6 Shift method to achieve Hill sorting (code is difficult to understand and fast)
// ********************** Shift method *********************** ************* public static void shellSort3 ( int [] arr) { // Incremental gap inserts and sorts the groups it is in directly for ( int gap = arr.length / 2; gap> 0; gap / = 2 ) { // From the gap element, insert and sort the groups one by one for ( int i = gap; i <arr.length; i ++ ) { int j = i; int temp = arr [j]; if (arr [j] <arr [j- gap]) { while(j-gap> = 0 && temp <arr [j- gap]) { // Move arr [j] = arr [j- gap]; j- = gap; } // When exiting this loop, give temp Find the insertion position arr [j] = temp; } } } }
7 Hill sort speed test
public static void main (String [] args) { // ********************* Hill array exchange method speed test ********* ********************** 888 int [] arr2 = new int [8000000 ]; for ( int i = 0; i <arr2.length; i ++ ) { arr2 [i] = ( int ) (Math.random () * 800000 ); } // Display the array before sorting // System.out.println ("Select sorting test array:" + Arrays.toString (arr2)); SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss" ); Date date1 = new Date (); String time1 = simpleDateFormat.format (date1); System.out.println ( "The time before sorting is:" + time1); // Hill sort exchange method shellSort2 (arr2); // Hill sort shift method shellSort3 (arr2); Date date2 = new Date (); String time2 = simpleDateFormat.format (date2); System.out.println ( "The time after sorting is:" + time2); }
Exchange method ----- speed test result:
80,000 data sorting results (exchange method) takes about 10 seconds:
800,000 data sorting results (exchange method) takes about countless seconds: (can't wait for results)
Shift method ----- speed test result:
80,000 data sorting results (shift method) takes less than 1 second:
800,000 data sorting results (shift method) takes less than 1 second:
The 8 million data sorting result (shift method) takes less than 3 seconds: