C language-Direct Insertion Algorithm
Insert sorting is a Sort Algorithm. Instead of changing the original sequence (array), it creates a new sequence and operates on the new sequence.
Here we will explain how to sort data from small to large.
The basic idea of insert sorting is to add elements to the sorted array one by one. At the same time, it is required that the inserted elements must be in the correct position, in this way, the sorted array is still ordered.
In actual use, the entire unordered array is usually sorted. Therefore, this unordered array is divided into two sorted sub-arrays and elements to be inserted. In the first round, use the first element as the sorted sub-array and insert the second element. In the second round, use the first two elements as the sorted array and insert the third element. Similarly, in the I-round sorting, insert the I + 1 element in the subarray of the first I element. Until all elements are added to the sorted array.
Next, we will sort 3 2 4 1 to describe the insertion process, and use j to record the position where the element needs to be inserted. The sorting target is to sort arrays from small to large.
1st rounds
[3] [2 4 1] (in the initial state, 1st elements are divided into sorted sub-arrays, and the rest are elements to be inserted)
[3] [2 4 1] (due to 3> 2, the position to be inserted is j = 1)
[2 3] [4 1] (insert 2 to position j)
2nd rounds
[2 3] [4 1] (1st round sorting result)
[2 3] [4 1] (due to 2 <4, assume j = 2 First)
[2 3] [4 1] (j = 3 due to 3 <4)
[2 3 4] [1] (because 4 is just in position 3, no need to insert)
3rd rounds
[2 3 4] [1] (2nd round sorting results)
[2 3 4] [1] (j = 1 due to 1 <2)
[1 2 3 4] (insert 1 into position j. If the elements to be sorted are empty, the sorting ends) algorithm summary and Implementation of the selection of the N size of the unordered array R [N] to sort, N-1 wheel selection process. First, 1st elements are used as sorted child arrays, and then the remaining N-1 elements are inserted one by one into sorted child arrays ;. Therefore, in the I round sorting, the first I element is always ordered, and the I + 1 element is inserted to the correct position.
Copy text-only new window
# Include
# Include
# Include
Using namespace std; void insert_sort (int a [], int & n); // use the reference operation in C ++; int main () {int num [] = {89, 38, 11, 78, 96, 44, 19, 25}; // number of data elements: int N = sizeof (num)/sizeof (N [0]); insert_sort (num, N); for (int I = 0; I
// Insert sorting. void insert_sort (int a [], int & n) is sorted in ascending order. // n indicates the number of elements in array, here we use the reference operation in c ++; {int t = 0; for (int I = 2-1; I <= n-1; I ++) {if (a [I] t; j --) a [j + 1] = a [j]; // Insert the element to the correct position a [j + 1] = t ;}}}
Note: insert sorting is a stable sorting algorithm that does not change the order of the same numbers in the original sequence.
Insert sorting inserts an element at a time based on an ordered small sequence. Of course, at the beginning, this ordered small sequence had only one element, which was the first element. The comparison starts from the end of the ordered sequence, that is, the element to be inserted is compared with the already ordered sequence. If it is larger than it, it is directly inserted after it, otherwise, search until you find the inserted position. If you encounter an element that is equal to the inserted element, the inserted element is placed behind the element that you want to insert. Therefore, the order of equal elements is not changed, and the order from the original unordered sequence is the order after sorting, so insertion sorting is stable.