3 ms·
today i coded franzSort, faster than mergeSort, slower than quickSort function franzSort(a) { function m(l, r) { let s = []; while (l.lengt
by franze 2y ago
today i coded franzSort, faster than mergeSort, slower than quickSort
function franzSort(a) {
function m(l, r) {
let s = [];
while (l.length && r.length) {
if (l[0] <= r[0]) {
s.push(l.shift());
} else {
s.push(r.shift());
}
}
return s.concat(l).concat(r);
}
if (a.length <= 1) {
return a;
}
let h = Math.floor(a.length / 2);
return m(franzSort(a.slice(0, h)), franzSort(a.slice(h)));
}
// Example usage
let arr = [5, 3, 8, 6, 2, 7, 1, 4];
let sortedArr = franzSort(arr);
console.log(sortedArr);