Skip to content

Instantly share code, notes, and snippets.

@AnwarShahriar
Created January 1, 2018 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnwarShahriar/51fc44737456284d06c674aa22ca8d95 to your computer and use it in GitHub Desktop.
Save AnwarShahriar/51fc44737456284d06c674aa22ca8d95 to your computer and use it in GitHub Desktop.
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