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

    一、冒泡排序

    冒泡排序的基本思路就是通过不断的比较相邻两个数的大小,第一层循环0--->length-1,然后第二层不断把大的一个数往后移动,每轮循环都会把最大的那个数放到最后。最多经过N-1轮就能完全排序好。如果优化下算法,还能更快。
    

    二、C语言一般实现

    #include<stdio.h>
    #include<stdlib.h>
    
    void buble_sort(int arr[], int length);
    
    
    void buble_sort(int arr[], int length)
    {
        int i, j, max;
        for(i=0, i<length-1, i++)
        {
            for(j=0, j<length-i-1, j++)
            {
                if(a[j]>a[j+1])
                {
                    max = a[j];
                    a[j] = a[j+1];
                    a[j+1] = max; 
                }
            }
        }
    }
    
    
    int  main()
    {
        int arr_num[] = {17, 23, 5, 54, 13, 36, 7};
        bubble_sort(arr_num, 7); 
        for(int i=0; i<7; i++)
            printf("%d  ", arr_num[i]);
        printf("
    ");
        system("pause");
        return 0;
    }
    

    三、C 语言改进实现

    #include<stdio.h>
    #include<stdlib.h>
    
    void better_buble_sort(int arr[], int length);
    
    
    void better_buble_sort(int arr[], int length)
    {
        int i, j, max;
        //设置标记位,sorted是否排序好
        bool sorted = true;
        for(i=0, i<length-1, i++)
        {
            for(j=0, j<length-i-1, j++)
            {
                if(a[j]>a[j+1])
                {
                    //如果本轮循环,有未排序好的数,就将设置的标记改为false,继续下轮循环
                    sorted = false
                    max = a[j];
                    a[j] = a[j+1];
                    a[j+1] = max; 
                }
            }
        }
        if(sorted) break;
    }
    
    
  • 相关阅读:
    SAP SD 模块面试题
    商品ATP check FM(获得可用库存和总库存)
    获得SO的凭证流
    SAP XI 常用事务代码
    ABAP 面试问题及答案(一)(转)
    ABAP 面试题(转)
    SAP XI 3.0考试大纲
    Enterprise System Applicaiton 试题
    Enterprise Portal 试题
    ABAP 100 道面试题
  • 原文地址:https://www.cnblogs.com/shiqi17/p/9393239.html
Copyright © 2011-2022 走看看