Created
January 1, 2018 14:14
-
-
Save AnwarShahriar/51fc44737456284d06c674aa22ca8d95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function merge(a, b) { | |
let i = 0; | |
let j = 0; | |
let k = 0; | |
let arr = []; | |
while (i < a.length && j < b.length) { | |
if (a[i] <= b[j]) { | |
arr[k] = a[i]; | |
i++; | |
} else { | |
arr[k] = b[j]; | |
j++; | |
} | |
k++; | |
} | |
while (i < a.length) { | |
arr[k] = a[i]; | |
k++; | |
i++; | |
} | |
while (j < b.length) { | |
arr[k] = b[j]; | |
k++; | |
j++; | |
} | |
console.log(`merge: ${arr}`); | |
return arr; | |
} | |
function devide(a) { | |
console.log(`devide: ${a}`); | |
if (a.length == 1 || a.length == 0) { | |
return a; | |
} | |
let leftArr = a.slice(0, Math.floor(a.length/2)); | |
let rightArr = a.slice(Math.floor(a.length/2), a.length); | |
return merge(devide(leftArr), devide(rightArr)); | |
} | |
console.log(`sorted: ${devide([])}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment