1
+ '''
2
+ each algorith is in a function which prints a sorted array (ill change it to return)
3
+ there are notes before each algorithm explaining it
4
+
5
+ n = number of elements to sort
6
+ d = number of digits in the largest element
7
+ r = range of elements (largest - smallest)
8
+ k = size of key
9
+
10
+ the best, average and worst shows the trend of how each algorith will perform when increasing these values.
11
+
12
+ I will continue improving this and adding new algorithms
13
+ '''
14
+
1
15
from random import seed
2
16
from random import random
3
17
from random import randint
@@ -28,8 +42,26 @@ def store(data):
28
42
with open ('outfile' , 'wb' ) as fp :
29
43
pickle .dump (data , fp )
30
44
45
+
46
+
31
47
#bubble sort
32
48
def bubble ():
49
+ '''
50
+ Overview:
51
+ swap conscutive numbers until its sorted
52
+ Best:
53
+ n
54
+ Average:
55
+ n^2
56
+ Worst:
57
+ n^2
58
+ Stable:
59
+ Yes
60
+ Comparision:
61
+ Yes
62
+ Uses:
63
+ Don't use it unless you are teaching the basics of sorting
64
+ '''
33
65
global size , array , rng , out , count
34
66
#big loop
35
67
for i in range (0 , size - 1 ):
@@ -44,6 +76,22 @@ def bubble():
44
76
45
77
#counting sort
46
78
def counting ():
79
+ '''
80
+ Overview:
81
+ count the numbers of items places before the place of each item
82
+ Best:
83
+ n+r
84
+ Average:
85
+ n+r
86
+ Worst:
87
+ n+r
88
+ Stable:
89
+ Yes
90
+ Comparision:
91
+ No
92
+ Uses:
93
+ Very good sort for integers, esspcailly if the data set has a small range
94
+ '''
47
95
global size , array , rng , out , count
48
96
total = [0 ]
49
97
#count frequency
@@ -62,6 +110,24 @@ def counting():
62
110
63
111
#quick sort
64
112
def quick (arrays ,within = False ):
113
+ '''
114
+ Overview:
115
+ Pivot items around
116
+ Best:
117
+ n*log(n)
118
+ Average:
119
+ n*log(n)
120
+ Worst:
121
+ n^2
122
+ Stable:
123
+ no
124
+ Comparision:
125
+ yes
126
+ Uses:
127
+ good general algorithm, slower than merge sort on average but uses less space (usually)
128
+ Notes:
129
+ I need to improve this
130
+ '''
65
131
low = []
66
132
high = []
67
133
# end recurssion
@@ -88,6 +154,24 @@ def quick(arrays,within = False):
88
154
89
155
#radix sort
90
156
def radix ():
157
+ '''
158
+ Overview:
159
+ Sort based on each digit of an integer
160
+ Best:
161
+ n*(k/d)
162
+ Average:
163
+ n*(k/d)
164
+ Worst:
165
+ n*(k/d)
166
+ Stable:
167
+ Yes
168
+ Comparision:
169
+ No
170
+ Uses:
171
+ Very good sort for integers, esspcailly if the numbers have few digits
172
+ Notes:
173
+ this is a LSD radix I might add a MSD later
174
+ '''
91
175
global array , rng
92
176
#find the number of iterations
93
177
num = math .ceil (math .log10 (rng ))
@@ -108,13 +192,24 @@ def radix():
108
192
109
193
print (array )
110
194
111
- #merge sort
112
- def merge_sort ():
113
- pass
114
- #comming soon
115
-
116
195
#insertion sort
117
196
def insertion ():
197
+ '''
198
+ Overview:
199
+ Insert each number into its correct place
200
+ Best:
201
+ n
202
+ Average:
203
+ n^2
204
+ Worst:
205
+ n^2
206
+ Stable:
207
+ Yes
208
+ Comparision:
209
+ yes
210
+ Uses:
211
+ not great, easy to code use block sort instead if you need it to be stable and are short on memory
212
+ '''
118
213
global array
119
214
out = [array [0 ]]
120
215
#loop through orginal array
@@ -133,6 +228,22 @@ def insertion():
133
228
134
229
#selection sort
135
230
def select ():
231
+ '''
232
+ Overview:
233
+ Select the smallest item one by one
234
+ Best:
235
+ n^2
236
+ Average:
237
+ n^2
238
+ Worst:
239
+ n^2
240
+ Stable:
241
+ No
242
+ Comparision:
243
+ Yes
244
+ Uses:
245
+ Don't, its similar to insertion sort but worse since best is worse and its not stable
246
+ '''
136
247
global array
137
248
out = []
138
249
#loop until the start array is empty
@@ -148,6 +259,24 @@ def select():
148
259
149
260
#bogo sort
150
261
def bogo ():
262
+ '''
263
+ Overview:
264
+ Pick random orders in the hope that one will work
265
+ Best:
266
+ n
267
+ Average:
268
+ n*n!
269
+ Worst:
270
+ FOREVER!!!
271
+ Stable:
272
+ No
273
+ Comparision:
274
+ kinda of
275
+ Uses:
276
+ MEMES!, its closer to shuffling than a good sorting algoritm
277
+ notes:
278
+ It can go on forever so dont use it unless you are trying to show what not to do
279
+ '''
151
280
# WARNING this can take a LONG time only run with fewer than 10 items to sort
152
281
global array
153
282
cont = True
@@ -176,6 +305,11 @@ def sudo_bogo():
176
305
pass
177
306
#comming soon
178
307
308
+ #merge sort
309
+ def merge_sort ():
310
+ pass
311
+ #comming soon
312
+
179
313
#pigeonhole sort
180
314
def pigeonhole ():
181
315
pass
0 commit comments