-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbogoSort.py
More file actions
49 lines (45 loc) · 1.3 KB
/
Copy pathbogoSort.py
File metadata and controls
49 lines (45 loc) · 1.3 KB
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
import random
import time
def is_sorted(data):
for i in range(len(data) - 1):
if data[i] > data[i + 1]:
return False
return True
def bogoSortMemoized(data):
seenDict = {}
count = 0
res = []
while not is_sorted(data):
count += 1
print("Unsorted List Fast: " + str(data))
seenDict[str(data)] = True
while str(data) in seenDict:
random.shuffle(data)
res.append(count)
res.append(data)
return res
def bogoSortBruteForce(data):
res = []
count = 0
while not is_sorted(data):
count += 1
print("Unsorted List Slow: " + str(data))
random.shuffle(data)
res.append(count)
res.append(data)
return res
if __name__ == "__main__":
lst1 = []
lst2 = []
while True:
userinput = input("Give list you would like sorted: ")
if userinput == 'end':
break
lst1.append(int(userinput))
lst2.append(int(userinput))
print("Unsorted List: " + str(lst1))
lst1 = bogoSortMemoized(lst1)
print("FAST BOGOSORT: " + "Sorted List: " + str(lst1[1]) + " Number of Steps: " + str(lst1[0]))
time.sleep(10)
lst2 = bogoSortBruteForce(lst2)
print("Slow BOGOSORT: " + "Sorted List: " + str(lst2[1]) + " Number of Steps: " + str(lst2[0]))