News for hackers. Stuff that matters.
Re:Is this the simplest (and most surprising) sorting algori... by antognini on Tuesday October 05, 2021 @12:19PM (#28761507)
Bogosort is pretty simple, too.

  while not is_sorted(a) do
    shuffle(a)
However the shuffle function hides a surprising amount of complexity. It is nontrivial to write a correct shuffle function. [1]

But Bogosort isn't even the worst sorting algorithm out there! There is also Worstsort [2].

[1]: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

[2]: https://arxiv.org/abs/1406.1077

Re:Is this the simplest (and most surprising) sorting algori... by CaptainNegative on Tuesday October 05, 2021 @12:39PM (#28761826)
Shuffling is even less tricky if you're not trying to do it in place, if you're not trying to do it in linear(ithmic) time, or if you're not trying to induce a uniform distribution over all permutations (but rather just want some positive probability for all of them). Relaxing any combination of these conditions works just fine for Bogosort.
Re:Is this the simplest (and most surprising) sorting algori... by infinisil on Tuesday October 05, 2021 @01:04PM (#28762192)
It would even suffice to just swap two random elements every iteration!
Re:Is this the simplest (and most surprising) sorting algori... by 6510 on Tuesday October 05, 2021 @02:33PM (#28763593)
My gut says that combining that with other sort cycles in some magic ratio could do magical things. I'm quite wrong quite often ofc but the adventure is out there!
Re:Is this the simplest (and most surprising) sorting algori... by a1369209993 on Wednesday October 06, 2021 @09:44PM (#28780809)
Fun fact: Fisher-Yates samples a uniform distribution over permutations and is therefor capable of producing any permutation, so you can sort any array in linear time using Fisher-Yates with its RNG replaced by a appropriate oracle. (So the complexity of sorting a array mostly reduces to the complexity of determining which permutation it's in relative to a sorted version.)
Re:Is this the simplest (and most surprising) sorting algori... by innocenat on Tuesday October 05, 2021 @01:30PM (#28762625)
I think most C++ implementation of bogosort use std::next_permutation
Re:Is this the simplest (and most surprising) sorting algori... by jtolmar on Tuesday October 05, 2021 @07:33PM (#28767111)
You can turn a sort into a shuffle by replacing the comparator with a random coin flip, and you already have defined a sort algorithm, so there's no added code complexity.

    fn bogosort(a, comparator):
      while not is_sorted(a, comparator) do
        bogosort(a, rand)
Re:Is this the simplest (and most surprising) sorting algori... by aidenn0 on Tuesday October 05, 2021 @08:05PM (#28767400)
You can't turn all sorts (not even all comparison sorts!) into a shuffle by replacing the comparator with a random coin flip. In fact your code post is a demonstration of that fact.
Re:Is this the simplest (and most surprising) sorting algori... by j16sdiz on Tuesday October 05, 2021 @01:36PM (#28762707)
a good (even) shuffle is not easy.