我想用Java来实现合并排序,以此作为一种学习体验。我有功能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()内部发生的情况:
步骤1
leftArray:[4,5,6,7,8,8]
rightArray:[1,2,3,5,9]
结果:[]
第2步
leftArray [4,5,6,7,8,8]
rightArray [2,3,5,9]
结果:[1]
第三步
(索引编制不当... array [0]返回两个值)
leftArray [0] = 4
rightArray [0] = 2,3
leftArray [5,6,7,8,8]
rightArray [2,3,5,9]
结果[1,4]
步骤4
(索引编制不当... array [0]返回两个值)
leftArray [0] = 5
rightArray [0] = 2,3
leftArray [5,6,7,8,8]
rightArray [5,9]
结果[1,4,2,3]
... array [0]索引再次拧紧,然后返回rightArray [0] = 5,9。怪异的部分是,如果我在mergeArray()上独立于leftArray = [4,5,6,7,8,8]和rightArray [1,2,3,5,9]调用我的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,Array 1,Array [2],Array 1,Array [2],
数组[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>