Java Hill Sorting Algorithm and java Hill sorting
Java Hill sorting algorithm code: http://www.zuidaima.com/share/1550463279090688.htm
The basic idea of the hill sorting algorithm is: first take an integer d1 less than n as the first increment, and divide all the records of the file into d1 groups. All records whose distance is a multiple of dl are placed in the same group. Sort directly inserted persons in each group first, and then take the second incremental d2 <d1 repeat the preceding grouping and sorting, until the incremental dt = 1 (dt <dt-l <... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting. This method is essentially a grouping insertion method.
package com.zuidaima.sort;/***@author www.zuidaima.com**/public class ShellSort { public static <T extends Comparable<? super T>> void shellSort(T[] array, int len) { int d = len; while (d > 1) { d = (d + 1) / 2; for (int i = 0; i < len - d; i++) { if (array[i + d].compareTo(array[i]) < 0) { T temp = array[i + d]; array[i + d] = array[i]; array[i] = temp; } } } } public static void main(String[] args) { Integer[] testArray = {23, 25, 12, 42, 35}; ShellSort.shellSort(testArray, 5); System.out.println("The result is:"); for (Integer item : testArray) { System.out.print(item); System.out.print(' '); } } }
JAVA Hill sorting
You can sort it for me !!!!!
The logic is wrong! It is not a code issue.
For (d = src. length/2; d> 0; d = d/2) Look at this for loop. D = d/2 is always d, so it's not getting smaller !! How to traverse the array comparison !. This is an obvious problem. You can definitely see it. I also want. But you didn't study it, right?
Public class shellsort {
Static void sort (int [] a, int dk ){
Int I, j, temp;
For (I = dk; I <a. length; I ++ ){
If (a [I] <a [I-dk]) {
Temp = a [I];
A [I] = a [I-dk];
For (j = I; j> 0 & temp <a [J-1]; j = j-dk ){
A [j] = a [J-1];
}
A [j] = temp;
}
}
}
Public static void main (String args []) {
Int [] a = {2, 4, 1, 5, 6, 8, 7 };
Int w = 1;
While (w <= a. length/5 ){
Sort (a, w );
W = w * 5 + 1;
}
For (int I = 0; I <a. length; I ++ ){
System. out. print (a [I] + "");
}
}
}
Java Data Structure-hill sorting interpretation code
First, explain the sort by hill Based on the Data found on the Internet:
Take an integer d1 smaller than n as the first increment, and divide all records of the file into d1 groups. All records whose distance is a multiple of dl are placed in the same group. Insert and sort data in each group first. Then, repeat the group and sort data in the second incremental d2 <d1 until the incremental dt = 1 (dt <dt-l <... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.
Next, let's talk about the essence of hill sorting: This method is essentially a grouping insertion method.
In addition, this sorting benefit: the reason why the time performance of hill sorting is better than that of direct insertion sorting:
① When the initial state of the file is basically ordered, the number of comparisons and moves required for direct insertion sorting is relatively small.
② When the n value is small, the difference between n and n2 is also small, that is, the best time complexity of direct insertion sorting O (n) and the worst time complexity 0 (n2) are not much different.
③ At the beginning of the hill sorting, there were a large increase in the number of groups and a small number of records in each group. Therefore, the number of records in each group was rapidly inserted. Later, the incremental di gradually reduced and the number of groups gradually reduced, the number of records in each group gradually increased, but because the di-1 has been used as the distance sorting order, the file is closer to the orderly state, so the new sorting process is also faster.
Therefore, the efficiency of hill sorting is much higher than that of direct insertion sorting.
Now, start to explain your code.
First, the code is grouped by the value of h. H = h * 3 + 1; and h = (h-1)/3; different values of h can be generated, and arrays can be divided into several groups. Then, insert the sorting method in each group. As the h value becomes smaller and smaller, the sorting is more and more meticulous. When h = 1, it is equivalent to performing a complete insertion sorting. However, this is the sorting array of the entire queue, so it is faster than simply using insert for sorting.
This is the essence of the Code.
It's time to eat.