Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中构建mergesort时的无限循环_Javascript_Mergesort - Fatal编程技术网

在JavaScript中构建mergesort时的无限循环

在JavaScript中构建mergesort时的无限循环,javascript,mergesort,Javascript,Mergesort,我试图构建一个猴子补丁版本的mergeSort,但每次都会出错。我已经运行了几次调试器,看起来一切都在正确排序,直到最后一步,我跳转到loader.js文件中的一行 有人能帮我查一下吗?提前谢谢 Array.prototype.mergeSort = function(callback) { if (this.length <= 1) return this; if (!callback) { callback = function(x, y) {

我试图构建一个猴子补丁版本的
mergeSort
,但每次都会出错。我已经运行了几次调试器,看起来一切都在正确排序,直到最后一步,我跳转到loader.js文件中的一行

有人能帮我查一下吗?提前谢谢


Array.prototype.mergeSort = function(callback) {
    if (this.length <= 1) return this;

    if (!callback) {
        callback = function(x, y) {
            if (x > y) return -1;
            else if (x < y) return 1;
            else return 0;
        };
    }

    const mid = Math.floor(this.length / 2);
    const sortedLeft = this.slice(0, mid).mergeSort(callback);
    const sortedRight = this.slice(mid).mergeSort(callback);

    return sortedLeft.merge(sortedRight, callback);
};

Array.prototype.merge = function(arr, callback) {
    let merged = [];

    while (this.length > 0 || arr.length > 0) {
        if (callback(this[0], arr[0]) < 0) {
            merged.push(arr.shift());
            break;
        } else if (callback(this[0], arr[0]) >= 0) {
            merged.push(this.shift());
            break;
        }
    }

    merged = merged.concat(this);
    merged = merged.concat(arr);

    return merged;
};


Array.prototype.mergeSort=函数(回调){
如果(此长度y)返回-1;
如果(x0 | | arr.length>0){
if(回调(此[0],arr[0])<0){
合并。推(arr.shift());
打破
}else if(回调(this[0],arr[0])>=0){
merged.push(this.shift());
打破
}
}
merged=merged.concat(此);
合并=合并的.concat(arr);
返回合并;
};

列表为空时,合并循环应停止:而不是
而(this.length>0 | | arr.length>0)
您应编写:

while (this.length > 0 && arr.length > 0)
此外,在每次存储到
合并
数组后,不应
从循环中断开
,两次比较元素是多余的

以下是更正的版本:

Array.prototype.merge = function(arr, callback) {
    let merged = [];

    while (this.length > 0 && arr.length > 0) {
        if (callback(this[0], arr[0]) < 0) {
            merged.push(arr.shift());
        } else {
            merged.push(this.shift());
        }
    }
    merged = merged.concat(this);
    return merged.concat(arr);
};

旁注;您的第一个
回调=…
部分实际上与
返回y-x相同第二个侧注<代码>}else if(回调(this[0],arr[0])>=0){
与第一个if配对时只是一个else语句。在这两个计数上都理解了,但由于某些原因仍然存在问题。@Taplar:不太正确,例如
返回y-x
不处理字符串,而OP的默认比较函数则处理字符串。@chqrlie如果值是字符串,则减法不合适首先,应该使用本机
localeCompare
Array.prototype.mergeSort = function(callback) {
    if (this.length <= 1)
        return this;

    const mid = this.length >> 1;
    const sortedLeft = this.slice(0, mid).mergeSort(callback);
    const sortedRight = this.slice(mid).mergeSort(callback);

    return sortedLeft.merge(sortedRight, callback);
};

Array.prototype.merge = function(arr, callback) {
    let merged = [];

    if (callback) {
        while (this.length > 0 && arr.length > 0) {
            if (callback(this[0], arr[0]) <= 0) {
                merged.push(this.shift());
            } else {
                merged.push(arr.shift());
            }
        }
    } else {
        while (this.length > 0 && arr.length > 0) {
            if (this[0] <= arr[0]) {
                merged.push(this.shift());
            } else {
                merged.push(arr.shift());
            }
        }
    }
    return merged.concat(this).concat(arr);
};