05c05-2.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 32 行

PHP
32
字号
<?php// Implement the Insertion Sortfunction insertion_sort(&$a) {    // Loop through all entries in the array    $count = count($a);    for ($i = 0; $i < $count; $i++) {        // Save the current value which we are going to compare:        $value = $a[$i];                // Now loop backwards from the current value until we reach the        //  beginning of the array or a value less than our current one        for ($x = $i - 1; ( ($x >= 0) && ($a[$x] > $value) ); $x--) {            // Swap this value down one place:            $a[$x + 1] = $a[$x];        }                // Now assign our value to it's proper sorted position:        $a[$x + 1] = $value;    }}        // Prepare an array of values:$values = array(73, 3, 42, 6, 14, 23, 15, 9, 74, 1, 234, 45, 23, 76, 12, 3);// Sort them:insertion_sort($values);// Echo them out in order://  1 3 3 6 9 12 14 15 23 23 42 45 73 74 76 234foreach ($values as $v) { echo "{$v} "; }?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?