Clever Geek Handbook
📜 ⬆️ ⬇️

Bogosort

Bogosort (also random sorting , gun sorting or monkey sorting ) is a very inefficient sorting algorithm. It is used only for educational purposes, opposing other, more realistic algorithms. If bogosort is used to sort a deck of cards, then first in the algorithm you need to check whether all cards lie in order, and if they do not lie, then randomly mix it, check whether all cards are now in order, and repeat the process until the deck is sorted .

The average time of the algorithm

O(n⋅Σi=one∞in!⋅(one-onen!)i-one)=O(n⋅n!).{\ displaystyle O \ left (n \ cdot \ sum _ {i = 1} ^ {\ infty} {\ frac {i} {n!}} \ cdot \ left (1 - {\ frac {1} {n! }} \ right) ^ {i-1} \ right) = O (n \ cdot n!).} {\ displaystyle O \ left (n \ cdot \ sum _ {i = 1} ^ {\ infty} {\ frac {i} {n!}} \ cdot \ left (1 - {\ frac {1} {n! }} \ right) ^ {i-1} \ right) = O (n \ cdot n!).}

With the cycle once per second, sorting out the following number of elements on average can take:

Number of itemsone23fourfive67eight9teneleven12
Average time1 s4 s18 s96 s10 min1.2 h9.8 h3.7 days37.8 days1.15 years13.9 years182 years

When running a 4-core processor at 2.4 GHz (9.6 billion operations per second):

Number of itemsteneleven12131415sixteen1718nineteen20
Average time0.0037 s0.045 s0.59 s8.4 s2.1 min33.6 min9.7 h7.29 days139 days7.6 years160 years

A deck of 32 cards will be sorted by a computer with an average of 2.7⋅10 19 years.

Implementation Example

  int correct ( int * arr , int size ) { while ( size -> 0 ) if ( arr [ size - 1 ] > arr [ size ]) return 0 ;  return 1 ;  } void shuffle ( int * arr , int size ) { for ( int i = 0 ; i < size ; ++ i ) swap ( arr [ i ], arr [( rand () % size )]);  } void bogoSort ( int * arr , int size ) { while ( ! correct ( arr , size )) shuffle ( arr , size );  } 

See also

  • Las Vegas (algorithm)
  • Pseudo Random Number Generators

Links

  • Bogosort / Jargon File (eng.)
  • Max Sherman Bogo-sort is Sort of Slow , June 2013 (Eng.)
  • Hermann Gruber, Markus Holzer, and Oliver Ruepp, Sorting the Slow Way: An Analysis of Perversely Awful Randomized Sorting Algorithms , 2007. (English)
  • Rahul Agrawal, https://www.geeksforgeeks.org/bogosort-permutation-sort/ (eng.)
  • Impractical sorting - senseless and merciless / valemak, October 18, 2013
Source - https://ru.wikipedia.org/w/index.php?title=Bogosort&oldid=100064515


More articles:

  • Springs (Kharkiv region)
  • Orson Welles Filmography
  • Khatanga Bay
  • Tubyanskoe Rural Settlement
  • Aleksinac
  • Gumilev, Lev Nikolaevich
  • Ishay, Eli
  • Safari
  • Notification
  • Zemlyanichenko, Alexander Vadimovich

All articles

Clever Geek | 2019