Original address: C/C ++ algorithm learning notes (6)-Insert Method
Insert method:
The insertion method is complicated. The basic working principle is to draw a card, find the corresponding position in the front card, and then continue to the next one.
#include <iostream.h>void InsertSort(int* pData,int Count){ int iTemp; int iPos; for(int i=1;i<Count;i++) { iTemp = pData[i]; iPos = i-1; while((iPos>=0) && (iTemp<pData[iPos])) { pData[iPos+1] = pData[iPos]; iPos--; } pData[iPos+1] = iTemp; }} void main(){ int data[] = {10,9,8,7,6,5,4}; InsertSort(data,7); for (int i=0;i<7;i++) cout<<data[i]<<" "; cout<<"/n";}
I = 1:
Itemp = pdata [1] = 9
IPOs = 1-1 = 0;
IPOs = 0> = 0 & itemp = 9 <pdata [0] = 10;
Pdata [1] = pdata [0] = 10;
IPOs -- = 0-1 =-1;
Pdata [0] = 9; 9-10-8-7-6-5-4
I = 2:
Itemp = pdata [2] = 8
IPOs = 2-1 = 1;
IPOs = 1> = 0 & itemp = 8 <pdata [1] = 10;
Pdata [2] = pdata [1] = 10;
IPOs -- = 1-1 = 0; 9-10-10-7-6-5-4
IPOs = 0> = 0 & itemp = 8 <pdata [0] = 9;
Pdata [1] = pdata [0] = 9;
IPOs -- = 0-1 =-1;
Pdata [0] = 8; 8-9-10-7-6-5-4
I = 3:
Itemp = pdata [3] = 7
IPOs = 3-1 = 2;
IPOs = 2> = 0 & itemp = 7 <pdata [2] = 10;
Pdata [3] = pdata [2] = 10;
IPOs -- = 2-1 = 1; 8-9-10-10-6-5-4
IPOs = 1> = 0 & itemp = 8 <pdata [1] = 9;
Pdata [2] = pdata [1] = 9;
IPOs -- = 1-1 = 0; 8-9-9-10-6-5-4
IPOs = 0> = 0 & itemp = 7 <pdata [0] = 8;
Pdata [1] = pdata [0] = 8;
IPOs -- = 0-1 =-1;
Pdata [0] = 7; 7-8-9-10-6-5-4
I = 4:
Itemp = pdata [4] = 6;
IPOs = 4-1 = 3;
IPOs = 3> = 0 & itemp = 6 <pdata [3] = 10;
Pdata [4] = pdata [3] = 10;
IPOs -- = 3-1 = 2; 7-8-9-10-10-5-4
IPOs = 2> = 0 & itemp = 7 <pdata [2] = 9;
Pdata [3] = pdata [2] = 9;
IPOs -- = 2-1 = 1; 7-8-9-9-10-5-4
IPOs = 1> = 0 & itemp = 7 <pdata [1] = 8;
Pdata [2] = pdata [1] = 8;
IPOs -- = 1-1 = 0; 7-8-8-9-10-5-4
IPOs = 0> = 0 & itemp = 7 <pdata [0] = 7;
Pdata [1] = pdata [0] = 7;
IPOs -- = 1-1 = 0;
Pdate [0] = 6; 6-7-8-9-10-5-4
We can see from the above:
Insert sorting first extracts the next element from the set and puts it in a temporary variable to compare it with the first element. Record the position of the element in the set. If the second element is smaller than the first element, the first element and the second element are called. Next, compare the third element with the second element after the change. If the second element after the change is smaller than the third element, overwrite the third element with the value of the second element. Extract the element from the temporary variable and put it in the second element.
Reverse Order (worst case)
First round: 10, 9, 8, 7-> 9, 10, 8, 7 (switching once) (loop once)
Round 2: 9, 10, 8, 7-> 8, 9, 10, 7 (switching once) (looping twice)
First round: 8, 9, 10, 7-> 7, 8, 9, 10 (switching once) (looping three times)
Cycles: 6
Number of exchanges: 3
Others:
First round: 8, 10, 7, 9-> 8, 10, 7, 9 (0 switching) (1 loop)
Round 2: 8, 10, 7, 9-> 7, 8, 10, 9 (switching once) (looping twice)
First round: 7, 8, 10, 9-> 7, 8, 9, 10 (switching once) (loop once)
Number of cycles: 4
Number of exchanges: 2
The behavior analysis at the end of the preceding section actually creates an illusion that this algorithm is the best in a simple algorithm, but it is not because its number of loops is not fixed, we can still use the O method. From the above results, we can see that the number of cycles F (n) <= 1/2 * n * (n-1) <= 1/2 * n. So its complexity is still O (N * n) (here, we will explain that the number of exchanges can still be deduced if it is not to show the differences in these simple sorting ).
Now let's look at the switching. In terms of appearance, the number of switching times is O (n) (derivation is similar to the selection method), but we need to perform the '=' operation with the same number of inner loops each time. For a normal exchange, we need three times '=', but here we are obviously a little more, so we waste time.
In the end, I personally think that the selection method is the best in simple sorting algorithms.
Insert sort
# Include <iostream> usingnamespace STD; void coutstream (int A [], int N) {for (INT I = 0; I! = N; I ++) cout <A [I] <"";} void insertsort (int A [], int N) {int temp; for (INT I = 1; I <n; I ++) {Int J = I; temp = A [I]; // first save the data at location a [I] While (j> 0 & temp <A [J-1]) {A [J] = A [J-1]; J --;} A [J] = temp;} int main () {int A [5] = {, 4}; insertsort (A, 5 ); // Insert the sorted coutstream (A, 5); // return0 ;}