← 返回博客
算法2017-02-26 09:34:361 分钟 · 74 7273

选择排序的php实现

选择排序的php代码实现

#php#算法
https://github.com/ningbnii/suanfatujie 
function findSmallest($arr){
	$smallest = $arr[0];
	$smallestIndex = 0;
	foreach ($arr as $key => $value) {
		if($value < $smallest){
			$smallest = $value;
			$smallestIndex = $key;
		}
	}
	return $smallestIndex;
}

function selectionSort($arr){
	$newArr = [];
	foreach ($arr as $key => $value) {
		$smallest = findSmallest($arr);
		$newArr[] = $arr[$smallest];
		array_splice($arr, $smallest, 1);
	}
	return $newArr;
}

print_r(selectionSort([5,3,6,2,10]));

相关推荐

本文为原创文章,采用CC BY-NC-SA 4.0协议授权,转载请保留署名与原文链接。原文链接:https://www.wxbuluo.com/article/23