图解算法第二章选择排序使用PHP语言实现,仅供参考,错误之处,欢迎联系我纠正
<?php function selectionSort($list) { $length = count($list); $result = []; for( $i = 0; $i < $length ; $i++ ) { $minIndex = 0; $min = $list[ $minIndex ]; foreach($list as $key => $value) { if( $value < $min ){ $min = $value; $minIndex = $key; } } $result[] = $min; unset($list[ $minIndex ]); $list = array_values($list); } return $result; } print_r(selectionSort([1, 2, 34, 4, 5, 66, 22, 8, 534, 23, 11, 12, 43, 14, 13, 16, 17, 32, 41, 32])); print_r(selectionSort([1, 2, 3])); print_r(selectionSort([1])); print_r(selectionSort([])); ?>