Research on algorithm of inserting sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.