## 5 comments on “Sorting in R as Inefficiently as Possible”

1. This post is hilariously funny: my belly was aching from laughing so hard!
Thank you for posting this amazing blog entry!

2. To be fair, shouldn't a bad sorting algorithm be required to have each step move toward creating a more sorted list? After all, one could just design an algorithm that attempts to factor a large product of primes (or some other computationally intensive task) between efficient steps (similar to the sleep algorithm).

I nominate an algorithm that repeatedly iteratively takes minimum values from a vector. It's a bad sorting algorithm, but not intentionally bad or complex. Likewise, if the algorithm runs for a limited amount of time, it will partially sort a vector, whereas bogosort has no partial sorting.

That being said, there's probably some trick involving Cartesian products which can be justified, and will easily gum up a system.

3. If, and when, R gets tail call optimization (not any time soon, I guess) slow sort won't blow up, and may even be less optimal.

4. a slightly more fair version is much faster:

bogosort2 <- function(x)
{
is.sorted = 0)
n=length(x)
while(x[1]!=min(x)) x <- sample(x)
for(i in 2:n) {
while(x[i]!=min(x[-(1:(i-1))])) x[-(1:(i-1))] <- sample(x[-(1:(i-1))])
}
x
}