php 冒泡排序

<?php

     function choose_style($arr, $index, $method = true)

    {

                 if($method == true)

       {

                    $result = $arr[$index] > $arr[$index+1];

                }else{

                    $result = $arr[$index] < $arr[$index+1];

                }

                    if($result){

        $temp = $arr[$index];

          $arr[$index] = $arr[$index + 1];

        $arr[$index+1] = $temp;

        } 

           }

  function bubble_sort($arr,$method)

  {    

      if(!is_array($arr)){

        echo "此函数参数必须是一维数组!";

        return; 

      }

      $len = count($arr);

      for($i = 0;$i < $len - 1; $i++)

      {

        for($j = 0; $j < $len - 1 - $i)

        {

          choose_style($arr,$j,$method));

        }

      }

      return $arr;

  }

  $arr = [1,2,3,1,34,132,1,411,132,54];

  bubble_sort($arr);

原文地址:https://www.cnblogs.com/hiraeth/p/8777740.html