Insertion class sorting -- direct insertion, half-insertion, Hill

1. Direct insertion sort

1. Execution process:

Original sequence: 49, 38, 65, 97, 76, 13, 27

1) Only watch 49 at first

49        38,65,97,76,13,27 

2) Insert 38. 38<49, so 49 is moved back one position, 38 is inserted into the original position of 49

38,49          65,97,76,13,27

3) Insert 65. 65>49, so insert directly after 49

38,49,65         97,76,13,27

4) Insert 97. 97>65, so insert directly after 65

38,49,65,97          76,13,27

5) Insert 76. 76<97, so 97 is moved back one position; continue to compare, 76>65, 76 is inserted behind 65

38,49,65,76,97          13,27

6) Insert 13. 13<97, so 97 is moved back one position; continue to compare, 13<76, 76 is moved back, compare one by one, 13 should be inserted at the front

        13,38,49,65,76,97          27

7) Insert 27. 27 < 97, so 97 is moved back one position; continue the comparison. In the same way, the discovery should be inserted after 13

       13,27,38,49,65,76,97     

2. Algorithm idea:

 In each pass, a key to be sorted is inserted into the proper position of the sorted partial sorted sequence according to the size of its value, until all the keys to be sorted are inserted into the sorted sequence.

Code:

void InsertSort(int R[],int n)
{
	int i,j;
	int temp;
	for(i=1; i<n; ++i)
	{
		temp = R[i];
		j = i - 1;
	}
	while (j>=0 && temp<R[j])
	{
		R[j+l] = R[j];
		--j;
	}
	R[j+l] = temp;
}

3. Complexity analysis:

(1) Time complexity analysis
     From the insertion sorting algorithm code, R[j+i]=R[j]; in the innermost loop can be selected as the basic operation.
    1) In the worst case, that is, the entire sequence is reversed, the condition of temp<R(j) in the inner loop is always true. The time complexity is O(n²) .

    2) In the best case, that is, the entire sequence is already ordered, the condition of temp<R[j] in the inner loop is always invalid. The time complexity is O(n) .

    Combining the above two cases, the average time complexity of this algorithm is O(n²) .

(2) The space complexity is O(1) .

Two, half insertion sort

A basic condition of the fold-and-half search method is that the sequence is already ordered, and in the process of direct insertion sorting, a new keyword is inserted into an already-ordered sequence every time, so the fold-and-half search method can be used in this order. Find the insertion position in the sequence.

1. Execution process:

The sequence now is 13, 38, 49, 65, 76, 97   27 49

27 is about to be inserted, and the sequence in the array is:

                                   |-------------------- Sorted ----------------------- ----------|-----Unsorted-----|

keywords 13 38 49 65 76 97 27 49
array subscript 0 1 2 3 4 5 6 7

1) low=0, high=5, m=|_(0+5)/2_|=2, the keyword with subscript 2 is 49, 27<49, 27 is inserted into the lower half of 49, and high= m-1=1, low=0;

2) low=0, high=1, m=|_(0+1)/2_|=0, the keyword with subscript 0 is 13, 13<27, 27 is inserted into the high half of 13, and high= 1, low=m+1=1;

3) low=1, high=1, m=|_(1+1)/2_|=1, the keyword with subscript 1 is 38, 27<38, 27 is inserted into the lower half of 38, and high= m-1=0, low=1, low>high, the search is over;

4) Move the keywords 97, 76, 65, 49, 38 backwards in sequence. Then insert 27, and fold it in half and insert it to end.

2. Complexity analysis:

(1) Time complexity: O(nlog₂n) in the best case, O(n²) in the worst case

(2) The space complexity is O(1) .

3. Hill sort

1. Execution process:

    Original sequence: 49, 38, 65, 97, 76, 13, 27, 49 , 55, 04

    1) Split the sequence in increments of 5

    Subsequence 1: 49 13

    Subsequence 2: 38 27

    Subsequence 3: 65                                  49

    Subsequence 4: 97 55

    Subsequence 5: 76 04

     Perform direct insertion sorting on the 5 subsequences to get:

    Subsequence 1: 13 49

    Subsequence 2: 27 38

    Subsequence 3:                49                                   65

    Subsequence 4: 55 97

    Subsequence 5: 04 76
    One trip results in 13, 27, 49, 55, 04, 49 , 38, 65, 97, 76

     2) Divide the above sorted results by incrementing the sequence of 3 divisions

    Subsequence 1: 13 55 38 76

    Subsequence 2: 27 04 65

    Subsequence 3:                 49                      49 97

    Perform direct insertion sorting on the three subsequences to get:

    Subsequence 1: 13 38 55 76

    Subsequence 2: 04 27 65

    Subsequence 3:                 49                   49 97

    One trip results are 13, 04, 49 , 38, 27, 49, 55, 65, 97, 76

    3) Finally, divide the sequence with the increment of 1 to divide the above sorting result

    One trip results are 04, 13, 27, 38, 49, 49 , 55 , 65, 76, 97

    The two 49 positions are reversed, so the Hill sort is unstable .

3. Complexity analysis:

(1) The time complexity of Hill is O(n²).
(2) The space complexity is O(1) .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325638621&siteId=291194637