Skip to content

Commit b0c3c0f

Browse files
authored
Typehints + refactor (TheAlgorithms#2154)
1 parent 9e2206e commit b0c3c0f

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
"""
2+
Source: https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
3+
24
This is a non-parallelized implementation of odd-even transpostiion sort.
35
46
Normally the swaps in each set happen simultaneously, without that the algorithm
57
is no better than bubble sort.
68
"""
79

810

9-
def OddEvenTransposition(arr):
11+
def odd_even_transposition(arr: list) -> list:
1012
"""
11-
>>> OddEvenTransposition([5, 4, 3, 2, 1])
13+
>>> odd_even_transposition([5, 4, 3, 2, 1])
1214
[1, 2, 3, 4, 5]
1315
14-
>>> OddEvenTransposition([13, 11, 18, 0, -1])
16+
>>> odd_even_transposition([13, 11, 18, 0, -1])
1517
[-1, 0, 11, 13, 18]
1618
17-
>>> OddEvenTransposition([-.1, 1.1, .1, -2.9])
19+
>>> odd_even_transposition([-.1, 1.1, .1, -2.9])
1820
[-2.9, -0.1, 0.1, 1.1]
1921
"""
20-
for i in range(0, len(arr)):
21-
for i in range(i % 2, len(arr) - 1, 2):
22+
arr_size = len(arr)
23+
for _ in range(arr_size):
24+
for i in range(_ % 2, arr_size - 1, 2):
2225
if arr[i + 1] < arr[i]:
2326
arr[i], arr[i + 1] = arr[i + 1], arr[i]
2427

@@ -27,4 +30,4 @@ def OddEvenTransposition(arr):
2730

2831
if __name__ == "__main__":
2932
arr = list(range(10, 0, -1))
30-
print(f"Original: {arr}. Sorted: {OddEvenTransposition(arr)}")
33+
print(f"Original: {arr}. Sorted: {odd_even_transposition(arr)}")

0 commit comments

Comments
 (0)