php - 为什么我的就地快速排序算法会出现 undefined index 错误?

标签 php algorithm

<分区>

<?php

$arr = array('1', '-1', '3', '-2');

function inPlaceQuicksort($arr){
    inPlaceQuickSortProcessing($arr, 0, count($arr));
    return $arr;
}

function inPlaceQuickSortProcessing(&$arr, $pivotIndex, $arraySize){

    if($pivotIndex + 1>= $arraySize)
        return;
    else{
        for($i = $pivotIndex + 1; $i++; $i<$arraySize){

            if($arr[$i] < $arr[$pivotIndex]){
                $temp = $arr[$i];
                unset($arr[$i]);
                array_values($arr);
                array_splice($arr, $pivotIndex, 0, $temp);
                $pivotIndex++;
            }
        }                
    }
    //inPlaceQuickSortProcessing($arr, 0, $pivotIndex+1);
    //inPlaceQuickSortProcessing($arr, $pivotIndex+1, $arraySize);            
}

inPlaceQuicksort($arr);


?>

for 循环中的最后一个表达式是否确保 $i<$arraySize

最佳答案

for($i = $pivotIndex + 1; $i++; $i<$arraySize){

应该是

for($i = $pivotIndex + 1; $i<$arraySize; $i++){

关于php - 为什么我的就地快速排序算法会出现 undefined index 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15181149/

相关文章:

php - CakePHP:无法识别 hasMany 关联

c++ - 位摆弄黑客 : most efficient way to remove one bit every n bits?

c# - 寻找最佳线环绕宽度以最大化垂直相同值邻居的数量

c - 找到两首或多首歌曲交集的算法

algorithm - 如何在算法中使用线性代数?

javascript - AJAX 针对单个 anchor 元素

PHP switch case 多个值的情况

用于处理多个连接的 PHP 脚本

php - SSL 连接错误无法发送 tlsv1.2

algorithm - 如何找到无向图中从s(任意起始顶点)到v(任意顶点)的最短路径是否唯一?