-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Bogo_sort.py
107 lines (88 loc) · 2.7 KB
/
Bogo_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
BogoSort is also known as permutation sort, and/or stupid sort.
Imagine throwing a pile of cards, and then search for 2 pre-determined cards.
You pick 2 cards, match them if they are the ones you need, if not, you throw them back in the pile.
Pick 2 cards again, match them till you get the required pair. Yes it is stupid, slow and depends upon Luck.
The algorithm successively generates permutations of its input until it finds one that is sorted.
"""
import time
import random
n = int(input("Please Enter the number of Elements you want in Array: "))
# takes the whole line of n numbers
arr = input(
"Enter the elements separated by a WhiteSpace, for e.g. <1 2 3 4 5>: ")
final_array = list(map(int, arr.split(' ')))
def is_sorted(data) -> bool:
"""
Determine the Array is sorted or not.
Returns: bool
"""
return all(data[i] <= data[i + 1] for i in range(len(data) - 1))
def bogo_sort(final):
"""
Takes an array and applies Bogo Sorting algorithm it.
"""
count = 0
while not is_sorted(final):
random.shuffle(final)
print("Shuffling the array randomly.")
print(final)
time.sleep(2)
count += 1
print("\nObtained the sorted list (after {} round):".format(count - 1))
print(final)
bogo_sort(final_array)
"""
Test Case 1:
Please Enter the number of Elements you want in Array: 4
Enter the elements separated by a WhiteSpace, for e.g. <1 2 3 4 5>: 2 1 4 3
Shuffling the array randomly.
[3, 2, 4, 1]
Shuffling the array randomly.
[2, 1, 4, 3]
Shuffling the array randomly.
[1, 4, 3, 2]
Shuffling the array randomly.
[3, 2, 4, 1]
Shuffling the array randomly.
[2, 1, 3, 4]
Shuffling the array randomly.
[1, 4, 2, 3]
Shuffling the array randomly.
[2, 3, 1, 4]
Shuffling the array randomly.
[3, 4, 1, 2]
Shuffling the array randomly.
[1, 2, 3, 4]
Obtained the sorted list (after 8 round):
[1, 2, 3, 4]
Process finished with exit code 0
## Number of rounds for the Same array can be different. SEE TEST CASE 2 ##
Test Case 2:
Please Enter the number of Elements you want in Array: 4
Enter the elements separated by a WhiteSpace, for e.g. <1 2 3 4 5>: 2 1 4 3
Shuffling the array randomly.
[1, 3, 4, 2]
Shuffling the array randomly.
[2, 4, 1, 3]
Shuffling the array randomly.
[2, 1, 4, 3]
Shuffling the array randomly.
[1, 2, 4, 3]
Shuffling the array randomly.
[1, 4, 2, 3]
Shuffling the array randomly.
[1, 3, 4, 2]
Shuffling the array randomly.
[1, 2, 4, 3]
Shuffling the array randomly.
[1, 2, 3, 4]
Obtained the sorted list (after 7 round):
[1, 2, 3, 4]
Process finished with exit code 0
_____________________________________________
Time Complexity:
Worst Case : O(∞) [no upper bound]
Average Case: O(n*n!)
Best Case : O(n) [array is already sorted]
"""