Bogo Sort
- Ω( n )
- Θ( n · n! )
- unbounded
- space O( 1 )
Bogo sort is a loop with two steps: look at the array, and if it is not sorted, shuffle it completely and look again. There is no progress between rounds, because each shuffle forgets everything the last one did. The only way out is to land on the sorted order by luck, so the running time is a matter of probability rather than of the input.
Bogo Sort is limited to 7 elements: on average it takes Θ( n · n! ) steps.
- Comparisons count only element-against-element checks; loop and index checks are never counted.
- Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
- A swap counts once, as a swap. It is never also counted as a read or a write.
- Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.
How bogo sort works
The first check on 5, 2, 6, 1, 4, 3 fails at once: it compares the first pair, finds that 2 is smaller than 5, and stops after a single comparison. The shuffle then walks from the last position down, swapping each position with a random one at or before it. In the default seeded run the first shuffle makes three visible swaps and leaves 4, 2, 3, 6, 5, 1. The other two positions drew themselves, so they cause no swap and no row in the table.
The second shuffle starts again from the last position and turns that array into 4, 5, 1, 3, 6, 2, and the third produces 6, 3, 5, 4, 2, 1. None of these is closer to sorted than the input. That is the point of the algorithm: a shuffle is uniform over all arrangements, so each result has the same small chance of being the sorted one whatever came before it. A badly scrambled array and a nearly sorted one are shuffled exactly the same way.
With six distinct values there are 720 arrangements, so about 720 shuffles are needed on average. The default seeded run for this input needed 2,489 shuffles, 4,209 comparisons and 8,883 swaps before the check finally passed, more than three times the average, which is why the table can only show the opening of the run. The first states quoted here come from the default random generator, and a different seed gives different arrays but the same behaviour.
| Step | Change | Array after |
|---|---|---|
| 1 | Swap indexes 5 and 3 | 5 2 6 3 4 1 |
| 2 | Swap indexes 4 and 0 | 4 2 6 3 5 1 |
| 3 | Swap indexes 3 and 2 | 4 2 3 6 5 1 |
| 4 | Swap indexes 5 and 1 | 4 1 3 6 5 2 |
| 5 | Swap indexes 4 and 3 | 4 1 3 5 6 2 |
| 6 | Swap indexes 3 and 2 | 4 1 5 3 6 2 |
| 7 | Swap indexes 2 and 1 | 4 5 1 3 6 2 |
| 8 | Swap indexes 5 and 2 | 4 5 2 3 6 1 |
| 9 | Swap indexes 4 and 2 | 4 5 6 3 2 1 |
| 10 | Swap indexes 3 and 0 | 3 5 6 4 2 1 |
| 11 | Swap indexes 2 and 1 | 3 6 5 4 2 1 |
| 12 | Swap indexes 1 and 0 | 6 3 5 4 2 1 |
12 of 8,883 changes shown; the rest follow the same pattern until the array is sorted.
Pseudocode
attempts = 0
while a is not sorted and attempts < 10000
shuffle a at random
attempts = attempts + 1
Why these complexities
Each round has two parts. The check compares neighbours from the left and gives up at the first pair out of order, which on a random array happens after about 1.7 comparisons, so it is constant on average. The shuffle swaps up to n - 1 pairs, so it costs about n. A round is therefore O(n), and the number of rounds is the random part. With distinct values exactly one of the n! arrangements is sorted, so the number of rounds is geometric with mean n!. The expected total is about n times n!, which gives 5,040 rounds at n = 7 and over 3.6 million at n = 10.
The best case is n - 1 comparisons: if the array is already sorted, the first check succeeds and no shuffle happens, which is why this implementation counts as adaptive in that narrow sense. The worst case is unbounded, because a geometric variable has no maximum. This implementation therefore stops after 10,000 attempts. For 7 elements the chance of not having succeeded by then is about (1 - 1/5040) raised to the 10,000th power, near 14 percent, which is why roughly one run in seven or eight ends unsorted. With 6 elements the same cap would almost never be hit.
Space is constant. The shuffle swaps elements in place and the check keeps no state, so no auxiliary array is allocated. The sort is not stable, since the shuffle ignores positions entirely and equal values come out in random relative order. Duplicates change the arithmetic in an interesting way, described in the second question below.
Measured on this site
Exact counts from this site's own engine for 7 elements, using the same rules as the visualiser.
| Input | Comparisons | Writes | Swaps |
|---|---|---|---|
| Random | 2,615 | 0 | 6,779 |
| Already sorted | 6 | 0 | 0 |
| Reversed | 8,800 | 0 | 22,777 |
| Few distinct values | 285 | 0 | 649 |
When to use it
Use it only as a joke or as a warning about what a bad algorithm looks like. Never use it for real work.
Frequently asked questions
How long does bogo sort take?
On average about n! shuffles, which is why it is limited to 7 elements here. There is no upper bound, because the shuffles can keep missing.
What happens if it does not finish?
After 10,000 attempts the run stops and shows the array as it was left, unsorted.
Why is the worst case unbounded?
Each shuffle is random, so there is always a chance that the next one is not sorted either.
Does the kind of shuffle matter?
Yes. This implementation walks from the end and swaps each position with a random one at or before it, which gives every arrangement the same probability. That uniformity is what makes the expected number of rounds exactly n!. A careless shuffle that swaps each position with a random position anywhere in the array is biased, so some arrangements are more likely than others and the sorted one can be less likely than 1 in n!, which stretches the running time.
Do duplicate values make bogo sort faster?
They do. With repeated values several arrangements look identical and several of them count as sorted. For 1, 1, 2 there are three distinct arrangements and one is sorted, so a shuffle succeeds one time in three instead of one in six. In general the chance of success is the product of the factorials of each value's multiplicity divided by n!. An array of equal values passes the first check and never shuffles, which is the best case.
Background
Nobody is credited with inventing bogo sort. The name comes from bogus, in the vocabulary of programmers' slang, and the algorithm is recorded in the Jargon File as the archetypal perversely awful algorithm, also known as stupid sort. It circulated as a joke for years before anyone analysed it, and it appears in many lecture notes as the standard example of a sort worse than any systematic one.
It does have a proper analysis. Gruber, Holzer and Ruepp studied it and related randomised sorts in a 2007 paper on perversely awful sorting algorithms, deriving expected counts of comparisons and swaps. A close relative is bozo sort, which swaps just two random elements before re-checking instead of reshuffling the whole array. A deterministic cousin, permutation sort, tries the arrangements in a fixed order instead of at random and so is guaranteed to finish.
References
- Gruber, Holzer, Ruepp, Sorting the slow way: an analysis of perversely awful randomized sorting algorithms, Fun with Algorithms (FUN), 2007
- Raymond (ed.), The Jargon File, entry for bogo-sort
- Knuth, The Art of Computer Programming, vol. 2, section 3.4.2 (Random sampling and shuffling)
Compare with
| Algorithm | Average time | Worst time |
|---|---|---|
| Stooge Sort | Θ( n2.71 ) | O( n2.71 ) |
| Best time | Ω( n ) | good |
|---|---|---|
| Average time | Θ( n · n! ) | poor |
| Worst time | unbounded | poor |
| Worst space | O( 1 ) | good |
Properties
- Stable
- No
- In place
- Yes
- Adaptive
- Yes
- Comparison-based
- Yes