Skip to content

Instantly share code, notes, and snippets.

@4skinSkywalker
Last active April 8, 2019 12:45
Show Gist options
  • Select an option

  • Save 4skinSkywalker/f71b17e81bfcb8ecd3c3efb5d32571dd to your computer and use it in GitHub Desktop.

Select an option

Save 4skinSkywalker/f71b17e81bfcb8ecd3c3efb5d32571dd to your computer and use it in GitHub Desktop.
function mergeSort(array) {
let len = array.length
let b = 1
while (b < len) {
for (let i = 0; i + b < len; i += b * 2) {
let l = i
let r = i + b
let left = array.slice(l, l+b)
let right = array.slice(r, r+b)
let j = i
for (let item of merge(left, right)) // merge is the function we defined earlier
array[j++] = item
}
b *= 2
}
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment