Skip to content

Commit

Permalink
Typehints + refactor (TheAlgorithms#2154)
Browse files Browse the repository at this point in the history
  • Loading branch information
l3str4nge authored Jun 25, 2020
1 parent 9e2206e commit b0c3c0f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions sorts/odd_even_transposition_single_threaded.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
"""
Source: https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
This is a non-parallelized implementation of odd-even transpostiion sort.
Normally the swaps in each set happen simultaneously, without that the algorithm
is no better than bubble sort.
"""


def OddEvenTransposition(arr):
def odd_even_transposition(arr: list) -> list:
"""
>>> OddEvenTransposition([5, 4, 3, 2, 1])
>>> odd_even_transposition([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]
>>> OddEvenTransposition([13, 11, 18, 0, -1])
>>> odd_even_transposition([13, 11, 18, 0, -1])
[-1, 0, 11, 13, 18]
>>> OddEvenTransposition([-.1, 1.1, .1, -2.9])
>>> odd_even_transposition([-.1, 1.1, .1, -2.9])
[-2.9, -0.1, 0.1, 1.1]
"""
for i in range(0, len(arr)):
for i in range(i % 2, len(arr) - 1, 2):
arr_size = len(arr)
for _ in range(arr_size):
for i in range(_ % 2, arr_size - 1, 2):
if arr[i + 1] < arr[i]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]

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

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

0 comments on commit b0c3c0f

Please sign in to comment.