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

    <?php
    /**
     * Created by PhpStorm.
     * User: brady
     * Date: 2019/8/19
     * Time: 14:52
     */
    
    function bubble_sort($arr = [])
    {
    
        if(empty($arr) || !is_array($arr)){
            return [];
        }
    
        $max = count($arr);
        $change_count = 0;
        $total_count = 0;
        for($i=0;$i<$max-1; $i++){
            print_r("<br>第".($i+1)."趟比较开始<br>");
            for($j=0;$j<$max-1-$i;$j++){
                if($arr[$j] > $arr[$j+1]){
                    $tmp = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1] = $tmp;
                    print_r("<br>");
                    print_r(join(',',$arr));
                    $change_count++;
                }
                $total_count ++;
    
            }
        }
        var_dump("一共比较".$change_count);
        var_dump("总共循环".$total_count);
        return $arr;
    
    }
    
    $list = [12,3,4,5,7,2,3,4,55,66,22,11];
    
    echo "<pre>";
    print_r("比较前");
    print_r(join(',',$list));
    
    $res = bubble_sort($list);
    
    print_r("比较后");
    print_r(join(',',$res));
    

    python

    #coding=utf-8
    
    #冒泡排序
    def bubble_sort(input_list):
        if len(input_list) <= 0:
            return []
        else:
            for i in range(len(input_list)-1):
                print('第',i+1,'趟循环 
    ')
                for j in range(len(input_list)-1-i):
                    if input_list[j] > input_list[j+1]:
                        input_list[j],input_list[j+1] = input_list[j+1],input_list[j]
                    print("第",j,'次排序后结果',input_list,'
    ')
    
    
    if __name__ == '__main__':
        input_list = [4,2,3,7,8,1]
        print('排序前:',input_list)
    
        sorted_list = bubble_sort(input_list)
    
        print('排序后:',sorted_list)
    

      

    https://www.cnblogs.com/jingmoxukong/p/4302718.html

    https://cuijiahua.com/blog/2017/12/algorithm_1.html

  • 相关阅读:
    火狐URL编码问题
    Asp.net动态关键字
    charindex ,PATINDEX,contains,FREETEXT用法
    PanGu词库批量添加关键词
    Dictionary SortedList HashSet List用法
    hubbledotnet查询速度慢的问题
    asp.net 页面static变量问题
    String.Concat和String.Format用法
    .net显示今天农历的代码
    存储过程用户登录
  • 原文地址:https://www.cnblogs.com/brady-wang/p/11377328.html
Copyright © 2011-2022 走看看