1. Direct Insert Sort
Direct insertion Sorting is one of the simplest sorting algorithms, and its basic operation is to insert a record into a sequence that has already been sorted to get a new ordered table. The direct insertion sorting algorithm works as follows:
The direct insertion sorting algorithm is as follows:
void insertsort (int arr[],int length ) {int key,j; for (int i=1 ; I< length ; ++i) {key=arr[i]; //record flag; J=i-1 ; //cycle compares and exchanges adjacent two numbers; while (J>=0 &&arr[j]>key) {Arr[j+1 ]=arr[j]; J=j-1 ; } arr[j+1 ]=key; }}
Insertion Sorting algorithm complexity analysis: from a spatial point of view, it requires a record of the secondary space, from the point of view of time, the basic operation of the sort: compare the size of two keywords and move records. As you can see from the algorithm, the number of times a for loop depends on the relationship of the keyword being inserted to the previous i-1 keyword. Throughout the sorting process, when the sequence to be sorted is recorded by keyword in a small to large positive order, only a n-1 comparison is made, the number of comparisons reaches the minimum, and the record does not need to be moved. Conversely, when the sequence to be sorted is ordered in reverse order, the total number of comparisons reaches the maximum value (n+2) (n-1)/2. The number of records moved also reached the maximum (n+4) (n-1)/2. We can take the average of the minimum and maximum values, then the number of comparisons and the number of moving records is approximately N^2/4. As a result, the direct insertion sort algorithm has an O (n^2) insertion order where the same elements are sorted before and after the position is not changed, so the direct insert sort is a stable sort.
2. Hill sort
Hill sort is also known as "narrowing the incremental sort" it belongs to the Insert Sort class method, but it has a great improvement in time efficiency compared with the preceding direct insertion sorting method. When inserting a sort, the time complexity is O (n) If all is in the positive order.
Therefore, if the sequence of records is "basically ordered", the efficiency of the direct insertion of the sort can be greatly improved. The basic idea of hill sort: First, the whole sequence of records is divided into several sub-sequences to insert the order, and then the whole record is sorted by a direct insertion when the entire series is "basically ordered". The process of hill sequencing is as follows:
Hill sorting algorithm idea: A set of records to be sorted by an increment D (n/2,n to the number of orders to be sorted) into several sets of sub-sequences, each group of records of the subscript difference D. Direct insertion of all elements in each group, followed by a smaller increment (D/2) to group it, The direct insert sort is then done in each group. Continue to shrink the increment until it is 1, and finally use the direct insert sort to complete the sorting.
The hill sort algorithm is described as follows:
voidShellsort (intArr[],int length){intGap//Select the step size of hill sort; //Calculate the number of hill sorts; for(Gap =length/2; Gap >0; Gap = Gap/2) {//conduct gap order; for(inti =0; I < gap; i++) {//One-time insertion sort; for(intj = i + gap; J <length; J = j + Gap) {if(Arr[j] < ARR[J-GAP]) {inttemp = Arr[j];intK = J-gap; while(k>=0&&arr[k]>temp) {arr[k + gap] = arr[k]; K = K-gap; } Arr[k + gap] = temp; } } } }}
The algorithm complexity calculation of hill sort is a complicated process, so far the best "increment" sequence function has not been found. On average, the algorithm complexity of the hill sort is O (n^1.3) because the local sort is based on the increment in the hill sort, so the same elements change before and after the hill sort, so the hill sort is an unstable sort.
3. Summary
Sorting algorithm is the basis of the algorithm, only to understand the basis of the algorithm, it will have a deeper understanding of the algorithm, this blog is mainly for a more in-depth understanding of the sorting algorithm in the insertion of the sort, if there is anything wrong, look at you more advice.
Insert sort of data structure and hill sort