News for hackers. Stuff that matters.
Re:American Flag Sort by franze on Wednesday April 24, 2024 @03:32PM (#40148569)
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);

There are no comments on this comment yet.