Hill sorting principle and C++ source code implementation


1. Principle

A summary of an idiom: divide and conquer.

Hill sort is a kind of insertion sort, also known as "reduced incremental sort", which is a more efficient and improved version of the direct insertion sort algorithm. The records are grouped by a certain increment, and each group is sorted by the direct insertion sort algorithm. As the increment decreases, each group contains more and more keywords. When the increment decreases to 1, the entire file is just Divide into a group and the algorithm terminates.

Insert picture description here


2. Thinking

First, take an integer d1 less than n as the first increment to group all records in the array. All data whose distances are multiples of d1 are placed in the same group, and the direct insertion sort is performed in each group first; then, the second increment d2 (d2<d1) is used to repeat the above grouping and sorting until the increment is taken. Quantity dt=1 (dt<...<d2<d1), that is, all records are placed in the same group for direct insertion sort.
Generally, the first half of the sequence is taken as the increment, and then it is halved each time until the increment is 1.

Example:
Hill sort data [1, 4, 2, 6, 7, 3, 5, 10, 9].

  1. The first increment: 9/2=4.
    Insert picture description here
    Each group of colors represents a group, and then insert sorting in the group to get an array: [1, 3, 2, 6, 7, 4, 5, 10,9].

  2. The second increment: 4/2=2.
    Insert picture description here
    Each group of colors represents a group. Insert and sort the group to get the array: [1, 3, 2, 4, 5, 6, 7, 10,9].

  3. The third increment: 2/2=1, at this time, directly sort the array using insertion sort, and get the array: [1, 2, 3, 4, 5, 6, 7, 9, 10].


Three, Hill sorting characteristics

  • In different insertion sort processes, the same element may move in their respective insertion sorts, and finally its stability will be disrupted, so Hill sort is unstable.
  • It does not require a lot of auxiliary space and is as easy to implement as merge sort. The time complexity is O(n3/2), and the lower bound of the time complexity is n*log2n.
  • Hill sorting is not as fast as quick sorting. It performs well on medium-sized scales. It is not the best choice for sorting very large data. But it is much faster than the O(n2) complexity algorithm.
  • The execution efficiency of Hill's algorithm in the worst case and the average case is not much different, and at the same time, the execution efficiency of quick sort in the worst case will be very poor.
  • Almost any sorting job can use Hill sorting at the beginning. If it proves that it is not fast enough in actual use, then change to a more advanced sorting algorithm such as quick sort.

Four, C++ source code implementation


void shellSort(int arr[], int len)
{
    
    
	int gap=0;	//增量
	int i=0, j=0;
	int temp=0;

	for(gap=len/2; 0 < gap; gap /= 2) //每次增量都是取上次的一般半
	{
    
    
		//增量的位置右边后面所有的元素都要遍历比较
		for(i=gap; i<len; i++) 
		{
    
    
			temp=arr[i];
			//与i位置前面的数值做比较,若左边的值比右边的大则交换位置
			//比较交替位置以增量为差值
			for(j=i-gap; 0 <=j && temp < arr[j]; j=j-gap) 
			{
    
    
				Arr[j+gap]=arr[j];
			}
			arr[j+gap]=temp;
		}
	}
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/110181147