Hill sorting is optimized based on insert sorting, mainly to reduce the number of inserts. The data to be sorted in the example {3, 32, 1, 55, 0, 3, 9}
The sorting rules are sorted from left to right, from small to large. The correct sorting result is: {0, 1, 3, 3, 9, 32, 55}
I. Hill sorting source code
Public class ShellSort {public void shellSort (int array []) {if (array = null) {throw new NullPointerException ();} // 1 select the step for (int gap = array. length/2; gap> 0; gap/= 2) {// 2 traverse all groups for (int I = 0; I <gap; I ++) {// 3 traverse all elements in the group for (int j = I + gap; j <array. length; j + = gap) {// compare the size of elements in the 4 groups if (array [j-gap]> array [j]) {// 5 insert sorting int insertValue = array [j]; int k = j-gap; while (k> = 0 & array [k]> insertValue) {array [k + gap] = array [k]; k-= gap;} array [k + gap] = insertValue ;}}}}} public static void main (String [] args) {int [] array = {3, 32, 1, 55, 0, 3, 9}; ShellSort shellSort = new ShellSort (); shellSort. shellSort (array); System. out. println (Arrays. toString (array ));}}
Ii. Gradual explanation of hill sorting
1. Select step size
for (int gap = array.length / 2; gap > 0; gap/=2) {}
What is step size? Compare several elements, if the step size rule is gap = array. length/2, gap/= 2; divide each time by 2. In this example, the step size is: array length = 7, and all step sizes are 3, respectively, 1gap = 7/2 = 3gap = 3/2 = 1
How do I select the optimal step size? Each time we use this method, we divide it by 2. for the research on the Optimal Step Size of hill sorting, you can refer to the Encyclopedia "Hill sorting-Wikipedia" and "Hill sorting-Baidu Encyclopedia".
2. traverse all Groups
for (int i = 0; i < gap; i++) {}
The current operation is nested in 1, that is, it is executed once when the step is 3 and once when the step is 1. The calculated step size (gap) values are 3 and 1 respectively.
Gap value |
I value |
3 |
0, 1, 2 |
1 |
1 |
3. traverse all elements in the group
for (int j = i + gap; j < array.length; j += gap) {}
Gap value |
I value |
J value |
3 |
0 |
3, 6 |
3 |
1 |
4 |
3 |
2 |
5 |
1 |
1 |
1, 2, 3, 4, 5, 6 |
4. group element comparison size
if (array[j - gap] > array[j]) {}
Correspondence between array indexes and values
Array Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
Corresponding value |
3 |
32 |
1 |
55 |
0 |
3 |
9 |
4.1 step 3
Gap value |
I value |
J value |
Comparison |
Judgment |
3 |
0 |
3 |
A [0]> a [3] |
3> 55 |
3 |
0 |
6 |
A [3]> a [6] |
55> 9 |
3 |
1 |
4 |
A [1]> a [4] |
32> 0 |
3 |
2 |
5 |
A [2]> a [5] |
1> 3 |
The table above {3, 32, 1, 55, 0, 3, 9} can also be written. The step size is 3: 3 and 55. Comparison between 55 and 9. Comparison between 32 and 0. Comparison between 1 and 3.
When the Step 4.2 is 1
Gap value |
I value |
J value |
Comparison |
Judgment |
1 |
1 |
1 |
A [0]> a [1] |
|
1 |
1 |
2 |
A [1]> a [2] |
|
1 |
1 |
3 |
A [2]> a [3] |
|
1 |
1 |
4 |
A [3]> a [4] |
|
1 |
1 |
5 |
A [4]> a [5] |
|
1 |
1 |
6 |
A [5]> a [6] |
|
5. Insert sorting
int insertValue = array[j]; int k = j - gap; while (k >= 0 && array[k] > insertValue) { array[k + gap] = array[k]; k -= gap; } array[k + gap] = insertValue;
For more information, see Java insertion sorting.
3. Before the complete execution process is processed: {3, 32, 1, 55, 0, 3, 9} gap = 3, I = 0, j = 6, a [0]> a [6], insert the sorting result as follows: {3, 32, 1, 9, 0, 3, 55} after processing}
Gap = 3, I = 1, j = 4, a [1]> a [4], insert the sorting result as follows: {3, 0, 1, 9, 32, 3, 55}
Gap = 1, I = 0, j = 1, a [0]> a [1], that is, 3> 0. The insert sorting result is as follows: {0, 3, 1, 9, 32, 3, 55}
Gap = 1, I = 1, j = 2, a [1]> a [2], that is, 3> 1. Insert the sorting result as follows: {0, 1, 3, 9, 32, 3, 55}
Gap = 1, I = 4, j = 5, a [4]> a [5], that is, 32> 3. Insert the following sorting result: Before processing: {0, 1, 3, 3, 9, 32, 55}
Iv. Time and space complexity time complexity: average O (nlogn), preferably O (n ^ 1.3), worst case O (n ^ 2) space complexity: O (1) only one cache zone is required.
V. References: hill sorting-Wikipedia, Hill sorting-Baidu encyclopedia, big talk data structure, 9.6 Hill sorting, implementation of the series of Vernacular classical algorithms, and classic sorting algorithm-hill sorting shell sort