Skip to content

Commit 87521aa

Browse files
committed
refat
1 parent 41f965d commit 87521aa

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

Diff for: src/main.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def test_all_algoithms():
2020

2121
print('Press [Enter] to continue!')
2222
input()
23-
23+
24+
2425
def test_algoithm(op):
2526
print('\nSorting by ', algorithms[op], ' method...')
2627
print('\nOrdered array: ')
@@ -33,12 +34,11 @@ def test_algoithm(op):
3334

3435
if __name__ == '__main__':
3536

36-
algorithms = [ 'Bubble', 'Insertion', 'Selection', 'Quick', 'Merge',
37-
'Shell', 'Heap', 'Counting', 'Radix', 'Bucket', 'Gnome', 'Comb', 'Cocktail', 'All' ]
37+
algorithms = ['Bubble', 'Insertion', 'Selection', 'Quick', 'Merge',
38+
'Shell', 'Heap', 'Counting', 'Radix', 'Bucket', 'Gnome', 'Comb', 'Cocktail', 'All']
3839

3940
sort_methods = [bubble, insertion, selection, quick, merge, shell, heap, counting, radix, bucket, gnome, comb, cocktail]
40-
41-
41+
4242
while True:
4343
print('\n\n\tSorting Algorithms\n\n')
4444

@@ -52,7 +52,8 @@ def test_algoithm(op):
5252
print('\nOriginal array: ')
5353
print(vet)
5454

55-
print('\nChoose a Sort Algorithm.: ')
55+
print('\nChoose a Sort Algorithm: ')
56+
5657
for i, x in enumerate(algorithms):
5758
print(i, ' - ', x)
5859
op = input()
@@ -63,14 +64,3 @@ def test_algoithm(op):
6364
print('Invalid option.')
6465
else:
6566
test_algoithm(int(op))
66-
67-
68-
69-
70-
71-
72-
73-
74-
75-
76-

Diff for: src/sort/sort.py

+27-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
'''
1+
"""
22
Created on 22 de dez de 2016
33
44
@author: patterson
5-
'''
5+
"""
6+
67
import math
78

89

@@ -24,6 +25,7 @@ def bubble(nlist):
2425
exchanges = True
2526

2627
return nlist
28+
2729

2830
def insertion(nlist):
2931
nlist = list(nlist)
@@ -43,6 +45,7 @@ def insertion(nlist):
4345

4446
return nlist
4547

48+
4649
def selection(nlist):
4750
nlist = list(nlist)
4851
size = len(nlist)
@@ -59,6 +62,7 @@ def selection(nlist):
5962
nlist[i], nlist[lower] = nlist[lower], nlist[i]
6063

6164
return nlist
65+
6266

6367
def quick(nlist):
6468
nlist = list(nlist)
@@ -68,19 +72,20 @@ def quick(nlist):
6872
return nlist
6973

7074
pivot = nlist[0]
71-
lr = [x for x in nlist if x < pivot] # lower
72-
gt = [x for x in nlist[1:] if x >= pivot] # greater
75+
lr = [x for x in nlist if x < pivot] # lower
76+
gt = [x for x in nlist[1:] if x >= pivot] # greater
7377

7478
return quick(lr) + [pivot] + quick(gt)
7579

80+
7681
def merge(nlist):
7782
if len(nlist) < 2:
7883
return nlist
7984

8085
result, mid = list(), len(nlist) // 2
8186

82-
l = merge(nlist[:mid]) # left
83-
r = merge(nlist[mid:]) # right
87+
l = merge(nlist[:mid]) # left
88+
r = merge(nlist[mid:]) # right
8489

8590
while (len(l) > 0) and (len(r) > 0):
8691
if l[0] > r[0]:
@@ -92,6 +97,7 @@ def merge(nlist):
9297

9398
return result
9499

100+
95101
def shell(nlist):
96102
nlist = list(nlist)
97103
size = len(nlist)
@@ -115,6 +121,7 @@ def shell(nlist):
115121

116122
return nlist
117123

124+
118125
def heap(nlist):
119126
nlist = list(nlist)
120127
size = len(nlist)
@@ -131,6 +138,7 @@ def heap(nlist):
131138

132139
return nlist
133140

141+
134142
def _max_heap(nlist, root, end):
135143
while True:
136144
child = root * 2 + 1
@@ -144,7 +152,8 @@ def _max_heap(nlist, root, end):
144152
root = child
145153
else:
146154
break
147-
155+
156+
148157
def counting(alist):
149158
nlist = list(alist)
150159
size = len(nlist)
@@ -169,6 +178,7 @@ def counting(alist):
169178

170179
return nlist
171180

181+
172182
def radix(aList):
173183
nlist = list(aList)
174184
size = len(nlist)
@@ -178,15 +188,15 @@ def radix(aList):
178188

179189
RADIX = 10
180190
maxLength = False
181-
tmp , placement = -1, 1
191+
tmp, placement = -1, 1
182192

183193
while not maxLength:
184194
maxLength = True
185-
buckets = [list() for i in range( RADIX )]
195+
buckets = [list() for i in range(RADIX)]
186196

187-
for i in nlist:
197+
for i in nlist:
188198
tmp = int(i / placement)
189-
buckets[int(tmp % RADIX)].append( i )
199+
buckets[int(tmp % RADIX)].append(i)
190200
if maxLength and tmp > 0:
191201
maxLength = False
192202

@@ -201,6 +211,7 @@ def radix(aList):
201211

202212
return nlist
203213

214+
204215
def bucket(aList, bucketSize=5):
205216
nlist = list(aList)
206217
size = len(nlist)
@@ -228,6 +239,7 @@ def bucket(aList, bucketSize=5):
228239

229240
return nlist
230241

242+
231243
def gnome(aList):
232244
nlist = list(aList)
233245
size = len(nlist)
@@ -245,7 +257,8 @@ def gnome(aList):
245257
pivot += 1
246258

247259
return nlist
248-
260+
261+
249262
def comb(aList):
250263
nlist = list(aList)
251264
size = len(nlist)
@@ -268,7 +281,8 @@ def comb(aList):
268281
swaps = True
269282

270283
return nlist
271-
284+
285+
272286
def cocktail(aList):
273287
nlist = list(aList)
274288
size = len(nlist)
@@ -298,19 +312,3 @@ def cocktail(aList):
298312

299313
if not swapped:
300314
return nlist
301-
302-
303-
304-
305-
306-
307-
308-
309-
310-
311-
312-
313-
314-
315-
316-

0 commit comments

Comments
 (0)