I think I programmed a bottom-up merge sort, but I'm a little sceptical that it will work under any data set. By now, I've tested it with many random arrays of massive lengths, and it seems to work; the only thing making me doubt my function is that whenever I look online for a non-recursive one, all of the algorithms by other people are much longer than mine (in terms of the number of lines of code). This makes me suspect I have missed out something important form my function. Here is my work:
function mergeSort(list) {
list = list.map(n => [n]);
while(list.length > 1) {
list.push(merge(list[0], list[1]));
list.splice(0, 2);
}
return list[0];
}
the merge function is here:
function merge(lista, listb) {
let newList = new Array();
while(lista.length && listb.length) {
const listToProcess = lista[0] < listb[0] ? lista : listb;
newList.push(listToProcess[0]);
listToProcess.shift();
}
const listWithRemainingElmnts = lista.length ? lista : listb;
newList = newList.concat(listWithRemainingElmnts);
return newList;
}
Why are all of the online examples of bottom-up MergeSort so large compared to mine?