| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 507 人关注过本帖
标题:发个直接插入排序的代码,刚学C语言请指正
只看楼主 加入收藏
liuting8181
Rank: 2
等 级:论坛游民
帖 子:54
专家分:19
注 册:2011-4-21
结帖率:50%
收藏
 问题点数:0 回复次数:0 
发个直接插入排序的代码,刚学C语言请指正
#include<stdio.h>

#define  M 10

int arry[M]={0,41,48,23,56,78,90,23,56,67};

int sort(int a,int b);

void display(int a[M]);

int main(void)
{
    printf("显示当前数据\n");

    display(arry);

    for(int i=2;i<=M-1;i++)
    {
        if(sort(arry[i],arry[i-1]))
        {
            arry[0]=arry[i];

            arry[i]=arry[i-1];
   
        //for(int j=i-2; ;j--)
        int j=i-2;
        while (sort(arry[0],arry[j]))
        {
            arry[j+1]=arry[j];
            j--;
        }
            arry[j+1]=arry[0];

        }

            

   


    }

    printf("显示排序后的数据:\n");

    display(arry);


    return 0;
}

int sort(int a,int b)
{
    if(a<b)
        return  1;
    else
        return 0;
}

void display(int a[M])
{
    for (int i=1;i<=M;i++)

        printf("%-4d",arry[i]);
    printf("\n");
        
   
}
折半插入排序代码如下:
#include<stdio.h>

#define  M 10

int arry[M]={0,41,48,23,56,78,90,23,56,67};

int sort(int a,int b);

void display(int a[M]);

int main(void )
{
    printf("显示当前数据\n");
   
    display(arry);

    for (int i=2;i<=M-1;i++)
    {
        arry[0]=arry[i];

        int low=1;
        int high=i-1;

        while (low<=high)
        {
            int m=(low+high)/2;

            if(sort(arry[0],arry[m]))
                high=m-1;
            else
                low=m+1;
        }

        for(int j=i-1;j>=high+1;--j)
            arry[j+1]=arry[j];

        arry[high+1]=arry[0];


   


    }

    printf("显示排序后的数据:\n");
   
        display(arry);
    return 0;
}

int sort(int a,int b)
{
    if(a<b)
        return  1;
    else
        return 0;
}

void display(int a[M])
{
    for (int i=1;i<=M-1;i++)
        
        printf("%-4d",arry[i]);
    printf("\n");
   
   
}
搜索更多相关主题的帖子: 数据 void include display C语言 
2012-06-08 17:14
快速回复:发个直接插入排序的代码,刚学C语言请指正
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020828 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved