First, direct insertion sort
Its idea: each time the first element is taken out of an unordered table, it is inserted into the proper position of the ordered table, and the ordered table remains orderly.
Let me take a look at my ideas:
1. First pass compares the first two numbers, then the second number is inserted into the ordered table by size.
2. The second pass scans the third data with the first two numbers from the front, and inserts the third number into the ordered table by size.
3. Proceed sequentially, the entire sequencing process is completed after the (n-1) scan.
Below I use the diagram to help you understand:
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/80/2B/wKiom1c5vWPD6XQdAAEyTTrgyrc502.jpg "title=" 0.jpg " alt= "Wkiom1c5vwpd6xqdaaeyttrgyrc502.jpg"/>
Implementation code:
void Insertsort (int *arr, size_t size) {int end;//the subscript int tmp;for of the last element of all elements to be sorted (int i = 0; i < size-1; i++) {end = I;t MP = Arr[end + 1];//The last element is saved//the element that is greater than the last element is moved backwards while (arr[end]>tmp&&end >= 0) {arr[end + 1] = arr[end];en d--;} Inserts the last element beyond its element Arr[end + 1] = tmp;}}
Ii. sort of Hill
Hill sort, also called descending incremental sorting algorithm, is a more efficient and improved version of insertion sequencing. Hill Sort is a non-stable sorting algorithm.
Hill Sort : The unordered array is divided into several sub-sequences, the sub-sequence is not segmented by segment, but a sub-sequence separated by a specific increment, the various sub-sequences are inserted sort, then select a smaller increment, and then split the array into multiple sub-sequences to sort ... The last selection increment is 1, which means that the final array is ordered using a direct insert sort.
Incremental Selection : There is an increment in the sort process for each trip, at least one rule increment relationship d[1] > D[2] > D[3]. > D[t] = 1 (t-trip sequencing); According to the increment sequence selection its time complexity will also change, this many papers carried on the research, here no longer delve into; in this paper, the preferred increment is N/2, this recursion, each increment is the original 1/2, until the increment is 1;
The process of hill sequencing is explained in detail:
650) this.width=650; "src=" http://img.my.csdn.net/uploads/201209/06/1346931476_8161.jpg "style=" border:none; "/ >
650) this.width=650; "src=" http://img.my.csdn.net/uploads/201209/06/1346931514_9288.jpg "style=" border:none; "/ >
Code implementation:
void Shillsort (int *arr, size_t size) {int end;//the subscript of the last element of all elements to be sorted int gap = size/2;//divides the array into size/2 subsequence int tmp;//temporary value for (GA p = SIZE/2; Gap > 0; Gap/= 2)//increment decrement {for (int i = gap; i < size; i++) {if (Arr[i]<arr[i-gap]) {end = I-gap;tmp = arr[i];//when Arr[end]> ; TMP, move while (arr[end]>tmp&&end >= 0) {arr[end + gap] = arr[end];end-= gap;} Arr[end + gap] = tmp;}}}
I hope you can understand that the writing is not good enough place, I hope you point out.
Insert sort (Direct insert sort and hill Sort)