You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
61 lines
1.3 KiB
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
|
|
class TestController extends BaseController
|
|
{
|
|
/**
|
|
*冒泡排序
|
|
*/
|
|
public function bubble_sort()
|
|
{
|
|
$arr = [22, 69, 10];
|
|
$d = $this->quick_sort($arr);
|
|
dd($d);
|
|
// $length = count($arr);
|
|
// for ($i = 0; $i < $length; $i++) {
|
|
// for ($j = $i+1; $j < $length; $j++) {
|
|
// if($arr[$i] > $arr[$j]){
|
|
// $res = $arr[$i];
|
|
// $arr[$i] = $arr[$j];
|
|
// $arr[$j] = $res;
|
|
// }
|
|
// }
|
|
// }
|
|
// dd($arr);
|
|
}
|
|
|
|
/**
|
|
*快速排序
|
|
*/
|
|
public function quick_sort($arr)
|
|
{
|
|
try {
|
|
|
|
if (!isset($arr[1])) {
|
|
return $arr;
|
|
}
|
|
$left = array();
|
|
$right = array();
|
|
$first = $arr[0];
|
|
foreach ($arr as $v) {
|
|
if ($v < $first) {
|
|
$left[] = $v;
|
|
} else {
|
|
$right[] = $v;
|
|
}
|
|
}
|
|
|
|
$left = $this->quick_sort($left);
|
|
$right = $this->quick_sort($right);
|
|
$left[] = $first;
|
|
|
|
}catch(\Exception $e) {
|
|
dd($e);
|
|
}
|
|
return array_merge($left, $right);
|
|
}
|
|
|
|
}
|