C# implements Hill sort
Process dismantling
Suppose there is an existing array, as follows
The basic sorting code is as follows
static void Main(string[] args)
{
int[] array = new int[] {
5, 4, 5, 2, 3, 6, 1 };//替换代码
BaseSort(array, 3);//替换代码
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadKey();
}
public static void BaseSort(int[] array, int gap)
{
for (int i = gap; i < array.Length; i++)
{
int insertValue = array[i];
for (int j = i - gap; j >= 0; j -= gap)
{
if (insertValue < array[j])
{
array[j + gap] = array[j];
array[j] = insertValue;
}
else
{
break;
}
}
}
}
- Replace the replacement code with the following code, run and analyze
int[] array = new int[] {
5, 4, 5, 2, 3, 6, 1 };
BaseSort(array, 3);
//将下标差值为 3 的一组数进行排序
Find the number subscript 0 with an interval of 3 forward at subscript 3 , and use the insertion sort algorithm to sort.
Find the number subscript 1 with an interval of 3 in subscript 4 forward , and use the insertion sort algorithm to sort. Find the number subscript 2 with an interval of 3 forward at subscript 5 , and use the insertion sort algorithm to sort. Find the number subscript 3 and subscript 0 with an interval of 3 at subscript 6 forward , and at this time subscript 0 and subscript 3 have been sorted, and then use the insertion sort algorithm to sort.
- Replace the replacement code with the following code, run and analyze
int[] array = new int[] {
1, 3, 5, 2, 4, 6, 5 };
BaseSort(array, 1);
//将下标差值为 1 的一组数进行排序
Find the number subscript 0 with an interval of 1 forward at subscript 1 , and perform the insertion sort algorithm for sorting. Find the number subscript 1 and subscript 0 with an interval of 1 forward at subscript 1 , and perform insertion sorting algorithm for sorting. Find the number with an interval of 1 at subscript 1 forward, subscript 2, subscript 1, and subscript 0 , and perform insertion sorting algorithm for sorting. Find the number with an interval of 1 in subscript 1 forward, subscript 3, subscript 2, subscript 1, subscript 0 , and perform insertion sorting algorithm for sorting. Find the number with an interval of 1 in subscript 1 forward, subscript 4, subscript 3, subscript 2, subscript 1, subscript 0 , and perform insertion sorting algorithm for sorting. Find the number with an interval of 1 at subscript 1 forward, subscript 5, subscript 4, subscript 3, subscript 2, subscript 1, subscript 0 , and perform insertion sorting algorithm for sorting.
Algorithm implementation
- Hill sort uses intervals for group sorting and optimizes the direct insertion sorting algorithm .
- Choose half the length of the array as the initial interval length, and sort them, after which the loop takes n/2 as the interval.
- As far as possible, let the time algorithm complexity of the last direct insertion sort be O(n).
code show as below
static void Main(string[] args)
{
int[] array = new int[] {
5, 4, 5, 2, 3, 6, 1 };
ShellSort(array);
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadKey();
}
public static void ShellSort(int[] array)
{
for (int gap = array.Length / 2; gap > 0; gap /= 2)
{
//直接插入排序算法
for (int i = gap; i < array.Length; i++)
{
int insertValue = array[i];
for (int j = i - gap; j >= 0; j -= gap)
{
if (insertValue < array[j])
{
array[j + gap] = array[j];
array[j] = insertValue;
}
else
{
break;
}
}
}
}
}
The split code is as follows
public static void ShellSort(int[] array)
{
for (int gap = array.Length / 2; gap > 0; gap /= 2)
{
InsertSort(array, gap);
}
}
public static void InsertSort(int[] array, int gap)
{
for (int i = gap; i < array.Length; i++)
{
int insertValue = array[i];
for (int j = i - gap; j >= 0; j -= gap)
{
if (insertValue < array[j])
{
array[j + gap] = array[j];
array[j] = insertValue;
}
else
{
break;
}
}
}
}
Complexity and Stability
- Optimal time complexity: When the interval is 1 and the order is sorted , only the first for loop in the insertion sort needs to be experienced .
- Worst time complexity: When the array is in reverse order , the number of executions of the second for loop in the insertion sort can be reduced after the interval operation in the Hill sort . So the actual sorting time is the same as the worst time complexity of direct insertion sorting.
- Average time complexity: mainly depends on the value of the interval
- Space complexity: need to use the insertValue variable to record the value for comparison
- Stability: After the sorting algorithm, the previous values are ranked behind (eg: 5)
Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.