-1

I was following down a rabbit hole of math when I came up with a sorting algorithm. I try looking around the internet to see if anyone has come up with the same thing. It doesn't seem like it was the case (if anyone can prove me wrong, please tell me). I think I will call it anti-bogosort. The sorting algorithm is a variation on bogosort but with a twist. Whenever you randomize the list, every item is in a different place than before.

Take the sorted list $\{1, 2, 3, 4, 5\}$. The randomization will require that every item is in a different location than it is now, so $\{2, 5, 3, 4, 1\}$ is not a valid randomization.

A key observation I noticed is that the list may only have the chance to become sorted if all items are in the wrong spot. If just one of them were to be correctly placed, it would not be able to be in the same spot again. This might change how different it is from normal bogosort. All I know for sure is that it is less efficient. This isn't my area of expertise, so if anyone could take a crack at it, be my guest.

  • Why do you say that everything need to start on the wrong spot? Something can start on the right spot, then go to a wrong spot and then go back in order to be sorted, not? – Lucas Resende Aug 14 '20 at 22:36
  • Please give a reference to what you call "bogosort", which is unknown to me and probably to a majority of people here... – Jean Marie Aug 14 '20 at 23:15
  • @LucasResende that is true, but what I meant to say is that in order for the list to be sorted within the next randomization, all items of the list have to be in the wrong place. – ThePretzelMan Aug 15 '20 at 16:56
  • I started a question to get a full answer for yours, and it is now answered: https://math.stackexchange.com/questions/3791271/uniformly-choose-two-derangements-sigma-i-sigma-j-what-is-the-distribution, that, together with my partial answer, shows that the bogosort with only derangements has the same time complexity (but with different constants) as the original bogosort. – Lucas Resende Aug 17 '20 at 23:14

1 Answers1

1

I will show just that the algorithm works.

An order is just a permutation of the index order $\{1,\cdots,n\}$. Lets denote a permutation by $\sigma$, the set of all permutations of $n$ elements is called $S_n$.

Let $\sigma^*(i)=i~\forall i$ be the the permutation that represents the ordered list and let $\sigma_0$ be your initial permutation.

We want to know what happens if we iterate by randomly sorting derangements $$\sigma_i \in D = \{ \sigma\in S_n : \sigma(i) \neq i~\forall i=1,\cdots, n \}$$ and testing if $\sigma_i \circ \sigma_{i-1}\circ\cdots\circ \sigma_1 \circ \sigma_0 = \sigma^*$ or not.

This question here says that every $\sigma \in S_n$ is the product of two derangements. The classic bogosort is just a composition of several permutations, since we can construct any permutation by compositing two derangements, then, since bogosort works and $|S_n|=n!< \infty$, this version also works.

To show that both algorithms have the same complexity it is sufficient to show that uniformly taking $\sigma_i,\sigma_j \in D$, we have $$P(\sigma_i\circ\sigma_j = \sigma) \in O\left( \frac{1}{n!} \right)~\forall \sigma \in S_n,$$ because, that way, we can group the derangements two by two and they will work almost as if we allow any kind of permutation. I don't know if it holds, but it is easy to see that this probability is not uniform. Take $\sigma = \sigma^*$, the only way that $\sigma_j\circ \sigma_i = \sigma^*$ is if $\sigma_i = \sigma_j^{-1}$, therefore, $$ P(\sigma_i\circ \sigma_j = \sigma^*) = \frac{1}{|D|} > \frac{1}{|S_n|}, $$ since we are free to pick any $\sigma_i$, but after that $\sigma_j$ is defined.

I hope it was useful.

Lucas Resende
  • 1,286
  • 9
  • 21