mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-25 17:32:36 +08:00
Odd-Even Transposition Sort (#769)
* -Added a single-threaded implementation of odd-even transposition sort. This is a modified bubble sort meant to work with multiple processors. Since this is running on a single thread, it has the same running time as bubble sort. * -Added a parallel implementation of Odd-Even Transposition sort This implementation uses multiprocessing to perform the swaps at each step of the algorithm simultaneously.
This commit is contained in:
32
sorts/Odd-Even_transposition_single-threaded.py
Normal file
32
sorts/Odd-Even_transposition_single-threaded.py
Normal file
@ -0,0 +1,32 @@
|
||||
"""
|
||||
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):
|
||||
for i in range(0, len(arr)):
|
||||
for i in range(i % 2, len(arr) - 1, 2):
|
||||
if arr[i + 1] < arr[i]:
|
||||
arr[i], arr[i + 1] = arr[i + 1], arr[i]
|
||||
print(*arr)
|
||||
|
||||
return arr
|
||||
|
||||
#creates a list and sorts it
|
||||
def main():
|
||||
list = []
|
||||
|
||||
for i in range(10, 0, -1):
|
||||
list.append(i)
|
||||
print("Initial List")
|
||||
print(*list)
|
||||
|
||||
list = OddEvenTransposition(list)
|
||||
|
||||
print("Sorted List\n")
|
||||
print(*list)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user