Data structure: Common sorting algorithm (4): Hill sorting (C++ implementation)

Data structure: common sorting algorithm (4)-Hill sorting

Insertion sort:

1. Idea: At each step, a record to be sorted is inserted into the appropriate position of the previously sorted word sequence according to its sequence code size, until all insertions are sorted.
2. The key question: Find the appropriate insertion position in the previously sorted sequence.
Method: into direct insertion sort , binary insertion sort , Shell sort

Hill sort

Shell invented in 1959, the first sorting algorithm to break O(n2), which is an improved version of simple insertion sort. The difference between it and insertion sort is that it will compare the farther elements first. Hill sorting is also called reduced incremental sorting .

1. Basic idea:

Hill sorting is also called "reducing increment sorting". First, take an integer d1 smaller 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 d1 are placed in the same group. First, perform direct insertion sort in each group, and then take the second increment d2. It is an improved algorithm for insertion sorting. The Hill sorting step size is adjusted from large to small. The elements after the first cycle are compared and exchanged one by one with the previous elements according to the interval step, until the step size is 1, and the step size selection is the key.

Calculate the description:

First divide the entire sequence of records to be sorted into several sub-sequences for direct insertion sorting, the specific algorithm description:

  • Choose an incremental sequence t1, t2,..., tk, where ti>tj, tk=1;
  • Sort the sequence k times according to the number of increment sequences k;
  • In each sorting pass, according to the corresponding increment ti, the sequence to be sorted is divided into several sub-sequences of length m, and each sub-table is directly inserted and sorted. When only the increment factor is 1, the entire sequence is treated as a table, and the length of the table is the length of the entire sequence.

2. Examples

Sort the array a[11]={70,30,40,10,80,20,90,100,75,60,45} by Hill sorting

img
Image source: https://www.cnblogs.com/zwtgyh/p/10631760.html

#include<iostream>
using namespace std;
//希尔排序算法
void shellSort(int a[], int n)  //a -- 待排序的数组, n -- 数组的长度
{
	int i, j, gap;   // gap为步长,每次减为原来的一半。
	for (gap = n / 2; gap > 0; gap /= 2)
	{
		// 共gap个组,对每一组都执行直接插入排序
		for (i = 0; i < gap; i++)
		{
			for (j = i + gap; j < n; j += gap)
			{
				// 如果a[j] < a[j-gap],则寻找a[j]位置,并将后面数据的位置都后移。
				if (a[j] < a[j - gap])
				{
					int tmp = a[j];
					int k = j - gap;
					while (k >= 0 && a[k] > tmp)
					{
						a[k + gap] = a[k];
						k -= gap;
					}
					a[k + gap] = tmp;
				}
			}
		}
	}
}
//打印数组的函数
void print(int a[], int n)
{
	for (int j = 0; j<n; j++)
	{
		cout << a[j] << "  ";
	}
	cout << endl;
}

//主函数
int main()
{
	int a[11] = { 70, 30, 40, 10, 80, 20, 90, 100, 75, 60, 45 };
	cout << "初始序列:";
	print(a, 11);
	shellSort(a, 11);
	cout << "排序结果:";
	print(a, 11);
	system("pause");
}

3. Summary:

1. The time complexity of incremental sorting depends on the function of the incremental sequence taken, but so far there is no best incremental sequence. Someone has concluded after a large number of experiments; when n is in a specific After the range, the comparison and movement times of Hill sorting are reduced to n^1.3. Regardless of the value of the increment sequence, it should satisfy the last increment value of 1.

2. Some literature pointed out that when the incremental sequence is d[k]=2 (t-k+1), the time complexity of Hill sorting is O(n 1.5), where t is the number of sorting passes.

3. In terms of space complexity, binary insertion is also sorted in-place, and the space complexity is (O(1)).

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/108108612