Insertion sorting principle and C++ source code implementation


1. Principle

Insert a piece of data into the sorted ordered list to form a new ordered list with the number of records incremented by 1.


2. Thinking

Direct insertion sorting is to insert the data in the unordered sequence into the ordered sequence. When traversing the unordered sequence, first compare the first element in the unordered sequence with each element in the ordered sequence and insert it into the appropriate Until all the elements in the unordered sequence are inserted.

Example:
Insert and sort data [1, 4, 2, 6, 7, 3, 5, 10, 9].

Taking the first element 1 of the array sequence as the ordered sequence, the ordered sequence [1] is obtained, and the array is [1, 4, 2, 6, 7, 3, 5, 10, 9].

  1. The first sorting: Take out the second element 4 of the array, insert the ordered sequence, and get the new ordered sequence [1,4], the sequence is [1, 4, 2, 6, 7, 3, 5, 10 , 9].

  2. Second sorting: Take out the third element 2 of the array, insert the ordered sequence, and get the new ordered sequence [1,2,4], the sequence is [1, 2, 4, 6, 7, 3, 5 , 10, 9].

  3. The third sorting: Take out the fourth element 6 of the array, insert it into the ordered sequence, and get the new ordered sequence [1,2,4,6], the sequence is [1, 2, 4, 6, 7, 3, 5, 10, 9].

  4. Fourth sorting: Take out the fifth element of the array 7, insert it into the ordered sequence, and get a new ordered sequence [1,2,4,6,7], and the sequence is [1, 2, 4, 6, 7, 3, 5, 10, 9].

  5. Fifth sorting: Take out the sixth primary color 3 of the array, insert it into the ordered sequence, and get the new ordered sequence [1,2,3,4,6,7], and the sequence is [1, 2, 3, 4, 6, 7, 5, 10, 9].

  6. The sixth sorting: Take out the seventh element 5 of the array, insert it into the ordered sequence, and get a new ordered sequence [1,2,3,4,5,6,7], and the sequence is [1, 2, 3, 4, 5, 6, 7, 10, 9].

  7. Seventh sorting: Take the eighth element 10 of the array, insert it into the ordered sequence, and get a new ordered sequence [1,2,3,4,5,6,7,10], and the sequence is [1, 2, 3, 4, 5, 6, 7, 10, 9].

  8. Eighth sorting: Take out the ninth element 9 of the array, insert it into the ordered sequence, and get a new ordered sequence [1,2,3,4,5,6,7,9,10], and the sequence is [ 1, 2, 3, 4, 5, 6, 7, 9, 10]


Three, insertion sort characteristics

  • Time complexity
    When the array to be sorted is ordered, it is the optimal situation. You only need to compare the current number with the previous number. This requires a total of N-1 comparisons, and the time complexity is O(n).
    The worst case is that the array to be sorted is in reverse order. At this time, the number of comparisons is the most. The total number of times is recorded as: 1+2+3+4+...+N-1. At this time, the time complexity of insertion sort in the worst case Is O(n2).

  • Space complexity The space complexity of
    insertion sort is constant order O(1)


Four, C++ source code implementation

The realization process uses a double-layer loop, the outer loop for all elements except one element, and the inner loop searches for the position to be inserted in the ordered list before the current element and moves it.


void StraightSort(int arr[], int len)
{
    
    
	int i,j,temp;
	for (i = 1; i < len; i++)
	{
    
    
		//数组往右逐个获取数据作为待插入到有序数组的值
		temp = arr[i];   
		
		//比较待插入值和有序数组的值,找到插入数值在有序数组中的位置
		for (j=i-1; 0 <=j && temp < arr[j]; j--) 
		{
    
    
			arr[j+1]=arr[j];
		}
		arr[j+1]=temp;
	}
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/110179234