Skip to content

Commit ae872e4

Browse files
committed
added notes about each algorithm
fixed formatting
1 parent 8d861e8 commit ae872e4

File tree

2 files changed

+152
-5
lines changed

2 files changed

+152
-5
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ please feel free to add and edit
2020
* Tim sort
2121
* Heap sort
2222
* Sudo bogo sort
23+
24+
## notes
25+
26+
each algorith is in a function which prints a sorted array (ill change it to return)\
27+
there are notes before each algorithm explaining it
28+
29+
* n = number of elements to sort
30+
* d = number of digits in the largest element
31+
* r = range of elements (largest - smallest)
32+
* k = size of key
33+
34+
the best, average and worst shows the trend of how each algorith will perform when increasing these values.\
35+
I will continue improving this and adding new algorithms

sort.py

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
115
from random import seed
216
from random import random
317
from random import randint
@@ -28,8 +42,26 @@ def store(data):
2842
with open('outfile', 'wb') as fp:
2943
pickle.dump(data, fp)
3044

45+
46+
3147
#bubble sort
3248
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+
'''
3365
global size, array, rng, out, count
3466
#big loop
3567
for i in range(0, size-1):
@@ -44,6 +76,22 @@ def bubble():
4476

4577
#counting sort
4678
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+
'''
4795
global size, array, rng, out, count
4896
total = [0]
4997
#count frequency
@@ -62,6 +110,24 @@ def counting():
62110

63111
#quick sort
64112
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+
'''
65131
low = []
66132
high = []
67133
# end recurssion
@@ -88,6 +154,24 @@ def quick(arrays,within = False):
88154

89155
#radix sort
90156
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+
'''
91175
global array, rng
92176
#find the number of iterations
93177
num = math.ceil(math.log10(rng))
@@ -108,13 +192,24 @@ def radix():
108192

109193
print(array)
110194

111-
#merge sort
112-
def merge_sort():
113-
pass
114-
#comming soon
115-
116195
#insertion sort
117196
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+
'''
118213
global array
119214
out =[array[0]]
120215
#loop through orginal array
@@ -133,6 +228,22 @@ def insertion():
133228

134229
#selection sort
135230
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+
'''
136247
global array
137248
out =[]
138249
#loop until the start array is empty
@@ -148,6 +259,24 @@ def select():
148259

149260
#bogo sort
150261
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+
'''
151280
# WARNING this can take a LONG time only run with fewer than 10 items to sort
152281
global array
153282
cont =True
@@ -176,6 +305,11 @@ def sudo_bogo():
176305
pass
177306
#comming soon
178307

308+
#merge sort
309+
def merge_sort():
310+
pass
311+
#comming soon
312+
179313
#pigeonhole sort
180314
def pigeonhole():
181315
pass

0 commit comments

Comments
 (0)