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;
    }

  • 相关阅读:
    HDU 1097
    HDU 1045
    HDU 1039 -Easier Done Than Said?
    HDU 1038
    HDU 1037 - Keep on Truckin'
    HDU 1036 - Average is not Fast Enough!
    hdu 1701 ACMer
    hdu 1711 Number Sequence(kmp)
    hdu 2087 剪花布条
    字符串匹配-KMP算法学习笔记
  • 原文地址:https://www.cnblogs.com/rancvl/p/5209203.html
Copyright © 2011-2022 走看看