mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Typehints + refactor (#2154)
This commit is contained in:
parent
9e2206e5fb
commit
b0c3c0fbf6
|
@ -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.
|
||||
|
||||
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]
|
||||
|
||||
>>> 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]
|
||||
|
||||
|
@ -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)}")
|
||||
|
|
Loading…
Reference in New Issue
Block a user