Skip to content

Commit 9484eb0

Browse files
Support comparable items in parallel odd-even sort
1 parent 2bddc10 commit 9484eb0

2 files changed

Lines changed: 42 additions & 26 deletions

File tree

‎sorts/odd_even_transposition_parallel.py‎

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,34 @@ def oe_process[T: Comparable](
4949
# we perform n swaps since after n swaps we know we are sorted
5050
# we *could* stop early if we are sorted already, but it takes as long to
5151
# find out we are sorted as it does to sort the list with this algorithm
52-
for i in range(10):
53-
if (i + position) % 2 == 0 and r_send is not None:
54-
# send your value to your right neighbor
55-
with process_lock:
56-
r_send[1].send(value)
57-
58-
# receive your right neighbor's value
59-
with process_lock:
60-
temp = rr_cv[0].recv()
61-
62-
# take the lower value since you are on the left
63-
value = min(value, temp)
64-
elif (i + position) % 2 != 0 and l_send is not None:
65-
# send your value to your left neighbor
66-
with process_lock:
67-
l_send[1].send(value)
68-
69-
# receive your left neighbor's value
70-
with process_lock:
71-
temp = lr_cv[0].recv()
72-
73-
# take the higher value since you are on the right
74-
value = max(value, temp)
75-
# after all swaps are performed, send the values back to main
76-
result_pipe[1].send(value)
52+
try:
53+
for i in range(10):
54+
if (i + position) % 2 == 0 and r_send is not None:
55+
# send your value to your right neighbor
56+
with process_lock:
57+
r_send[1].send(value)
58+
59+
# receive your right neighbor's value
60+
with process_lock:
61+
temp = rr_cv[0].recv()
62+
63+
# take the lower value since you are on the left
64+
value = min(value, temp)
65+
elif (i + position) % 2 != 0 and l_send is not None:
66+
# send your value to your left neighbor
67+
with process_lock:
68+
l_send[1].send(value)
69+
70+
# receive your left neighbor's value
71+
with process_lock:
72+
temp = lr_cv[0].recv()
73+
74+
# take the higher value since you are on the right
75+
value = max(value, temp)
76+
# after all swaps are performed, send the value back to main
77+
result_pipe[1].send((False, value))
78+
except Exception as e: # noqa: BLE001
79+
result_pipe[1].send((True, e))
7780

7881

7982
def odd_even_transposition[T: Comparable](arr: list[T]) -> list[T]:
@@ -105,6 +108,9 @@ def odd_even_transposition[T: Comparable](arr: list[T]) -> list[T]:
105108
>>> odd_even_transposition(unsorted_list) == sorted(unsorted_list)
106109
True
107110
"""
111+
if not arr:
112+
return arr
113+
108114
# spawn method is considered safer than fork
109115
multiprocessing_context = mp.get_context("spawn")
110116

@@ -179,7 +185,12 @@ def odd_even_transposition[T: Comparable](arr: list[T]) -> list[T]:
179185

180186
# wait for the processes to end and write their values to the list
181187
for p in range(len(result_pipe)):
182-
arr[p] = result_pipe[p][0].recv()
188+
is_error, result = result_pipe[p][0].recv()
189+
if is_error:
190+
for proc in process_array_:
191+
proc.terminate()
192+
raise result
193+
arr[p] = result
183194
process_array_[p].join()
184195
return arr
185196

‎tests/test_sorts.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
from sorts.merge_insertion_sort import merge_insertion_sort
4242
from sorts.merge_sort import merge_sort
4343
from sorts.odd_even_sort import odd_even_sort
44+
from sorts.odd_even_transposition_parallel import (
45+
odd_even_transposition as odd_even_transposition_parallel,
46+
)
4447
from sorts.odd_even_transposition_single_threaded import odd_even_transposition
4548
from sorts.pancake_sort import pancake_sort
4649
from sorts.patience_sort import patience_sort
@@ -116,6 +119,7 @@ def test_intro_sort_heap_fallback_preserves_surrounding_items(max_depth: int) ->
116119
merge_sort,
117120
odd_even_sort,
118121
odd_even_transposition,
122+
odd_even_transposition_parallel,
119123
pancake_sort,
120124
patience_sort,
121125
power_sort,
@@ -198,6 +202,7 @@ def test_rec_insertion_sort(case) -> None:
198202
merge_sort,
199203
odd_even_sort,
200204
odd_even_transposition,
205+
odd_even_transposition_parallel,
201206
pancake_sort,
202207
patience_sort,
203208
power_sort,

0 commit comments

Comments
 (0)