Sorting algorithm-----Hill sorting

Table of contents

Preface

Hill sort (shell)

Sorting principle

General idea

Example

 Code implementation (C language)

Analysis of Algorithms

time complexity

space complexity

stability


Preface

        I have a detailed article explaining insertion sort earlier (Link: Sorting Algorithm-----Insertion Sort (Detailed Picture and Text Explanation)_Greytard's Blog-CSDN Blog ) Today we will continue to study Hill sorting in the sorting algorithm (shell_sort), Hill sorting is related to insertion sort. It can be said that Hill sorting is a further optimization of insertion sort. Let’s start learning!

Hill sort (shell)

        Shell's Sort is a type of insertion sort , also known as "Diminishing Increment Sort". It is a more efficient and improved version of the direct insertion sort algorithm . Hill sorting is a non-stable sorting algorithm. This method is named after DLShell, which was proposed in 1959.

        Hill sorting groups records by a certain increment of the subscript, and sorts each group using the direct insertion sorting algorithm; as the increment gradually decreases, each group contains more and more keywords, and when the increment decreases to 1 , the entire file is divided into one group, and the algorithm terminates.

Sorting principle

General idea

        Hill sorting is to group an initial array by setting a grouping interval gap, and then sort the arrays in each group through the insertion sort algorithm. This is to complete a round of sorting; and then reduce the interval gap. , group again by the new gap interval, and then perform a new round of sorting according to the above method; finally until gap=1, the array at this time is a whole, and the whole array is sorted, and the final result This is the array after sorting. (The initial gap can be set to any value and will not affect the final result)

Example

Given an initial array [9,1,2,5,7,4,8,6,3,5] , how to complete this sorting through Hill sorting?

 The first step is to group first. At this time, set the interval gap to 5. After the grouping is completed, perform preliminary sorting. The results are as follows:

Group 1: 9 4 ——>4 9

Group 2: 1 8 ——>1 8

The third group: 2 6 ——>2 6

Group 4: 5 3 ——>3 5

The fifth group: 7 5 ——>5 7

At this time, the new array is   [4,1,2,3,5,9,8,6,5,7]

 The second step is to reduce the gap. You can reduce it by integer division. At this time, the gap is 2. Group and sort again. The result is as follows:

Group 1: 4 2 5 8 5——>2 4 5 5 8

The second group: 1 3 9 6 7——>1 3 6 7 9

At this time, the new array is  [2,1,4,3,5,6,5,7,8,9]

 In the third step, the gap is reduced again to 1, then there is only one group, that is, the entire array is sorted as a whole:

2 1 4 3 5 6 5 7 8 9——>1 2 3 4 5 5 6 7 8 9

At this point the sorting is complete.

The diagram is as follows: 

 Code implementation (C language)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
//希尔shell排序
void shell_sort(int* n,int length) {
	int i, j, gap;
	for (gap = length / 2; gap > 0;  gap /= 2) {
		for (i = 0; i < gap; i++) {
			for (j = i+gap; j < length; j += gap) {
				//以下对每一组进行插入排序
				int temp = n[j];
				int k = j - gap;
				if (n[j] < n[j - gap]) {
					while (k >= 0 && n[k] > temp) {
						n[k + gap]= n[k];
						k -= gap;
					}
					n[k + gap] = temp;
				}
			}
		}
	}
}

int main() {
	int array[10];
	srand((unsigned)time(0));
	for (int i = 0; i < 10; i++) {
		array[i] = rand() % 20;
	}
	for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
		printf("%d ", array[i]);
	}
	printf("\n排序后:");
	shell_sort(array, sizeof(array) / sizeof(int));//希尔排序
	for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
		printf("%d ", array[i]);
	}
}
//输出结果:
//3 9 17 4 5 18 13 14 12 15
//排序后:3 4 5 9 12 13 14 15 17 18

Analysis of Algorithms

time complexity

        The choice of increment sequence will greatly affect the efficiency of Hill sort. The time complexity of Hill sorting is very difficult to analyze. Its average complexity ranges from O(n) to O(n^2). It is generally believed that its best time complexity is O(n^1.3). Compared with Compared with insertion sort, the time complexity is reduced a lot, especially when the number of arrays is very large, the effect is most obvious. Of course, if the array is completely in reverse order, the time complexity is O(n^2).

space complexity

Hill sorting does not involve space opening, etc. The space used is the space of the original array, so the space complexity is O(1) 

stability

 We learned about insertion sort earlier and we know that one-time insertion sort is stable, but Hill sorting first groups and then inserts sorting, so when the same elements appear, the relative positions of the elements will change, so Hill sorting is unstable

 Okay, that’s all for this issue, see you in the next one! ! !

Share a wallpaper:

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/132863785