Php 什么';这个选择排序代码有什么问题
我得到这个错误 PHP注意:未定义的偏移量:25英寸 /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义的偏移量:25英寸 /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义的偏移量:12 in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义的偏移量:12英寸 /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义索引:in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义索引:in /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义索引:in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义索引:in /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义索引:in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义索引:in /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义的偏移量:22 in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义的偏移量:22英寸 /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义索引:in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义索引:in /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义索引:in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义索引:in /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义的偏移量:11 in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义的偏移量:11英寸 /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义索引:in/private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义索引:in /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php为空 编辑:我知道了 我错误地使用$tmp作为数组的索引而不是值。Php 什么';这个选择排序代码有什么问题,php,algorithm,Php,Algorithm,我得到这个错误 PHP注意:未定义的偏移量:25英寸 /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 第23行的3.php 注意:未定义的偏移量:25英寸 /private/var/folders/83/98g8707d0f364z7m1pm76vlh0000gr/T/CodeRunner/Untitled 3.php第23行php通知:未定义的偏移量:12 in/private/var/f
<?php
// Selection Sort in PHP
$arr = array(64, 25, 12, 22, 11);
var_export(selection_sort($arr));
function selection_sort($arr) {
$len = count($arr);
for($i = 0; $i < $len; $i++) {
$min = $i;
for($j = $i + 1; $j < $len; $j++) {
if($arr[$j] < $arr[$min]) {
$min = $j;
}
swap($min, $i, $arr);
}
}
return $arr;
}
// pass by reference
function swap($i, $j, &$arr) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $arr[$tmp];
}
?>
将
$arr[$tmp]
更改为$tmp
,因为$tmp是值而不是键。预期的输出是什么?@Rizier123排序数组。啊,好的。我错误地使用$tmp作为数组的索引,而不是值。
<?php
// Selection Sort in PHP
$arr = array(64, 25, 12, 22, 11);
var_export(selection_sort($arr));
function selection_sort($arr) {
$len = count($arr);
for($i = 0; $i < $len; $i++) {
$min = $i;
for($j = $i + 1; $j < $len; $j++) {
if($arr[$j] < $arr[$min]) {
$min = $j;
}
swap($min, $i, $arr);
}
}
}
// pass by reference
function swap($i, $j, &$arr) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
}
?>