从函数内部调用Javascript函数

时间:2021-01-13 16:02:50
function newsort(array) {
    if (array.length <= 1) {
        alert(array);
    } else {
        mid = array.length / 2;
        left = [];
        right = [];
        for (i = 0; i < mid; i++) {
            left.push(array[i]);
        }
        for (i = mid; i <= array.length; i++) {
            right.push(array[i]);
        }
    }
    result = merge(left, right);
    alert(result);
};

function merge(left, right) {
    alert(left);
}

function merge(arr1, arr2) {
    var arr3 = [];
    while (arr1.length > 0 && arr2.length > 0) {
        if (arr1[0] <= arr2[0]) {
            arr3.push(arr1.shift());
        } else {
            arr3.push(arr2.shift());
        }
    }

    while (arr1.length > 0) {
        arr3.push(arr1.shift());
    }

    while (arr2.length > 0) {
        arr3.push(arr2.shift());
    }
    //  time = timeSortingAlgorithm(arr3, newsort(array));
    //  return time;
    return arr3;
};

function makeRandomArray(size) {
    var result = Array();
    while (size > 0) {
        result.push(Math.floor(Math.random() * 100000));
        --size;
    }
    return result;
}

function timeSortingAlgorithm(array, sortingAlgorithm) {
    var startTime = new Date();
    newsort(array);
    var endTime = new Date();
    return endTime.getTime() - startTime.getTime();
}

var array = makeRandomArray(6);
newsort(array);
//timeSortingAlgorithm(array,newsort);

When I run this code, the idea is that it will take the function merge and send the two halves of the array to newsort - which then merges them together, sorted. The array is created by the makeRandomArray function. However, I cannot figure out how to incorporate the timeSortingAlgorithm(array, sortingAlgorithm) function, as when I call it - nothing happens. I have the calls commented out at the moment, but when I take those out - no output at all. If I leave that function out of the process altogether - everything works fine.

当我运行这段代码时,我们的想法是将函数merge合并并将数组的两半发送到newsort - 然后将它们合并在一起进行排序。该数组由makeRandomArray函数创建。但是,我无法弄清楚如何合并timeSortingAlgorithm(array,sortingAlgorithm)函数,就像我调用它一样 - 没有任何反应。我现在已经将这些电话注释掉了,但是当我把它们拿出来时 - 根本没有输出。如果我完全退出该功能 - 一切正常。

Anyone able to point me in the right direction?

有人能指出我正确的方向吗?

2 个解决方案

#1


1  

Three is a major problem with your implementation.

三是您实施的主要问题。

  1. If you are trying to implement Merge Sort, the array has to be split and the split parts have to be sorted before merging.
  2. 如果您尝试实现合并排序,则必须拆分数组,并且必须在合并之前对拆分部分进行排序。

Apart from that, the problems which I could find are

除此之外,我能找到的问题是

  1. You are using global variables, mid, left and right are global variables.

    您正在使用全局变量,mid,left和right是全局变量。

  2. You don't return the sorted arrays back from newsort function.

    您不会从newsort函数返回已排序的数组。

  3. You have two merge functions defined.

    您定义了两个合并函数。

  4. Your mid should be an integer, as you are using that for indexing an array and that will return undefined for floating point numbers.

    你的mid应该是一个整数,因为你使用它来索引一个数组,并且将返回未定义的浮点数。

So, the fixed implementation looks like this

因此,固定实现看起来像这样

function newsort(array) {
    if (array.length <= 1) {
        return array;
    } else {
        var mid   = parseInt(array.length / 2), left  = [], right = [];
        for (var i = 0; i < mid; i++) {
            left.push(array[i]);
        }
        for (i = mid; i < array.length; i++) {
            right.push(array[i]);
        }
    }
    return merge(newsort(left), newsort(right));
};

function merge(arr1, arr2) {
    var arr3 = [];
    while (arr1.length > 0 && arr2.length > 0) {
        if (arr1[0] <= arr2[0]) {
            arr3.push(arr1.shift());
        } else {
            arr3.push(arr2.shift());
        }
    }

    while (arr1.length > 0) {
        arr3.push(arr1.shift());
    }

    while (arr2.length > 0) {
        arr3.push(arr2.shift());
    }
    return arr3;
};

function timeSortingAlgorithm(array, sortingAlgorithm) {
    var startTime = new Date(), result = sortingAlgorithm(array);
    console.log(new Date().getMilliseconds() - startTime.getMilliseconds());
    return result;
}

var array = makeRandomArray(6);
console.log(array);
console.log(timeSortingAlgorithm(array, newsort));

#2


0  

You are having an infinite loop.

你有一个无限循环。

when you run this line :

当你运行这一行时:

time = timeSortingAlgorithm(arr3, newsort(array));

You call newsort.

你打电话给newsort。

newsort call the function merge which then run over, guess what, this line :

newsort调用函数merge然后运行,猜猜是什么,这行:

time = timeSortingAlgorithm(arr3, newsort(array));

#1


1  

Three is a major problem with your implementation.

三是您实施的主要问题。

  1. If you are trying to implement Merge Sort, the array has to be split and the split parts have to be sorted before merging.
  2. 如果您尝试实现合并排序,则必须拆分数组,并且必须在合并之前对拆分部分进行排序。

Apart from that, the problems which I could find are

除此之外,我能找到的问题是

  1. You are using global variables, mid, left and right are global variables.

    您正在使用全局变量,mid,left和right是全局变量。

  2. You don't return the sorted arrays back from newsort function.

    您不会从newsort函数返回已排序的数组。

  3. You have two merge functions defined.

    您定义了两个合并函数。

  4. Your mid should be an integer, as you are using that for indexing an array and that will return undefined for floating point numbers.

    你的mid应该是一个整数,因为你使用它来索引一个数组,并且将返回未定义的浮点数。

So, the fixed implementation looks like this

因此,固定实现看起来像这样

function newsort(array) {
    if (array.length <= 1) {
        return array;
    } else {
        var mid   = parseInt(array.length / 2), left  = [], right = [];
        for (var i = 0; i < mid; i++) {
            left.push(array[i]);
        }
        for (i = mid; i < array.length; i++) {
            right.push(array[i]);
        }
    }
    return merge(newsort(left), newsort(right));
};

function merge(arr1, arr2) {
    var arr3 = [];
    while (arr1.length > 0 && arr2.length > 0) {
        if (arr1[0] <= arr2[0]) {
            arr3.push(arr1.shift());
        } else {
            arr3.push(arr2.shift());
        }
    }

    while (arr1.length > 0) {
        arr3.push(arr1.shift());
    }

    while (arr2.length > 0) {
        arr3.push(arr2.shift());
    }
    return arr3;
};

function timeSortingAlgorithm(array, sortingAlgorithm) {
    var startTime = new Date(), result = sortingAlgorithm(array);
    console.log(new Date().getMilliseconds() - startTime.getMilliseconds());
    return result;
}

var array = makeRandomArray(6);
console.log(array);
console.log(timeSortingAlgorithm(array, newsort));

#2


0  

You are having an infinite loop.

你有一个无限循环。

when you run this line :

当你运行这一行时:

time = timeSortingAlgorithm(arr3, newsort(array));

You call newsort.

你打电话给newsort。

newsort call the function merge which then run over, guess what, this line :

newsort调用函数merge然后运行,猜猜是什么,这行:

time = timeSortingAlgorithm(arr3, newsort(array));