10
kiki
278d

There is a mAtHeMaTicAl pRoOf 🤡 that a comparison-based sorting algorithm cannot get more efficient than O(n*log n). That's bullshit. Radix sort et al. — granted, they're not comparison-based.

But there is one comparison-based algorithm that can sort an array in O(n). It's called Stalin sort.

It traverses the array and deletes every item that doesn't appear in order. Boom, problem solved — the array is sorted in O(n), at the expense of losing (most of the) elements.

This is the perfect metaphor of Stalin's politics.

Comments
  • 11
    ... By that definition a sorting algorithm with O(1) is possible... Just return the first element in the array. 🤔
  • 3
    An array doesn't need sorting if there is nothing to sort.

    Edit: I thought this was fullstackclown posting this when i saw the clown emoji
  • 2
    Still, big O and computational complexity do not take real world implementations as input.

    Radix sort is, of course, not comparison based, and is O(n), but that's amortized O(n). The cost of the hashing is hidden by the implicit k in O(k•n).

    Same way that in modern c++, a vector surpasses a set in performance (even in operations designed for a set), up to several thousands of elements, just because it exploits cache locality si much better.
  • 0
    @atheist returning the first element is guaranteed to lose data. Stalin sort losing data isn't predetermined.
Add Comment