zoukankan      html  css  js  c++  java
  • 冒泡排序

      /*
        需求:
            使用“冒泡”排序进行数字排序
            从小到大进行排序
            
        思路:
            每一次循环找到最大的那个数,放在最后,循环的次数等于数字的个数减1 
            当前位于下一位进行比较,如果当前数比后一个数大,那么就交换两个数的位置
        */
    #include<stdio.h>
    #define LEN 10    //定义数组长度常量 
    int main()
    {
        int a[LEN];
        int i, j, t;
        
        //用户输入数字 
        printf("Please enter  the %d integer:
    ", LEN);
        for(i = 0; i < LEN; i++)
            scanf("%d", &a[i]);
        
        //排序    
        for(i = 1; i < LEN; i++)    //需要循环数字的个数-1 
        {
            for(j = 0; j < LEN-i; j++)    //对每一个数字进行比较 
                if(a[j] > a[j+1])    //如果左边的数字比右边的数字大,两个数字位置交换 
                {
                    t = a[j];
                    a[j] = a[j+1];
                    a[j+1] = t;
                }
        }
        
        //输出 
        for(i = 0; i < LEN; i++)
            printf("%d  ", a[i]);
        
        return 0;
    }

  • 相关阅读:
    django core cache 永不失效配置
    zabbix-ODBC-oracle
    time
    zabbix中的触发器依赖
    Servlet1
    每周总结01
    使用IntelliJ IDEA集成TomCat
    hadoop环境配置
    《软件工程》学习进度条博客16
    03梦断代码读后感3
  • 原文地址:https://www.cnblogs.com/rancvl/p/5209203.html
Copyright © 2011-2022 走看看