function merge(a, b) {
const result = []
let i = 0, j = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
result.push(a[i])
i++
} else {
result.push(b[j])
j++
}
}
while (i < a.length) {
result.push(a[i])
i++
}
while (j < b.length) {
result.push(b[j])
j++
}
return result
}
function msort(items) {
if (items.length < 2) {
return items
}
const a = msort(items.slice(0, Math.floor(items.length / 2)))
const b = msort(items.slice(Math.floor(items.length / 2)))
return merge(a, b)
}
// driver code
let a = [6,4,2]
let b = [5,3,1]
let c = merge(a, b)
console.log(c)
console.log(msort(c))
To embed this project on your website, copy the following code and paste it into your website's HTML: