You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I implemented merge sort. However, it would not pass the test my code is below. I change the return output of merge_sort from a void to a numbers array.
function merge(arr1: number[], arr2: number[]): number[] {
let results = [];
let i = 0;
let j = 0;
while (i < arr1.length && j < arr2.length) {
if (arr2[j] > arr1[i]) {
results.push(arr1[i]);
i++;
} else {
results.push(arr2[j]);
j++;
}
}
while (i < arr1.length) {
results.push(arr1[i]);
i++;
}
while (j < arr2.length) {
results.push(arr2[j]);
j++;
}
return results;
}
export default function merge_sort(arr: number[]): number[] {
if (arr.length <= 1) return arr;
let mid = Math.floor(arr.length / 2);
let left = merge_sort(arr.slice(0, mid));
let right = merge_sort(arr.slice(mid));
return merge(left, right);
}
The original stub of code.
export default function merge_sort(arr: number[]): void {
}
merge sort is in memory sort, you are given a buffer of memory, you can sort it in place, then that object has changed and the original test works, also at a glance I am not positive you have solved merge sort properly anyway
I implemented merge sort. However, it would not pass the test my code is below. I change the return output of merge_sort from a void to a numbers array.
The original stub of code.
I changed the test code from this
To this and it passed.
I am not sure if the test is wrong or if my implementation is incorrect.
The text was updated successfully, but these errors were encountered: