开发者社区> 问答> 正文

运行以下代码时遇到错误。错误消息是:[关闭]

function divide(collection) {
    if (collection.length < 2) {
        return collection
    } else {
        let midpoint = Math.floor(collection.length / 2);
        var left = collection.slice(0, midpoint);
        var right = collection.slice(midpoint, collection.length);
        console.log("Left subproblem is ", left);
        console.log("Right subproblem is ", right);
        let leftArr = divide(left);
        let rightArr = divide(right);

        return ChannelMergerNode(leftArr, rightArr)

    }
}

function merge(leftArr, rightArr) {
    console.log("Merging subproblems ", leftArr, rightArr);

    let sorted = [];
    while (leftArr.length > 0 && rightArr.length > 0) {
        console.log("Ccomparing first element of ", leftArr, rightArr);
        if (leftArr[0] < rightArr[0]) {
            let data = leftArr.shift();
            console.log("Pushing ", data);
            console.log("remaining right array", rightArr);
            sorted.push(data)
        }
    }
    console.log("Sorted Arr", sorted.concat(leftArr).concat(rightArr));
    return sorted.concat(leftArr).concat(rightArr);
}
divide([9,6,7,1,6,7,4,8,6]);

展开
收起
几许相思几点泪 2019-12-23 17:36:31 643 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
面向失败设计 立即下载
小程序 大世界 立即下载
《15分钟打造你自己的小程序》 立即下载