Direct Insert Sort

Source: Internet
Author: User
Inserts the sort directly, and its insertion position is determined by comparing the size of the record to be inserted with each record in the ordered area from right to left. This example uses the direct insertion method to sort the numbers from small to large.
#include <stdio.h>void insort(int s[],int n){    int i,j;    for(i=2;i<=n;i++)/*数组下标从2开始,s[0]做监视哨*/    {        s[0]=s[i];/*将待插入的值赋给监视哨*/        j=i-1;        while(s[0]<s[j])        {            s[j+1]=s[j];/*数据右移*/            j--;/*移向左边一个未比较的数*/        }        s[j+1]=s[0];/*在确定的位置插入是s[i]*/    }}void main(){    int a[11],i;    printf("请输入十个数据:\n");    for(i=1;i<=10;i++)        scanf("%d",&a[i]);/*键盘输入10个数到数组*/    printf("原始顺序:\n");    for(i=1;i<11;i++)        printf("%5d",a[i]);    insort(a,10);/*调用插入函数*/    printf("\n插入排序后的顺序:\n");    for(i=1;i<11;i++)        printf("%5d",a[i]);    printf("\n");}

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.