美文网首页
选择排序,php实现

选择排序,php实现

作者: designer | 来源:发表于2020-05-06 13:51 被阅读0次
<?php
/**
 * Created by PhpStorm.
 * User: jiaoyang
 * Date: 2019/8/6
 * Time: 下午10:57
 */

namespace app\index\controller;


use think\Controller;

class SelectSort extends  Controller
{
    public function  sort(){
        $arr = [90,10,50];
        print_r($this->selectSort($arr));

    }

    public  function selectSort($arr){
        $len = count($arr);
        for($i = 0;$i<$len-1;$i++){
            $minIndex = $i;
            for ($j = $i+1;$j<$len;$j++){
                if($arr[$minIndex]>$arr[$j]){
                    $minIndex = $j;
                }
            }
            if($minIndex!=$i){
                $this->swap($arr[$i],$arr[$minIndex]);
            }
        }
        return $arr;
    }


    public function swap(&$a,&$b){
        if($a>$b){
            $tmp = $a;
            $a = $b;
            $b = $tmp;
        }
    }
}```

相关文章

网友评论

      本文标题:选择排序,php实现

      本文链接:https://www.haomeiwen.com/subject/vmsayctx.html