免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1366 | 回复: 0
打印 上一主题 下一主题

基础排序算法(冒泡排序、选择排序、插入排序) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-20 19:50 |只看该作者 |倒序浏览

冒泡排序、选择排序、插入排序
#include stdio.h>
#include string.h>
int strsrc[10] = {10, 1, 2, 5, 3, 9, 11, 12, 20, 9};
int str[10];
void load_str(void)
{
    int len = sizeof(str);
    memcpy(str, strsrc, len);
}
void swap(int* x, int* y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void exch(int* x, int* y)
{
    if(*x > *y) swap(x, y);
}
void sort_print(void)
{
    int i;
    int len = sizeof(str) / 4;
    for(i = 0;i len; i++) printf("%d ", str);
    printf("\n");
}
void sort_bubble(void)
{
    int i, j, l;
    l = sizeof(str) / 4;
    for(i = 0; i  l; i++)
    {
        for(j = l - 1; j > i; j--)
        {
            exch(&str, &str[j]);
        }
    }
}
void sort_select(void)
{
    int i, j, l;
    int min;
    int index = 0;
    l = sizeof(str) / 4;
    for(i = 0; i  l; i++)
    {
        min = str;
        for(j = i + 1; j  l; j++)
        {
            if(str[j]  min)
            {
                min = str[j];
                index = j;
            }
            
        }
        swap(&str, &str[index]);
    }
}
void sort_insert(void)
{
    int i, j, l;
    l = sizeof(str) / 4;
    for(i = 1; i  l; i++)
    {
        for(j = 0; j  i; j++)
        {
            if(str  str[j])
            {
                swap(&str, &str[j]);
            }
            
        }
    }
}
int main()
{
    printf("------------------------------sort_bubble------------------------------\n");
    load_str();
    sort_print();
    sort_bubble();
    sort_print();
    printf("------------------------------sort_select------------------------------\n");
    load_str();
    sort_print();
    sort_select();
    sort_print();
    printf("------------------------------sort_insert------------------------------\n");
    load_str();
    sort_print();
    sort_select();
    sort_print();
    return 0;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/99982/showart_2126444.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP