gpt4 book ai didi

javascript - 合并排序 : merge function array index zero returning multiple indexes

转载 作者:行者123 更新时间:2023-11-30 12:17:25 25 4
gpt4 key购买 nike

我想用 Javascript 实现归并排序作为一种学习体验。我有函数 mergeSort(unsortedArray),它接受一个未排序的数组并使用合并排序策略对其进行排序。 mergeSort() 调用 merge(leftArray,rightArray),它将两个已排序的数组合并在一起,生成一个已排序的数组。

我认为问题出在 merge() 函数上。在数组上调用 mergeSort 时:[8,8,7,5,4,6,3,2,1,5,9,8,7,6,5,4,2,3,6,5,4, 8] 我得到结果:[1,4,2,3,5,5,9,6,7,8,8]。据我所知,问题的根源是在 merge() 函数中,在比较 leftArray[0] 和 rightArray[0] 时,rightArray[0] 有时会返回多个值,而不仅仅是第一个索引.在我的例子中,它是用 2,3 和 5,9 来做的。所以代码运行时,有时rightArray[0] = 2,3,2,3拼接出数组后rightArray[0]=5,9。以下是出现此问题时 merge() 中发生的情况:

第一步

左数组:[4,5,6,7,8,8]
rightArray:[1,2,3,5,9]
结果:[]

第二步

左数组[4,5,6,7,8,8]
rightArray[2,3,5,9]
结果:[1]

第三步

(不正确的索引...array[0] 返回两个值)
左数组[0]=4
rightArray[0]=2,3

左数组[5,6,7,8,8]
rightArray[2,3,5,9]
结果[1,4]

第四步

(不正确的索引...array[0] 返回两个值)
左数组[0]=5
rightArray[0]=2,3

左数组[5,6,7,8,8]
rightArray[5,9]
结果[1,4,2,3]

...array[0] 索引再次搞砸,接下来返回 rightArray[0] = 5,9。奇怪的是,如果我在 leftArray=[4,5,6,7,8,8] 和 rightArray[1,2,3,5,9] 上独立于 mergeSort() 调用我的 merge() 函数,它工作正常并且返回正确的结果,没有奇怪的索引行为。

//Implement Merge Sort...
function mergeSort(unsortedArray) {
var leftArray = [];
var rightArray = [];
var result = [];

//Base Case of one element
if(unsortedArray.length <= 1){
//alert("Array is size 1 and value: " + unsortedArray);
return unsortedArray;
}
else{
var halfwayPoint = Math.round(unsortedArray.length/2);

//Sepertate unsortedArray into a left and right array
for(var i = 0; i < halfwayPoint; i++){
leftArray.push(unsortedArray[i]);
//alert("leftArray: "+ leftArray + " index i = " + i);
}
for(var i = halfwayPoint; i < unsortedArray.length; i++){
rightArray.push(unsortedArray[i]);
//alert("rightArray" + rightArray + " index i = " + i);
}
//alert("leftArray: " + leftArray + " rightArray: " + rightArray);
leftArray = mergeSort(leftArray);
rightArray = mergeSort(rightArray);
//alert("Arrays before merge = leftArray: " + leftArray + " rightArray: " + rightArray);
result = merge(leftArray, rightArray);
//alert("result: " + result);
}
return result;
}

//Helper function Merge for MergeSort
function merge(leftArray, rightArray)
{
var result = [];
while(leftArray.length > 0 && rightArray.length > 0){
//compare first items of both lists
//alert("top of while loop");
//alert("leftArray[0] = " + leftArray[0] + " rightArray[0] = " + rightArray[0]);
if(leftArray[0] >= rightArray[0]){
result.push(rightArray[0]);
//alert("result after push rightArray[0] " + result + " and rightArray before splice: "+ rightArray);
rightArray.splice(0,1);
//alert("rightArray after splce: " + rightArray);
}
else{
result.push(leftArray[0]);
//alert("result after push leftArray[0] " + result + " and leftArray before splice: "+ leftArray);
leftArray.splice(0,1);
//alert("leftArray after splce: " + leftArray);
}
}
//alert("before leftArray add");
if(leftArray.length > 0){
//alert("went into left array > 0 leftArray: " + leftArray);
result.push(leftArray);
}
//alert("before rightArray add");
if(rightArray.length > 0){
//alert("went into right array > 0 rightArray: " + rightArray);
result.push(rightArray);
}
//alert("result within merge function: " + result);
return result;
}
//Test Case
var unsortedArray = [8,8,7,5,4,6,3,2,1,5,9,8,7,6,5,4,2,3,6,5,4,8];
var sortedArray = mergeSort(unsortedArray);
lert(sortedArray);

//Problem is when Merge sort has left array and right array described below
//the merge function will yield proper result on left array and right array
//if called directly as it is below, however when merge is called through
//mergeSort with leftArray and rightArray as described below it yields
// improperResult below
var leftArray = [4,5,6,7,8,8];
var rightArray = [1,2,3,5,9];
var improperResult= [1,4,2,3,5,5,9,6,7,8,8];
var resultAct = merge(leftArray,rightArray);
alert(resultAct);
<h1>MergeSort Problem</h1>

最佳答案

您需要使用 Array.prototype.concat()而不是 .push() 来连接 2 个 arrays.

.concat 合并 2 个(或更多)数组并返回一个新的 array,而 push 只是将目标放在 array 的末尾>,它不会为您连接数组。

如果您记录原始结果而不是警报,您会看到

[1, 2, 3, 4, 4, Array[2], 5, Array1, Array[2], Array1, Array[2], Array[4]]

很明显,您只是将数组插入了结果。

所以在你的

if(leftArray.length > 0){
result.push(leftArray);
}
if(rightArray.length > 0){
result.push(rightArray);
}

你应该写信给:

if(leftArray.length > 0){
result = result.concat(leftArray);
}
if(rightArray.length > 0){
result = result.concat(rightArray);
}

    function mergeSort(unsortedArray) {
var leftArray = [];
var rightArray = [];
var result = [];

//Base Case of one element
if(unsortedArray.length <= 1){
return unsortedArray;
}
else{
var halfwayPoint = Math.round(unsortedArray.length/2);

//Sepertate unsortedArray into a left and right array
for(var i = 0; i < halfwayPoint; i++){
leftArray.push(unsortedArray[i]);
}
for(var i = halfwayPoint; i < unsortedArray.length; i++){
rightArray.push(unsortedArray[i]);
}

leftArray = mergeSort(leftArray);
rightArray = mergeSort(rightArray);

result = merge(leftArray, rightArray);
}
return result;
}

//Helper function Merge for MergeSort
function merge(leftArray, rightArray)
{
var result = [];

while(leftArray.length > 0 && rightArray.length > 0){
//compare first items of both lists
if(leftArray[0] >= rightArray[0]){
result.push(rightArray[0]);
rightArray.splice(0,1);
}
else{
result.push(leftArray[0]);
leftArray.splice(0,1);
}
}

if(leftArray.length > 0){
result = result.concat(leftArray);
}
if(rightArray.length > 0){
result = result.concat(rightArray);
}

return result;
}
//Test Case
var unsortedArray = [8,8,7,5,4,6,3,2,1,5,9,8,7,6,5,4,2,3,6,5,4,8];
var sortedArray = mergeSort(unsortedArray);
alert(sortedArray);

//Problem is when Merge sort has left array and right array described below
//the merge function will yield proper result on left array and right array
//if called directly as it is below, however when merge is called through
//mergeSort with leftArray and rightArray as described below it yields
// improperResult below
var leftArray = [4,5,6,7,8,8];
var rightArray = [1,2,3,5,9];
var improperResult= [1,4,2,3,5,5,9,6,7,8,8];
var resultAct = merge(leftArray,rightArray);
alert(resultAct);
<h1>MergeSort Problem</h1>

关于javascript - 合并排序 : merge function array index zero returning multiple indexes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32073950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com