微信公众号定制开发

选择排序的php实现
<pre class="prettyprint lang-js"><a href="https://github.com/ningbnii/suanfatujie" target="_blank">https://github.com/ningbnii/suanfatujie</a> </pre> <pre class="prettyprint lang-js">function findSmallest($arr){ $smallest = $arr[0]; $smallestIndex = 0; foreach ($arr as $key =&gt; $value) { if($value &lt; $smallest){ $smallest = $value; $smallestIndex = $key; } } return $smallestIndex; } function selectionSort($arr){ $newArr = []; foreach ($arr as $key =&gt; $value) { $smallest = findSmallest($arr); $newArr[] = $arr[$smallest]; array_splice($arr, $smallest, 1); } return $newArr; } print_r(selectionSort([5,3,6,2,10]));</pre>

上一篇:二分查找

下一篇:没有更多文章了