[Data Structure--Hand Sorting Part 2] This article will take you to understand Hill sorting in detail

Table of contents

1. Common sorting algorithms

1.1 Basic idea of ​​insertion sort

2. Hill sort

2.1 Hill sorting (shrinking incremental sorting)

2.1.1 Pre-sorting stage

2.1.2 Insertion sort phase

2.2 One-way Hill sorting

2.2.1 Idea analysis

2.2.2 Code implementation

3. Hill sorting code implementation

4. Hill sort time complexity

5. Efficiency comparison between Hill sorting and insertion sorting

6. Summary of Hill sorting characteristics


1. Common sorting algorithms

1.1 Basic idea of ​​insertion sort

Direct insertion sorting is a simple insertion sorting method. Its basic idea is:
insert the records to be sorted into a sequence that has been sorted one by one according to the size of their key values, until all records are inserted.
, a new ordered sequence is obtained .
In practice, when we play poker, we use the idea of ​​insertion sort

2. Hill sort

2.1 Hill sorting (shrinking incremental sorting)

The Hill sorting method is also known as the shrinking increment method. The basic idea of ​​the Hill sorting method is: first select an integer gap, divide all the records in the file to be sorted into multiple groups, and group all records with a distance difference of gap into the same group, and perform Sort. Then, take gap=gap/3+1 to repeat the above grouping and sorting work. When gap=1 is reached, all records are sorted in the same group.

Let's draw a picture to analyze: we are here in ascending order

From the figure, we can actually see that when the last gap=1, the essence is insertion sorting, and Hill sorting is to pre-sort first, and finally insert sorting once to achieve sorting .

Let's analyze the above picture:

2.1.1 Pre-sorting stage

1. In the first trip, when gap=5, it means that every interval of 5 numbers forms a group, 9 and 4, 1 and 8, 2 and 6, 5 and 3, 7 and 5 form a group, when the following If the number is smaller than the previous number, we will exchange it, so that we will arrange the small numbers in these groups to the front, and the large numbers to the back;

2. In the second trip, let gap = gap/3+1 (here gap/3+1 is to get gap=1 in the end, when gap<3, if you just divide by 3, you get 0, so Complete sorting cannot be achieved), gap=2, every interval of 2 numbers is a group, 4 is a group with 2, 5, 8, 5, 1 is a group with 3, 9, 6, 7, compare the size, and then exchange;

2.1.2 Insertion sort phase

3. When adjusting gap=1, the essence is insertion sorting. At this time, after the previous pre-sorting stage, our numbers are close to order, but not completely orderly. Insertion sorting at this time will greatly improve efficiency big.

2.2 One-way Hill sorting

2.2.1 Idea analysis

The one-way Hill sort is actually to divide the numbers separated by gap into a group and sort them first.

Drawing analysis:

Divide the numbers with a gap of each interval of a single trip into a group and sort them first.

2.2.2 Code implementation

for (int i = 0; i < n - gap; i++)//多组一起排序(没有提升效率,只是少写一组循环)
{
    int end = i;
    int tmp = a[i + gap];
    while (end >= 0)
    {
        if (a[end] > tmp)
        {
            a[end + gap] = a[end];
            end -= gap;
        }
        else
        {
            break;
        }
    }
    a[end + gap] = tmp;
}

3. Hill sorting code implementation

Add a layer of loop to the single-pass sorting, so that the gap is continuously reduced until it is 1, and the Hill sorting is realized.

void ShellSort(int* a, int n)
{
	//1. 当 gap > 1 时先进性预排序
	//2. 当 gap == 1 时直接插入排序
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;// +1可以保证最后一次一定是1
		for (int i = 0; i < n - gap; i++)//多组一块进行
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (a[end] > tmp)
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tmp;
		}
	}
}

4. Hill sort time complexity

The time complexity of Hill sorting is not easy to calculate, because there are many ways to value the gap, which makes it difficult to calculate. Therefore, the time complexity of Hill sorting given in many books is not fixed.

Here are two books describing the time complexity of Hill sorting:

The gap here is calculated according to the method proposed by Knuth, and Knuth has conducted a large number of experimental statistics. For the time being, we will calculate according to: O(n^1.25) to O(1.6*n^1.25).

5. Efficiency comparison between Hill sorting and insertion sorting

We open up two arrays, both of which are 100,000 in size, and use Hill sorting and insertion sorting to compare the time consumed.

//时间对比
void TestOp()
{
	srand((unsigned int)time(NULL));
	const int n = 100000;
	int* a1 = (int*)malloc(sizeof(int) * n);
	int* a2 = (int*)malloc(sizeof(int) * n);

	for (int i = 0; i < n; ++i)
	{
		a1[i] = rand();
		a2[i] = a1[i];
	}

	int begin1 = clock();
	InsertSort(a1, n);
	int end1 = clock();

	int begin2 = clock();
	ShellSort(a2, n);
	int end2 = clock();


	printf("InsertSort:%d\n", end1 - begin1);
	printf("ShellSort:%d\n", end2 - begin2);

	free(a1);
	free(a2);
}

int main()
{
	TestOp();
	return 0;
}

We can see that Hill sorting is much more efficient than insertion sorting. When the amount of data is larger, Hill sorting is more dominant.

However, when the array itself is ordered, Hill sorting is not as good as insertion sorting, because Hill sorting has pre-sorting, and it takes time to execute.

6. Summary of Hill sorting characteristics

1. Hill sort is an optimization of direct insertion sort.
2. When gap > 1, it is pre-sorted, the purpose is to make the array closer to order. When gap == 1, the array is already close to order, so it will be very fast. In this way, the overall optimization effect can be achieved. After we implement it, we can compare performance tests.
3. The time complexity of Hill sorting is not easy to calculate, because there are many ways to value the gap, which makes it difficult to calculate. Therefore, the time complexity of Hill sorting given in many books is not fixed.

4. Stability: Unstable.

Guess you like

Origin blog.csdn.net/Ljy_cx_21_4_3/article/details/131597842