直接插入排序(Insertion Sort):

来源:互联网 发布:apache 2.2 漏洞 编辑:程序博客网 时间:2024/06/05 08:33
直接插入排序(Insertion Sort):


基本思想是:每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子序列中的适当位置,直到全部记录插入完成为止。
源程序如下:


#include <stdio.h>
#define N 10
void insertsort(int a[])
{
    int i,j,temp;
    for(i=1;i<N;i++)
        if(a[i]<a[i-1])
        {
            temp=a[i];   //将待排序的数放到temp中
          for(j=i-1;a[j]>temp;j--)  //向前移动数字
              a[j+1]=a[j];
             a[j+1]=temp;  
    }


        for(i=0;i<N;i++)
          printf("%d ",a[i]);
        
}


int main()
{
    int a[N]={10,9,8,7,6,5,4,3,2,1};
    insertsort(a);
    return 0;


}

原文地址: http://www.bcoder.cn/?p=415


0 0
原创粉丝点击