First, Hill sort rules
Calculates the maximum increment, the formula H = 3h+1
Insert sort with an H as the starting point and the-H increment
Reverse calculates the next increment h = (h-1)/3
Repeat the second step:
Note: The final will be sorted in 1 increments, with four steps to reduce the number of orders in increments of 1
Second, the code example
public class Shellsort
{
public static void Main (string[] args)
{
long[] Testarr = new long[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Long Templong; Sorted Data Temp Variable
int tempint; Temporary variable for insertion position
int h = 1; Incremental
1. Calculate the maximum increment, formula H = 3h+1
while (H <= TESTARR.LENGTH/3)
H = h*3 + 1;
Insert sort for increment h
while (H > 0)
{
2. Insert sort with H as the starting point, and-H increments
for (int i = H;i < testarr.length;i++)
{
Templong = Testarr[i];
Tempint = i;
while (Tempint > H-1 && testarr[tempint-h] >= templong)
{
Testarr[tempint] = testarr[tempint-h];
Tempint-= h;
}
Testarr[tempint] = Templong;
}
3. Reverse calculate the next increment
h = (h-1)/3;
}
Print sorted data
for (int i = 0; i < testarr.length; i++)
System.out.println (Testarr[i]);
}
}
Java Hill Sort