Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for BogoSort or Permutation Sort
BogoSort, also known as Permutation Sort or Stupid Sort, is a highly inefficient sorting algorithm that works by generating random permutations of a list until it becomes sorted. Despite being impractical for real-world use, it serves as an interesting example of how not to design algorithms.
BogoSort continues to shuffle the array randomly until the list becomes sorted. The expected time complexity is unbounded, making its average performance extremely poor compared to efficient algorithms like QuickSort or MergeSort.
Algorithm Overview
The BogoSort algorithm follows these simple steps ?
- Check if the list is sorted in ascending order
- If not sorted, randomly shuffle all elements
- Repeat until the list becomes sorted
Time and Space Complexity
BogoSort has unique complexity characteristics ?
- Best Case: O(n) − when the list is already sorted
- Average Case: O(n × n!) − factorial time complexity
- Worst Case: Unbounded − theoretically infinite
- Space Complexity: O(1) − sorts in−place
Python Implementation
Here's a complete implementation of BogoSort using Python's random module ?
import random
def is_sorted(arr):
"""Check if the array is sorted in ascending order"""
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
return False
return True
def bogo_sort(arr):
"""Perform BogoSort on the given array"""
attempts = 0
while not is_sorted(arr):
random.shuffle(arr)
attempts += 1
# Safety check to prevent infinite execution
if attempts > 10000:
print(f"Stopped after {attempts} attempts")
break
print(f"Sorted after {attempts} shuffles")
return arr
# Example with a small array
numbers = [3, 1, 2]
print("Original array:", numbers)
sorted_numbers = bogo_sort(numbers.copy())
print("Sorted array:", sorted_numbers)
The output shows the random nature of BogoSort ?
Original array: [3, 1, 2] Sorted after 4 shuffles Sorted array: [1, 2, 3]
Analyzing Different Input Sizes
Let's see how BogoSort performs with different array sizes ?
import random
import time
def is_sorted(arr):
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
return False
return True
def bogo_sort_timed(arr, max_attempts=1000):
"""BogoSort with time tracking and attempt limit"""
attempts = 0
start_time = time.time()
while not is_sorted(arr) and attempts < max_attempts:
random.shuffle(arr)
attempts += 1
end_time = time.time()
execution_time = end_time - start_time
if is_sorted(arr):
print(f"Array of size {len(arr)}: Sorted in {attempts} attempts, {execution_time:.4f} seconds")
else:
print(f"Array of size {len(arr)}: Failed to sort in {max_attempts} attempts")
return arr
# Test with different sizes
test_cases = [
[2, 1],
[3, 1, 2],
[4, 3, 2, 1]
]
for case in test_cases:
bogo_sort_timed(case.copy())
Array of size 2: Sorted in 1 attempts, 0.0001 seconds Array of size 3: Sorted in 2 attempts, 0.0002 seconds Array of size 4: Sorted in 18 attempts, 0.0008 seconds
Comparison with Efficient Algorithms
| Algorithm | Best Case | Average Case | Worst Case | Practical Use |
|---|---|---|---|---|
| BogoSort | O(n) | O(n × n!) | Unbounded | Educational only |
| QuickSort | O(n log n) | O(n log n) | O(n²) | Widely used |
| MergeSort | O(n log n) | O(n log n) | O(n log n) | Stable sorting |
Why BogoSort is Impractical
BogoSort demonstrates several important algorithmic concepts ?
- Factorial complexity: For n elements, there are n! possible permutations
- No guarantee of termination: Theoretically, it might never finish
- Purely random approach: No intelligent strategy for improvement
- Educational value: Shows the importance of algorithm design
Conclusion
BogoSort serves as an excellent example of how randomness without strategy leads to extremely poor performance. While it guarantees a correct result (eventually), its factorial time complexity makes it unsuitable for any practical application beyond educational demonstrations.
