Skip to content

Refactor odd_even_transposition_single_threaded #2154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}")