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]);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。