This article will introduce three sorting algorithms--insert sort, hill sort, heap sort. All examples of this article use ascending
I. Insert Sort
Algorithmic thinking
Maintains an ordered array, comparing the data to be inserted with the number one by one of the ordered array from the last element until the appropriate position.
Eg: ordered array: 1,3,5,6,7 now the data to be inserted is 2, then he will be compared with 7,6,5,3, in turn, when the inserted data is less than the last element size of the ordered array, move the element back until the element to be inserted finds a suitable position.
Code implementation
Void insertsort (int* a, int size) 02{03 assert (a);04 for (int i = 0; i < size - 1; ++i) 05 {06 int end = i; //the last 07 that identifies an ordered array int tmp = a[end + 1];08 while (End >= 0 && tmp < a[end]) 09 {10 a[end + 1] = a[end]; //the data to be inserted is smaller than the last number of an ordered array, and the last digit of the ordered array is shifted backwards 11 --end;12 }13 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;A[END&NBSP;+&NBSP;1]&NBSP;=&NBSP;TMP;14&NBSP;&NBSP;&NBSP;&NBSP;}15}
Summarize
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/6B/wKiom1dAlf6C8SCwAABJqI_Mfjc653.png "title=" Insert sort. png "style=" border:0px;vertical-align:middle; "alt=" Wkiom1dalf6c8scwaabjqi_mfjc653.png "/> 1. The insertion sort can be thought of as the insertion algorithm with a spacing of 1, saying this is to better understand the hill sort later.
2. The time complexity of inserting sorting is O (n^2);
3. The spatial complexity of the insertion sort is O (1);
4. With stability
the stability of the sorting algorithm means that the relative position of the same element sorted by the sorting algorithm does not change.
Two. Hill sort
Algorithmic thinking
The hill sort can be thought of as an enhanced version of the insert sort, because he has added a pre-order process, that is, before the insertion algorithm with a spacing of 1, he has pre-arranged an array of gaps (gap has been reduced until >0). Therefore, when the gap = 1 of the insertion of the order to make the array is very close to order, so that the gap = 1 of the sorting time complexity, can be less than O (n^2) (gap = 1 of the insertion Order, the best case of Time complexity O (1), the previous pre-scheduling process is for this purpose).
Code implementation
Hill sort 02void shellsort (int* a,size_t size) 03{04 assert (a);05 int gap = size / 2;06 while (gap > 0) 07 {08 for (int i = 0; i < size - gap; ++i) 09 {10 int end = i; 11 int tmp = a[end + gap];12 while (end >= 0 & & tmp <&Nbsp;a[end]) 13 {14 a[end + gap] = a[end]; 15 end -= gap;16 }17 a[end + gap] = tmp ; 18 }19 --gap;20 &NBSP;&NBSP;&NBSP;&NBSP;}21}
Summarize
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/69/wKioL1dAmcezLq9UAAA-_4AF_-0959.png "title=" Hill sort. png "style=" border:0px;vertical-align:middle; "alt=" Wkiol1damcezlq9uaaa-_4af_-0959.png "/> is a pre-row for gap = 5
1. Hill sort the idea of pre-sorting is consistent with the idea of inserting a sort, except that he divides the original array into different intervals.
2. The time complexity of hill sorting is O (n^2), and the space complexity is O (1);
3, with instability
Three. Heap sequencing
Algorithmic thinking
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/6B/wKiom1dAmjOj5RrrAABza1LyiDI524.png "title=" Heap sort. png "style=" border:0px;vertical-align:middle; "alt=" Wkiom1damjoj5rrraabza1lyidi524.png "/> Code implementation
Void adjustdown (int* a, size_t size, int parent) 02{03 ASSERT (a);04 int child = parent * 2 + 1;05 while (child<size) 06 {07 if (child+1<size && a[child] < a[child + 1]) 08 ++child;09 if (A[parent] < a[child]) 10 {11 swap (a[parent], a[ Child]); 12 parent = child;13 child = parent * 2 + 1;14 }15 else16 {17 break ; 18 }19 20 }21}22void heapsort (int* a,size_t size) 23{24 ASSERT (a); 25 //build Heap 26 for (int i = (size -&NBSP;2) / 2; i >= 0; --i) //start from the first non-leaf node 27 {28 adjustdown (a, size, i);29 }30 for (Size_t i = 0; i < size; ++i) 31 {32 swap (a[0], a[size - 1 - i]); 33 adjustdown (a, size - i - 1, 0 ); 34 }35}
Summarize
1. The time complexity is O (N*LGN), the space complexity is O (1);
2. With instability
The above is my study in the process of some experience summary. Of course, my ability is limited, there will inevitably be flaws, I hope you can correct me.
Insert Sort, hill sort, heap sort detailed