Direct Insert Sort

Source: Internet
Author: User

Direct Insert Sort

Basic idea: Suppose that the records to be sorted are stored in the array R[1..N]. Initially, r[1] self into 1 ordered areas, unordered area is R[2..N]. From i=2 up to i=n, sequentially insert r[i] into the current ordered area R[1..i-1], generating an ordered area with N Records.

Code implementation:

#include <stdio.h> #include <iostream>using namespace std;int n;void insertsort (int a[]) {    int i,j;    for (i=2;i<=n;i++)    {        if (a[i]<a[i-1])        {            a[0]=a[i];    Sentinel            A[i]=a[i-1];            for (j=i-2;a[0]<a[j];j--)                a[j+1]=a[j];        Move            a[j+1]=a[0];}        }    } int main () {    int a[100],i;    cin>>n;    for (i=1;i<=n;i++)        cin>>a[i];    Insertsort (a);    for (i=1;i<=n;i++)        cout<<a[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.