• php实现冒泡排序


    冒泡排序是非常容易理解和实现,,以从小到大排序举例:
    设数组长度为N。
    1.比较相邻的前后二个数据,如果前面数据大于后面的数据,就将二个数据交换。
    2.这样对数组的第0个数据到N-1个数据进行一次遍历后,最大的一个数据就“沉”到数组第N-1个位置。
    3.N=N-1,如果N不为0就重复前面二步,否则排序完成。

    方案一:

     1 function bubble1_sort($array){
     2     $count=count($array);
     3     if($count<=1){
     4         return $array;
     5     }
     6     for($i=0;$i<$count;$i++){
     7         for($j=0;$j<$count;$j++){
     8             if($array[$i]<$array[$j]){
     9                 $temp=$array[$i];
    10                 $array[$i]=$array[$j];
    11                 $array[$j]=$temp;
    12             }
    13         }
    14     }
    15     return $array;
    16 }

     方案二:

     1 function bubble2_sort($array){
     2     $count=count($array);
     3     if($count<=1){
     4         return $array;
     5     }
     6     
     7     for($i=0;$i<$count;$i++){
     8         for($j=1;$j<$count-$i;$j++){
     9             if($array[$j-1]>$array[$j]){
    10                 $temp=$array[$j-1];
    11                 $array[$j-1]=$array[$j];
    12                 $array[$j]=$temp;
    13             }
    14         }
    15     }
    16     return $array;
    17 }

    方案三:

    设置一个标志,如果这一趟发生了交换,则为true,否则为false。明显如果有一趟没有发生交换,说明排序已经完成。

     1 function bubble3_sort($array){
     2     $count=count($array);
     3     if($count<=1){
     4         return $array;
     5     }
     6     $flag=true;
     7     $j=$count;
     8     while($flag){
     9         $flag=false;
    10         for($i=1;$i<$j;$i++){
    11             if($array[$i-1]>$array[$i]){
    12                 $temp=$array[$i-1];
    13                 $array[$i-1]=$array[$i];
    14                 $array[$i]=$temp;
    15                 $flag=true;
    16             }
    17         }
    18         $j--;
    19         
    20     }
    21     return $array;
    22     
    23 }

    方案四:

    如果有100个数的数组,仅前面10个无序,后面90个都已排好序且都大于前面10个数字,那么在第一趟遍历后,最后发生交换的位置必定小于10,且这个位置之后的数据必定已经有序了,记录下这位置,第二次只要从数组头部遍历到这个位置就可以了。

    function bubble4_sort($array){
        $count=count($array);
        if($count<=1){
            return $array;
        }
        $flag=$count;
        while($flag>0){
            $k=$flag;
            $flag=0;
            for($j=1;$j<$k;$j++){
                if($array[$j-1]>$array[$j]){
                    $temp=$array[$j-1];
                    $array[$j-1]=$array[$j];
                    $array[$j]=$temp;
                    $flag=$j;
                }
            }
        }
        return $array;
    }

    方案五:

    function bubble_sort($array){
        $count=count($array);
        if($count<=1){
            return $array;
        }
        for($i=$count-1;$i>0;$i--){
            $flag=false;
            for($j=0;$j<$count;$j++){
                if($array[$j]>$array[$j+1]){
                    $temp=$array[$j];
                    $array[$j]=$array[$j+1];
                    $array[$j+1]=$temp;
                    $flag=true;
                }
            }
            if(!$flag)
                break;
        }
        return $array;
    }
  • 相关阅读:
    动态规划_leetcode416
    动态规划_leetcode377
    基础整理
    super使用简介
    PHP替换指定字符串
    yii安装redis扩展(Windows)
    PHP多维数组去重
    git pull
    vue页面部署并移除url里面的#号
    fatal: refusing to merge unrelated histories(git pull)
  • 原文地址:https://www.cnblogs.com/hylaz/p/4375483.html
Copyright © 2020-2023  润新知