The insertion sort is a sort algorithm that does not change the original sequence (array), but instead creates a new sequence that operates on the new sequence.
This is explained by the example of small to large sort.
The basic idea of inserting a sort is to add elements one by one to an already sorted array, while requiring that the inserted elements be in the correct position, so that the original sorted array is still in order.
In actual use, it is usually the order of the entire unordered array, so the unordered array is divided into two parts sorted by the sub-array and the element to be inserted, the first round, the first element as a sorted sub-array, insert the second element, the second round, the first two elements as a sorted array, insert a third element; I insert the i+1 element in the sub-array of the first I element when I turn the sort. The array is sorted until all elements are added.
Below, a selection of 3 2 4 1 is used to sort the insertion procedure, using J to record where the element needs to be inserted. The sort goal is to arrange the array from small to large.
1th Round
[3] [2 4 1] (the initial state, the 1th element is divided into a sorted sub-array, the remaining elements to be inserted)
[3] [2 4 1] (due to 3>2, so to insert position J=1)
[2 3] [4 1] (insert 2 to position J)
2nd round
[2 3] [4 1] (1th round sort result)
[2 3] [4 1] (due to 2<4, so first assume j=2)
[2 3] [4 1] (due to 3<4, so j=3)
[2 3 4] [1] (due to 4 just in position 3, no need to insert)
3rd Round
[2 3 4] [1] (2nd round sort result)
[2 3 4] [1] (due to 1<2, so j=1)
[1 2 3 4] (will be 1 insert position J, to be sorted element is empty, sort end) algorithm summary and implementation Select sort the unordered array of size N r[n] to sort, make the N-1 wheel selection process. The 1th element is first used as a sorted sub-array, and then the remaining N-1 elements are inserted one after another into the sorted sub-array; Therefore, in the first round of sorting, the first I element is always ordered, and the i+1 element is inserted into the correct position.
Copy Plain Text New window
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace Std;
- void Insert_sort (int a[],int &n); Use reference operations in C + +;
- int Main ()
- {
- int num[] = { 11,. , ------ , - };
- Number of data elements:
- int n=sizeof (num)/sizeof (n[0]);
- Insert_sort (num,n);
- for (int i=0; i<N; i+ +)
- cout<<num[i]<<endl;
- system ("pause");
- return 0 ;
- }
- //Insert sort implementation, here by small to large sort
- void Insert_sort (int a[],int &n) //n The number of elements in the array A, where the reference operation in C + + is used;
- {
- int t=0;
- for (int i=2-1; i<=n-1; i+ +)
- {
- if (A[i]<a[i-1] )//First find element A[i] position to insert
- {
- T=a[i];
- for (int j=i-1;a[j]>t;j--)
- A[J+1]=A[J];
- //Insert element in the correct position
- a[j+1]=t;
- }
- }
- }
Note: Insertion sorting is a stable sorting algorithm that does not change the order of the same numbers in the original sequence.
An insert sort is an element that is inserted one at a time, based on an already ordered small sequence. Of course, there are only 1 elements at the beginning of this ordered sequence, which is the first element. The comparison starts at the end of the ordered sequence, that is, the element that you want to insert and the one that is already in order, is inserted directly behind it if it is larger than it is, or until it is found where it is inserted. If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.
C language--Direct insertion algorithm