php算法排序大全,面试必看算法

1.冒泡排序

function sort($array) {
    $length=count($array);
    for ($i=$length;$i>0;$i--){
        $swape=true;
        for ($j=0;$j<$i-1;$j++){
            if ($array[$j]>$array[$j+1]){
                $temp=$array[$j];
                $array[$j]=$array[$j+1];
                $array[$j+1]=$temp;
                $swape=false;
            }
        }
        if ($swape)break;
    }

    return $array;
}

2.气泡排序

function sort($input)
{
    
    if(!isset($input))
    {
        return [];
    }

    do
    {
        $swapped = false;

        for($i = 0, $count = sizeof($input) - 1; $i < $count; $i++)
        {
            if($input[$i + 1] < $input[$i])
            {
                list($input[$i + 1], $input[$i]) = [$input[$i], $input[$i + 1]];
                $swapped = true;
            }
        }
    }
    while($swapped);

    return $input;
}

$array = [51158,1856,8459,67957,59748,58213,90876,39621,66570,64204,79935,27892,47615,94706,34201,74474,63968,4337,43688,42685,31577,5239,25385,56301,94083,23232,67025,44044,74813,34671,90235,65764,49709,12440,21165,20620,38265,12973,25236,93144,13720,4204,77026,42348,19756,97222,78828,73437,48208,69858,19713,29411,49809,66174,52526277,7515,7873,8350,28229,24105,76818,86897,18456,29373,7853,24932,93070,4696,63015,9358,28302,3938,11754,33679,18492,91503,63395,12029,23954,27230,58336,16544,23606,61349,37348,78629,96145,57954,32392,76201,54616,59992,5676,97799,47910,98758,75043,72849,6466,68831,2246,69091,22296,6506,93729,86968,39583,46186,96782,19074,46574,46704,99211,55295,33963,77977,86805,72686];

$array = sort($array);
echo implode(',', $array);

3.计数排序

function sort($array, $min, $max)
{
    $count = array();
    for($i = $min; $i <= $max; $i++)
    {
        $count[$i] = 0;
    }

    foreach($array as $number)
    {
        $count[$number]++;
    }
    $z = 0;
    for($i = $min; $i <= $max; $i++) {
        while( $count[$i]-- > 0 ) {
            $array[$z++] = $i;
        }
    }
    return $array;
}

4.插入排序

function sort($array){

    for ($i=1;$i<count($array);$i++){
        $currentVal=$array[$i];
        for ($j=$i-1;$j>=0&&$array[$j]>$currentVal;$j--){
          $array[$j+1]=$array[$j];
        }
        $array[$j+1]=$currentVal;
    }
    return $array;
}

5.快速排序

function sort($input)
{
   
    if(!isset($input))
    {
        return [];
    }

    $lt = [];
    $gt = [];

    if(sizeof($input) < 2)
    {
        return $input;
    }

    $key = key($input);
    $shift = array_shift($input);

    foreach($input as $value)
    {
        $value <= $shift ? $lt[] = $value : $gt[] = $value;
    }

    return array_merge(quickSort($lt), [$key => $shift], quickSort($gt));
}


$array = [51158,1856,8459,67957,59748,58213,90876,39621,66570,64204,79935,27892,47615,94706,34201,74474,63968,4337,43688,42685,31577,5239,25385,56301,94083,23232,67025,44044,74813,34671,90235,65764,49709,12440,21165,20620,38265,12973,25236,93144,13720,4204,77026,42348,19756,97222,78828,73437,48208,69858,19713,29411,49809,3015,9358,28302,3938,11754,33679,18492,91503,63395,12029,23954,27230,58336,16544,23606,61349,37348,78629,96145,57954,32392,76201,54616,59992,5676,97799,47910,98758,75043,72849,6466,68831,2246,69091,22296,6506,93729,86968,39583,46186,96782,19074,46574,46704,99211,55295,33963,77977,86805,72686];

$array = quickSort($array);
echo implode(',', $array);

6.选择排序

function sort($array){
    $length=count($array);
    for ($i=0;$i<$length;$i++){
        $lowest=$i;
        for ($j=$i+1;$j<$length;$j++){
            if ($array[$j] < $array[$lowest]){
                $lowest=$j;
            }
        }
        if ($i !==$lowest){
            $temp=$array[$i];
            $array[$i]=$array[$lowest];
            $array[$lowest]=$temp;
        }

    }
    return $array;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_37540251

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值