PEP style and tree traversals · zinating/algorithms-python@9deae5d · GitHub
Skip to content

Commit 9deae5d

Browse files
PEP style and tree traversals
1 parent 0c409f3 commit 9deae5d

7 files changed

Lines changed: 115 additions & 15 deletions

File tree

sorts/bogosort.py

Lines changed: 4 additions & 3 deletions

sorts/bubble_sort.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
For manual testing run:
1010
python bubble_sort.py
1111
"""
12+
1213
from __future__ import print_function
1314

1415

sorts/heap_sort.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from __future__ import print_function
1414

15+
1516
def heapify(unsorted, index, heap_size):
1617
largest = index
1718
left_index = 2 * index + 1
@@ -26,6 +27,7 @@ def heapify(unsorted, index, heap_size):
2627
unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest]
2728
heapify(unsorted, largest, heap_size)
2829

30+
2931
def heap_sort(unsorted):
3032
'''
3133
Pure implementation of the heap sort algorithm in Python
@@ -44,7 +46,7 @@ def heap_sort(unsorted):
4446
[-45, -5, -2]
4547
'''
4648
n = len(unsorted)
47-
for i in range(n//2 - 1, -1, -1):
49+
for i in range(n // 2 - 1, -1, -1):
4850
heapify(unsorted, i, n)
4951
for i in range(n - 1, 0, -1):
5052
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]

sorts/insertion_sort.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def insertion_sort(collection):
3030
[-45, -5, -2]
3131
"""
3232
for index in range(1, len(collection)):
33-
while 0 < index and collection[index] < collection[index-1]:
34-
collection[index], collection[index-1] = collection[index-1], collection[index]
33+
while 0 < index and collection[index] < collection[index - 1]:
34+
collection[index], collection[
35+
index - 1] = collection[index - 1], collection[index]
3536
index -= 1
3637

3738
return collection

sorts/selection_sort.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def selection_sort(collection):
1717
:param collection: some mutable ordered collection with heterogeneous
1818
comparable items inside
1919
:return: the same collection ordered by ascending
20-
20+
2121
2222
Examples:
2323
>>> selection_sort([0, 5, 3, 2, 2])
@@ -29,7 +29,7 @@ def selection_sort(collection):
2929
>>> selection_sort([-2, -5, -45])
3030
[-45, -5, -2]
3131
"""
32-
32+
3333
length = len(collection)
3434
for i in range(length):
3535
least = i

sorts/shell_sort.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,30 @@ def shell_sort(collection):
1717
:param collection: Some mutable ordered collection with heterogeneous
1818
comparable items inside
1919
:return: the same collection ordered by ascending
20-
20+
2121
>>> shell_sort([0, 5, 3, 2, 2])
2222
[0, 2, 2, 3, 5]
2323
2424
>>> shell_sort([])
2525
[]
26-
27-
>>> shell_sort([-2, -5, -45])
26+
27+
>>> shell_sort([-2, -5, -45])
2828
[-45, -5, -2]
2929
"""
3030
# Marcin Ciura's gap sequence
3131
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
32-
32+
3333
for gap in gaps:
3434
i = gap
3535
while i < len(collection):
3636
temp = collection[i]
3737
j = i
38-
while j >= gap and collection[j-gap] > temp:
38+
while j >= gap and collection[j - gap] > temp:
3939
collection[j] = collection[j - gap]
4040
j -= gap
4141
collection[j] = temp
4242
i += 1
43-
44-
43+
4544
return collection
4645

4746
if __name__ == '__main__':

traverals/tree_traversals.py

Lines changed: 96 additions & 0 deletions

0 commit comments

Comments
 (0)