选择排序–php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


function (array $item) {
$returnItem = [];
$len = count($item);
for($j = 0; $j < $len; $j++) {
$smallest_index = findSmallest($item);
array_push($returnItem, $item[$smallest_index]);
array_splice($item, $smallest_index, 1);
// var_dump($item);
}
return $returnItem;
}

// 找出数组中最小数的索引
function findSmallest(array $item) {
$smallest_index = 0;
$len = count($item);
for ($i = 1; $i < $len; $i++) {
if ($item[$i] < $item[$smallest_index]) {
$smallest = $item[$i];
$smallest_index = $i;
}
}
return $smallest_index;
}

$testItem = array(3, 8, 1, 5, 9, 4, 7, 2, 6, 22, -1, 12313, 444, 55, 3233);
$return = selectSort($testItem);
var_dump($return);