function divide(collection) {
if (collection.length < 2) {
return collection
} else {
let midpoint = Math.floor(collection.length / 2);
var left = collection.slice(0, midpoint);
var right = collection.slice(midpoint, collection.length);
console.log("Left subproblem is ", left);
console.log("Right subproblem is ", right);
let leftArr = divide(left);
let rightArr = divide(right);
return ChannelMergerNode(leftArr, rightArr)
}
}
function merge(leftArr, rightArr) {
console.log("Merging subproblems ", leftArr, rightArr);
let sorted = [];
while (leftArr.length > 0 && rightArr.length > 0) {
console.log("Ccomparing first element of ", leftArr, rightArr);
if (leftArr[0] < rightArr[0]) {
let data = leftArr.shift();
console.log("Pushing ", data);
console.log("remaining right array", rightArr);
sorted.push(data)
}
}
console.log("Sorted Arr", sorted.concat(leftArr).concat(rightArr));
return sorted.concat(leftArr).concat(rightArr);
}
divide([9,6,7,1,6,7,4,8,6]);