Insert Sort: There are n number, the number of the first is ordered, then I inserted into the row of the series is very simple, and the previous number one by one comparison is OK, for a sequence, then the second number from the beginning and the previous number, the first 2 numbers to insert the third number into the first 2 numbers is very simple, The fourth number is also the case, and so on .........
#include <iostream> #include <vector>using namespace std;//void insert_sort (vector<int> &a)//{/ /int temp;//size_t n = a.size ()//for (int i= 1; i < n; i++)//{//for (int j = i-1; J >= 0 && A[j]>a[j + 1]; j--)//{//temp = a[j];//a[j] = a[j + 1];//a[j+ 1] = temp;//}//}//}void insert_sort2 (vector<int> &a) {int temp;size _t n = a.size (); for (int i = 1, i < n; i++) {for (int j = i-1; J >= 0; j--) {if (a[j]>a[j + 1]) {temp = A[j];a[j] = a[j + 1];a[j + 1] = temp;}}} int main () {vector<int> a = {5,9, 6, 8, 1, 5,100};insert_sort2 (a); for (const auto I:A) cout << i < ;< "" << Endl;return 0;}
Insert Sort: There are n number, the number of the first is ordered, then I inserted into the row of the series is very simple, and the previous number one by one comparison is OK, for a sequence, then the second number from the beginning and the previous number, the first 2 numbers to insert the third number into the first 2 numbers is very simple, The fourth number is also the case, and so on .........
Research on algorithm of inserting sort