Direct Insert Sort

Source: Internet
Author: User

Direct insertion sorting is a very simple sort, with the basic idea of inserting each record in a sorted sequence sequentially into a sorted sequence until all records are ordered.

In this process, in order to reduce the number of comparisons, set a sentinel in the array 0 subscript position.
Direct insert sorting is simple and intuitive, but less efficient, with a time complexity of O (N2), because a sentinel is required, so the spatial complexity is O (1).

Here is the implementation code:

#include <iostream>using namespacestd;intMain () {intr[ -]; intN; CIN>>N;  for(intI=1; i<=n; i++) cin>>R[i];  for(intI=1; i<=n; i++) {r[0]=R[i]; intJ;  for(j=i-1; r[0]<r[j]; j--) {r[j+1] =R[j]; } r[j+1]=r[0]; }     for(intI=1; i<=n; i++) cout<<r[i]<<" "; return 0;}

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.