Typehints + refactor (#2154)

This commit is contained in:
mateuszz0000 2020-06-25 09:48:52 +02:00 committed by GitHub
parent 9e2206e5fb
commit b0c3c0fbf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
""" """
Source: https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
This is a non-parallelized implementation of odd-even transpostiion sort. This is a non-parallelized implementation of odd-even transpostiion sort.
Normally the swaps in each set happen simultaneously, without that the algorithm Normally the swaps in each set happen simultaneously, without that the algorithm
@ -6,19 +8,20 @@ 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] [1, 2, 3, 4, 5]
>>> OddEvenTransposition([13, 11, 18, 0, -1]) >>> odd_even_transposition([13, 11, 18, 0, -1])
[-1, 0, 11, 13, 18] [-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] [-2.9, -0.1, 0.1, 1.1]
""" """
for i in range(0, len(arr)): arr_size = len(arr)
for i in range(i % 2, len(arr) - 1, 2): for _ in range(arr_size):
for i in range(_ % 2, arr_size - 1, 2):
if arr[i + 1] < arr[i]: if arr[i + 1] < arr[i]:
arr[i], arr[i + 1] = arr[i + 1], arr[i] arr[i], arr[i + 1] = arr[i + 1], arr[i]
@ -27,4 +30,4 @@ def OddEvenTransposition(arr):
if __name__ == "__main__": if __name__ == "__main__":
arr = list(range(10, 0, -1)) arr = list(range(10, 0, -1))
print(f"Original: {arr}. Sorted: {OddEvenTransposition(arr)}") print(f"Original: {arr}. Sorted: {odd_even_transposition(arr)}")