Need a quick chart or graph for your blog? Try our reStructured Text renderer.
Join Siafoo Now
or
Learn More
BogoSort 6
In Brief  BogoSort is a highly ineffective algorithm notable only for its comedic value.... more 
Implementation Difficulty 
1 function bogo_sort(array)
2
3 do
4 is_sorted = check_whether_sorted(array)
5
6 // If the array is sorted, we're done.
7 if is_sorted == True
8 return array
9 end if
10
11 // If array is not sorted, randomize it and try again!
12 array = randomize_array(array)
13
14 while is_sorted == False
15
16 end function
17
18
19 function randomize_array(array)
20
21 n = length(array)
22
23 for i = 1 to i = n1
24
25 // Get a random array position after "i"
26 j = random_integer(i...n)
27
28 //Swap the values at the two positions
29 swap(array[i] and array[j])
30
31 end for
32
33 return array
34
35 end function
36
37
38 function check_whether_sorted(array)
39
40 n = length(array)
41
42 for i = 1 to i = n1
43
44 // If any two consecutive values are out of order
45 // then the array is not sorted.
46 if array[i] > array[i + 1]
47 return False
48 end if
49
50 end for
51
52 // If nothing was out of order, the array is sorted.
53 return True
54
55 end function
56
BogoSort is a highly ineffective algorithm notable only for its comedic value.
The primary loop checks whether the input array is properly sorted and randomizes the entire array if it is not.
An analogy would be painting numbers on goldfish in a pond, counting the order in which the fish cross under a bridge, and dropping a grenade into the water whenever don't happen to like that order.In other words, you'd have to be crazy to implement this algorithm. Mathematically, the worst case scenario is that the algorithm takes an infinite amount of time to resolve. Even looking at "average case" values, the expected number of swaps is and the expected number of valuecomparisons is .
Implementations
Licenses

The standard library algorithm random_shuffle makes for an easy implementation of BogoSort in C++....
Real code trolling :D, note the generator expression combined with built in function any(),
it will return after the first miss match.
An implementation of the sweet BogoSort algorithm. Just download, run, and enjoy....
Just for fun. Only time I'd recommend using it is if you're trying to convince someone it's time to upgrade a box. :o...
Only as ridiculously inefficient as it needs to be.
A NumPybased implementation of the everhilarious Bogosort algorithm. Tries (hopelessly) to optimize the algorithm by taking advantage of lazy iterators, NumPy Cbased shuffling, and minimizing...
In Javascript, you can pass a function to the Array.sort method, and tell that function to just return whatever the hell it wants. This makes for an easy, but very slow (suprise!) Bogosort...
Comments
The averagecase number of swaps asymptotes towards (n1)*n! and the averagecase number of comparisons approaches (e1)*n! + O(1)
We should form a club, with the purpose of wasting CPU time sorting things with BogoSort and attempting to get BogoSort into database servers and numerical libraries.
.. image:: 66
:alt: Bogosort Flowchart
:width: 500
:height: 500