无法找出合并排序中的错误
Unable to figure out the bug in merge sort
我正在尝试构建一个排序可视化工具,当我编写合并排序算法时,returning 数组的长度始终为 1,我的算法看起来非常好,但我不知道是什么让它成为return 单一长度数组元素,我确实尝试使用控制台语句进行调试,在进入递归之前一切似乎都很好。
我的主要 app.js 代码在这里:
testSortingAlgo() {
//here we are creating 100 arrays for checking the sorting algorithms
for (let i = 0; i < 100; i++) {
const array = [];
//creating different lengths of the array
const length = randomIntFromInterval(1, 1000);
console.log(length);
for (let j = 0; j < length; j++) {
//push all collection of negative and positive numbers
array.push(randomIntFromInterval(-1000, 1000));
}
console.log(array.length);
console.log(array.slice().length);
//for sorting in javascript inbuilt function .we are passing (a, b) => (a - b) because inbult function
// checks from starting character so in case of (200,5) 5 is greater as first char of 5 is greater than
// first char of 200 that is 2
const javascriptSortedArray = array.slice().sort((a, b) => (a - b))
const SortedArray = sortingAlgorithms.mergeSort(array.slice());
console.log(arraysAreEqual(javascriptSortedArray, SortedArray));
}
}
在这个函数中,我正在检查我的排序算法的有效性。当我将 array.slice()
传递给合并排序时。该数组正在以下代码中进行操作
export const mergeSort = array => {
// for (let i = 0; i < array.length; i++) {
// console.log(array[i]);
if (array.length <= 1) return array;
const middleIdx = Math.floor(array.length / 2);
const firstHalf = mergeSort(array.slice(0, middleIdx));
const secondHalf = mergeSort(array.slice(middleIdx, array.length));
const sortedArray = [];
let i = 0, j = 0;
while (i < firstHalf.length && j < secondHalf.length) {
if (firstHalf[i] < secondHalf[j]) {
sortedArray.push(firstHalf[i++]);
} else {
sortedArray.push(secondHalf[j++]);
}
}
while (i < firstHalf.length) array.push(firstHalf[i++]);
while (j < secondHalf.length) array.push(secondHalf[j++]);
return sortedArray;
}
它是 return单个数组元素。
我想你会为此而自责,我过去做过很多次。
while(i<firstHalf.length) array.push(firstHalf[i++]);
while(j<secondHalf.length) array.push(secondHalf[j++]);
我认为你打算推送到 sortedArray
已处理此 repl https://repl.it/repls/BisqueNewMultitasking#index.js
我正在尝试构建一个排序可视化工具,当我编写合并排序算法时,returning 数组的长度始终为 1,我的算法看起来非常好,但我不知道是什么让它成为return 单一长度数组元素,我确实尝试使用控制台语句进行调试,在进入递归之前一切似乎都很好。
我的主要 app.js 代码在这里:
testSortingAlgo() {
//here we are creating 100 arrays for checking the sorting algorithms
for (let i = 0; i < 100; i++) {
const array = [];
//creating different lengths of the array
const length = randomIntFromInterval(1, 1000);
console.log(length);
for (let j = 0; j < length; j++) {
//push all collection of negative and positive numbers
array.push(randomIntFromInterval(-1000, 1000));
}
console.log(array.length);
console.log(array.slice().length);
//for sorting in javascript inbuilt function .we are passing (a, b) => (a - b) because inbult function
// checks from starting character so in case of (200,5) 5 is greater as first char of 5 is greater than
// first char of 200 that is 2
const javascriptSortedArray = array.slice().sort((a, b) => (a - b))
const SortedArray = sortingAlgorithms.mergeSort(array.slice());
console.log(arraysAreEqual(javascriptSortedArray, SortedArray));
}
}
在这个函数中,我正在检查我的排序算法的有效性。当我将 array.slice()
传递给合并排序时。该数组正在以下代码中进行操作
export const mergeSort = array => {
// for (let i = 0; i < array.length; i++) {
// console.log(array[i]);
if (array.length <= 1) return array;
const middleIdx = Math.floor(array.length / 2);
const firstHalf = mergeSort(array.slice(0, middleIdx));
const secondHalf = mergeSort(array.slice(middleIdx, array.length));
const sortedArray = [];
let i = 0, j = 0;
while (i < firstHalf.length && j < secondHalf.length) {
if (firstHalf[i] < secondHalf[j]) {
sortedArray.push(firstHalf[i++]);
} else {
sortedArray.push(secondHalf[j++]);
}
}
while (i < firstHalf.length) array.push(firstHalf[i++]);
while (j < secondHalf.length) array.push(secondHalf[j++]);
return sortedArray;
}
它是 return单个数组元素。
我想你会为此而自责,我过去做过很多次。
while(i<firstHalf.length) array.push(firstHalf[i++]);
while(j<secondHalf.length) array.push(secondHalf[j++]);
我认为你打算推送到 sortedArray
已处理此 repl https://repl.it/repls/BisqueNewMultitasking#index.js