I implemented the following mergesort algorithm in JavaScript, but I want to know your opinion about if is possible to do better than this.
function mergesort(list){
if(list.length < 2){
return list
}else if(list.length > 1){
let n = list.length
let middle = n/2
return merge(mergesort(list.slice(0, middle)), mergesort(list.slice(middle, n)))
}
}
function merge(listA, listB){
let list = []
listA.push(Infinity)
listB.push(Infinity)
let i = 0
let j = 0
while(i < listA.length-1 || j < listB.length-1){
if(listA[i] < listB[j]){
list.push(listA[i])
i++
}else{
list.push(listB[j])
j++
}
}
return list
}
Do you have any idea of how can I improve this?
if(list.length < 2) ... else if(list.length > 1) ...
You can turn the else if into just an else since all lists have a length bigger than 1 if they are not less than 2 big. \$\endgroup\$