Insert sort and its upgraded Shell sort

Yesterday, I finally turned over the weekly magazine of big man Ruan. It can be said that most of the resources collected in my bookmark folder came from the left ear mouse and big man Ruan. The weekly magazine was turned over, but looking back, there was not much. Is it my curiosity problem? I was just satisfied when I found something new. Now when I look at the folders, everyone wants to learn, but I should have a little awareness of knowledge. Today I mainly want to learn sorting algorithms, but I open many websites at the same time. In this way, the efficiency decreases. I open the program sheep, senior brother Wu, fish C, and one is a foreign website, but I mainly look at the latter two, Month after month and year after year, the first two are all the articles that were previously posted on the official account. Then they will see other articles, and they will go on recursively. Carle, the handsome ones, are all aggregating the things that were previously exported, that is, a PDF or a website upgrading to make a book. Is that a little bit of doing it over the years? Will it become a product? Think about Liu Tong, who wrote a diary every day, then contributed, and finally synthesized a book whose youth is not confused. That is, he doesn't have to do one thing deliberately. He insists on doing a little every day, and then he will enjoy the compound interest brought by time. Think about it, the fund is the same. The rising days are more than the falling days. A little bit. Over time, there will be high points, I looked at the military industry I bought at noon. Now there are more than 40 points. Where is the market? If it falls for many days, the message reflected is that the country's economic development is not good, but this is impossible. The country's development is very fast, and this trend is irreversible. The official account, blog, website I admire are very clear that they can express an event through animation. They have such output, and their input may be 2 times or more times the output.

The last sentence summarizes the above text: take your time to be faster!

The first thing to look at is the insert sort code

 1 #include<stdio.h>
 3 void insert_sort(int arr[],int size)
 4 {
 5     int i,j;
 6     for ( i = 1; i != size; ++i)//From the beginning, it is because the elements with default index = 0 are ordered
 7     {
 8         int temp = arr[i];//temp will always be the first in the group of unordered elements on the right. This operation can be said to extract one from the original array, and its position in the array can be used freely
 9         for ( j = i - 1; j >= 0 && arr[j] > temp; --j)//At first, I wrote the update condition as j -= i
10         {//If the former is larger than the latter, it will be overwritten by the next bit. Won't the data of the latter bit be gone? Don't be afraid. It's at temp
11             arr[j + 1] = arr[j];
12         }
13         arr[j + 1] = temp;
14     }
15 }
17 void printsort(int arr[],int size)
18 {
19     for (int i = 0; i != size; ++i)
20     {
21         printf("%d ",arr[i]);
22     }
23     printf("\n");
24 }
27 int main() 
28 {
29     int array[] = {0,17,9,2,7,14,8,12};
30     int size = sizeof(array) / sizeof(array[0]);
31     insert_sort(array,size);
32     printf("Increasing sort:");
33     printsort(array,size);
34     return 0;
35 }

Insertion sorting is to move an element slowly from back to front to find its own positioning. This is very slow. Is there a relatively fast way? If you ask, there must be. Hill sorting is an improved method of insertion sorting. Its beauty is that it makes full use of the knowledge we learned in kindergarten, that is, large elements are behind and small elements are in front. In fact, the latter elements are compared with the former elements. The former elements are exchanged with the latter elements, but ah, It only enlarges the distance between the two numbers. For example, the first element is 9. If the elements behind it are smaller than it, if you use insertion sorting, it will walk alone and approach its coordinates step by step. If you use Hill sorting, it will save a lot of trouble and have a bit of dichotomy. Upper code

 1 #include<stdio.h>
 3 void shell_sort(int arr[],int size)
 4 {
 5     int i,j,span;
 6     for (span = size >> 1; span > 0; span >>= 1)//This line is to expand the comparison range of the two elements
 7     {
 8         for (int i = span; i != size; ++i)//Like insertion sorting, the first element is ordered by default, but the elements behind it are not compared with the first element here
 9         {
10             int temp = arr[i];
11             for (j = i - span; j >= 0 && arr[j] > temp; j -= span)//In fact, this cycle is executed once
12             {
13                 arr[j + span] = arr[j];
14             }
15             arr[j + span] = temp;
16         }
17     }
18 }
20 void printsort(int arr[],int size)
21 {
22     for (int i = 0; i != size; ++i)
23     {
24         printf("%d ",arr[i]);
25     }
26     printf("\n");
27 }
30 int main() 
31 {
32     int array[] = {0,17,9,2,7,14,8,12};
33     int size = sizeof(array) / sizeof(array[0]);
34     shell_sort(array,size);
35     printf("Increasing sort:");
36     printsort(array,size);
37     return 0;
38 }

In essence, they are the same. Then some children will have questions. How much faster does Hill sort arrive than insertion sort? Hill sort has three for loops. How can it be faster than insert sort? This child only looks at appearances. Did the teacher tell you how many for loops there are in kindergarten, and its time complexity is the power of O n?

The space complexity of insert sort and Hill sort is O(1)

The average time complexity and worst time complexity of insertion sort are O(n^2)

The average time complexity of Hill sort is O(nlogn), and the worst time complexity is O(n^2)

Posted by joelhop on Sat, 04 Dec 2021 15:31:59 -0800