-
Notifications
You must be signed in to change notification settings - Fork 8
/
bogo sort.jl
72 lines (40 loc) · 1.14 KB
/
bogo sort.jl
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
# coding: utf-8
# In[1]:
using Random
using Plots
# In[2]:
#bogo, what the heck does it even mean
#i d call it bullocks sort
#it is not really sorting
#it keeps shuffling the list until the list is sorted
#kinda feel like monte carlo simulation
#the time complexity is (n+1)!
#for a large list, we may never wait for the day it gets sorted
# In[3]:
function bogo_sort(arr)
#deep copy is crucial
arr_copy=deepcopy(arr)
sort!(arr_copy)
counter=0
while arr_copy!=arr
shuffle!(arr)
counter+=1
end
return counter
end
# In[4]:
test_arr=rand(0:100,5)
#dont forget to deep copy
test_stats=[bogo_sort(deepcopy(test_arr)) for i in 1:100000]
# In[5]:
#visualize the ridiculous result of bogo sort by histogram
#it can go as far as 1400 times of shuffle to get it sorted from my test
#this is merely a five-element list
# In[6]:
#just out of curiosity
#could this be a power law distribution
histogram(test_stats,bins=10000,
normed=:true,fill=(:blue, true),
la=0,legend=:none,
xlabel="iterations to reach sorted state",
ylabel="frequency in 100k simulations")