Data structure-sort-direct Insert sort

Source: Internet
Author: User

                 Direct Insert Sort

The direct insertion sort is similar to the arrangement of playing cards, initially taking the first one as an ordered sequence, and then the whole disorder.

This sort has been written many times, but every time I look back I feel the ingenious arrangement of the designer.

#include <iostream>#include<vector>using namespacestd;//first define the test array here a Sentinel is defined at subscript 0 and then traversed from the back forward//The Sentinel is designed to reduce the problem of judging subscripts when traversing from backward to forward.inttest[]={0,5, -, the, One, A, $,98, -, the};//initializing vectors with arraysvector<int>test_v (Begin (Test), End (test));//Print ArrayInlinevoidPRINTV (Constvector<int>&v) {       for(auto a:v) cout<<a<<" "; cout<<Endl;}//Direct Insert SortvoidInsert_sort (vector<int> &4) {cout<<"Direct Insert Sort"<<Endl;  for(intI=2; I<v.size (); i++) {v[0]=v[i];//assign a value to a sentry          intj=0; //here J Initializes a position in front of I//start the last one from the positive order and the comparison to insert//If you want to insert a smaller than the current positive order [j], j--then move the data back one position           for(j=i-1; v[0]<v[j];j--) {v[j+1]=V[J];/*V[0] has saved v[i] content It's a clever idea to be covered .*/           }           //at the end of the cycle, the inevitable v[0]>v[j], we just v[j+1]=v[0] canv[j+1]=v[0]; } printv (v);//Print each result for easy observation}         intMain () {insert_sort (test_v); return 0;}

Direct insertion sorting is a stable sort;

But when the amount of data is large, efficiency will be reduced.

According to the characteristics, it is best to move 2 times at a time, as long as the comparison occurs. V[0], respectively, and v[j+1]=v[0]; The total number of comparisons is 2 (n-1).

So the time complexity of the case is 0 (n).

In the worst case, each time you want to compare the sentinel bits of all numbers +v[0], the first time is 2 and then 3 4 5 .... N. There's no doubt it's a square order.

So the worst case is O (n^2);

Data structure-sort-direct Insert 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.