Chinaunix首页 | 论坛 | 博客
  • 博客访问: 247460
  • 博文数量: 49
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-13 00:59
个人简介

make it run,make it better,make it fast. https://github.com/liulanghaitun

文章分类

全部博文(49)

文章存档

2023年(1)

2022年(2)

2020年(4)

2019年(4)

2017年(15)

2016年(3)

2014年(3)

2013年(14)

分类: C/C++

2016-02-28 17:04:54

  1. 1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  2. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
  3. 3.针对所有的元素重复以上的步骤,除了最后一个。
  4. 4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较

    代码实现:
    #include

    void bubble_sort(int* base,int lenght);

    int main(void){
            int test[] = {12,23,123,42,9,63,82};
            int lenght=sizeof(test)/sizeof(test[0]);
            bubble_sort(test,lenght);
            for(int i=0;i                 printf("%d\t",test[i]);
            }
            printf("\n");
            return 1;
    }
    void bubble_sort(int* base,int lenght){
            int tmp_value ;
            for(int i=0;i                 for(int j=0;j                         if(*(base+j) > *(base+j+1)){
                                    tmp_value=*(base+j);
                                    *(base+j)=*(base+j+1);
                                    *(base+j+1)=tmp_value;
                            }
                    }
            }
    }

    结果输出:
    9 12 23 42 63 82 123

阅读(1784) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~