Skip to content

Instantly share code, notes, and snippets.

@Tlaloc-Es
Last active August 16, 2019 15:41
Show Gist options
  • Save Tlaloc-Es/153ad2e7dd93c0010bf8905a470b29cd to your computer and use it in GitHub Desktop.
Save Tlaloc-Es/153ad2e7dd93c0010bf8905a470b29cd to your computer and use it in GitHub Desktop.
function mergesort(list){
if(list.length > 1){
let middle = list.length/2
return merge(mergesort(list.slice(0, middle)), mergesort(list.slice(middle, list.length)))
}else{
return list
}
}
function merge(listA, listB){
if(listA.length === 0){return listB}
if(listB.length === 0){return listA}
if(listA[0] < listB[0]){
return [listA[0]].concat(merge(listA.slice(1, listA.length), listB))
}else{
return [listB[0]].concat(merge(listA, listB.slice(1, listB.length)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment