|
| 1 | +import random |
| 2 | + |
| 3 | +def is_sorted(arr): |
| 4 | + """ |
| 5 | + Check if array is sorted in ascending order |
| 6 | + """ |
| 7 | + for i in range(len(arr) - 1): |
| 8 | + if arr[i] > arr[i + 1]: |
| 9 | + return False |
| 10 | + return True |
| 11 | + |
| 12 | + |
| 13 | +def bogo_sort(arr, max_iterations=10000): |
| 14 | + """ |
| 15 | + Bogo Sort Algorithm (also called Permutation Sort or Stupid Sort) |
| 16 | + Time Complexity: O((n+1)!) average, O(∞) worst case, O(n) best case |
| 17 | + Space Complexity: O(1) |
| 18 | + |
| 19 | + WARNING: This is extremely inefficient! Used only for educational purposes. |
| 20 | + The algorithm randomly shuffles the array until it happens to be sorted. |
| 21 | + |
| 22 | + For safety, this implementation has a max_iterations limit. |
| 23 | + """ |
| 24 | + attempts = 0 |
| 25 | + |
| 26 | + while not is_sorted(arr) and attempts < max_iterations: |
| 27 | + random.shuffle(arr) |
| 28 | + attempts += 1 |
| 29 | + |
| 30 | + if attempts >= max_iterations: |
| 31 | + print(f"Warning: Reached maximum iterations ({max_iterations})") |
| 32 | + print("Array may not be fully sorted!") |
| 33 | + |
| 34 | + return arr, attempts |
| 35 | + |
| 36 | + |
| 37 | +def bogo_sort_deterministic(arr): |
| 38 | + """ |
| 39 | + Deterministic Bogo Sort - tries all permutations systematically |
| 40 | + Still extremely inefficient but guarantees to finish |
| 41 | + """ |
| 42 | + from itertools import permutations |
| 43 | + |
| 44 | + n = len(arr) |
| 45 | + attempts = 0 |
| 46 | + |
| 47 | + for perm in permutations(arr): |
| 48 | + attempts += 1 |
| 49 | + if list(perm) == sorted(arr): |
| 50 | + return list(perm), attempts |
| 51 | + |
| 52 | + return arr, attempts |
| 53 | + |
| 54 | + |
| 55 | +def bogo_sort_with_tracking(arr, max_iterations=10000): |
| 56 | + """ |
| 57 | + Bogo Sort with attempt tracking for visualization |
| 58 | + """ |
| 59 | + attempts = 0 |
| 60 | + |
| 61 | + print(f"Starting array: {arr}") |
| 62 | + print(f"Target (sorted): {sorted(arr)}\n") |
| 63 | + |
| 64 | + while not is_sorted(arr) and attempts < max_iterations: |
| 65 | + random.shuffle(arr) |
| 66 | + attempts += 1 |
| 67 | + |
| 68 | + # Show progress every 100 attempts |
| 69 | + if attempts % 100 == 0: |
| 70 | + print(f"Attempt {attempts}: {arr}") |
| 71 | + |
| 72 | + if is_sorted(arr): |
| 73 | + print(f"\nSuccess! Sorted in {attempts} attempts") |
| 74 | + else: |
| 75 | + print(f"\nFailed after {attempts} attempts") |
| 76 | + |
| 77 | + return arr, attempts |
| 78 | + |
| 79 | + |
| 80 | +def bogo_bogo_sort(arr, max_iterations=1000): |
| 81 | + """ |
| 82 | + Bogo Bogo Sort - Even more inefficient variant! |
| 83 | + Checks if sorted using another bogo sort |
| 84 | + |
| 85 | + WARNING: Extremely slow! Use only with very small arrays (2-3 elements) |
| 86 | + """ |
| 87 | + attempts = 0 |
| 88 | + |
| 89 | + def bogo_is_sorted(check_arr, max_check=100): |
| 90 | + """Check if sorted using bogo sort logic""" |
| 91 | + temp = check_arr.copy() |
| 92 | + for _ in range(max_check): |
| 93 | + if temp == sorted(check_arr): |
| 94 | + return True |
| 95 | + random.shuffle(temp) |
| 96 | + return temp == sorted(check_arr) |
| 97 | + |
| 98 | + while not bogo_is_sorted(arr) and attempts < max_iterations: |
| 99 | + random.shuffle(arr) |
| 100 | + attempts += 1 |
| 101 | + |
| 102 | + return arr, attempts |
| 103 | + |
| 104 | + |
| 105 | +def calculate_expected_attempts(n): |
| 106 | + """ |
| 107 | + Calculate expected number of attempts for array of size n |
| 108 | + Expected attempts = n! (factorial of n) |
| 109 | + """ |
| 110 | + import math |
| 111 | + return math.factorial(n) |
| 112 | + |
| 113 | + |
| 114 | +# Example usage |
| 115 | +if __name__ == "__main__": |
| 116 | + # WARNING: Only use small arrays with Bogo Sort! |
| 117 | + |
| 118 | + # Test case 1 - Very small array |
| 119 | + arr1 = [3, 2, 1] |
| 120 | + print("Original array:", arr1) |
| 121 | + expected = calculate_expected_attempts(len(arr1)) |
| 122 | + print(f"Expected attempts: {expected}") |
| 123 | + sorted_arr, attempts = bogo_sort(arr1.copy()) |
| 124 | + print("Sorted array:", sorted_arr) |
| 125 | + print(f"Actual attempts: {attempts}\n") |
| 126 | + |
| 127 | + # Test case 2 |
| 128 | + arr2 = [4, 1, 3, 2] |
| 129 | + print("Original array:", arr2) |
| 130 | + expected = calculate_expected_attempts(len(arr2)) |
| 131 | + print(f"Expected attempts: {expected}") |
| 132 | + sorted_arr, attempts = bogo_sort(arr2.copy()) |
| 133 | + print("Sorted array:", sorted_arr) |
| 134 | + print(f"Actual attempts: {attempts}\n") |
| 135 | + |
| 136 | + # Test case 3 - Already sorted (best case) |
| 137 | + arr3 = [1, 2, 3] |
| 138 | + print("Already sorted:", arr3) |
| 139 | + sorted_arr, attempts = bogo_sort(arr3.copy()) |
| 140 | + print("Sorted array:", sorted_arr) |
| 141 | + print(f"Attempts: {attempts}\n") |
| 142 | + |
| 143 | + # Test case 4 - Deterministic version |
| 144 | + arr4 = [3, 1, 2] |
| 145 | + print("Using deterministic bogo sort:") |
| 146 | + print("Original array:", arr4) |
| 147 | + sorted_arr, attempts = bogo_sort_deterministic(arr4.copy()) |
| 148 | + print("Sorted array:", sorted_arr) |
| 149 | + print(f"Permutations checked: {attempts}\n") |
| 150 | + |
| 151 | + # Test case 5 - With tracking (small array only!) |
| 152 | + arr5 = [2, 1, 3] |
| 153 | + print("With tracking:") |
| 154 | + bogo_sort_with_tracking(arr5.copy(), max_iterations=500) |
| 155 | + |
| 156 | + # Complexity warning |
| 157 | + print("\n" + "="*50) |
| 158 | + print("COMPLEXITY WARNING:") |
| 159 | + print("Array size | Expected attempts") |
| 160 | + print("-"*50) |
| 161 | + for size in range(1, 8): |
| 162 | + print(f" {size} | {calculate_expected_attempts(size):,}") |
| 163 | + print("="*50) |
| 164 | + print("\nNever use Bogo Sort with arrays larger than 5 elements!") |
0 commit comments