05c05-3.php

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

PHP
54
字号
<?php// Implement the Shell Sortfunction shell_sort(&$a) {    $count = count($a);    // Determine in our sequence of 2x + 1, what the highest value that is    //  less than the count of the array is:    $columns = 1;    while ($columns < $count) {        $columns = $columns * 2 + 1;    }        // Ok, Columns went one too far that way, so step back one    $columns = ($columns - 1) / 2;    // Now, as long as our number of columns to sort by is at LEAST 1    while ($columns > 0) {        // Loop for as many columns as we have:        for ($c = 0; $c < $columns; $c++) {            // Loop through all members of this column:            for ($i = $columns; $i < $count; $i += $columns) {                // Save the current value which we are going to compare:                $value = $a[$i];                        // Loop backwards from the current value until we reach the                //  start of the array or a value less than our current one                for ($x = $i - $columns;                         ( ($x >= 0) && ($a[$x] > $value) );                         $x -= $columns) {                    // Swap this value down one place:                    $a[$x + $columns] = $a[$x];                }                        // Now assign our value to it's proper sorted position:                $a[$x + $columns] = $value;            }        }        // Now that we have finished with one set of columns        //  reduce the number of columns:        $columns = ($columns - 1) / 2;    }}        // 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:shell_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 + -
显示快捷键?