Skip to content

Commit da9e4c9

Browse files
Bricksort parallel implemented (#221)
1 parent b530dc4 commit da9e4c9

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

pydatastructs/linear_data_structures/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from .algorithms import (
2424
merge_sort_parallel,
25-
brick_sort
25+
brick_sort,
26+
brick_sort_parallel
2627
)
2728
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
__all__ = [
88
'merge_sort_parallel',
9-
'brick_sort'
9+
'brick_sort',
10+
'brick_sort_parallel'
1011
]
1112

1213
def _merge(array, sl, el, sr, er, end, comp):
@@ -163,3 +164,72 @@ def brick_sort(array, **kwargs):
163164

164165
if _check_type(array, DynamicArray):
165166
array._modify(force=True)
167+
168+
def _brick_sort_swap(array, i, j, comp, is_sorted):
169+
if _comp(array[j], array[i], comp):
170+
array[i], array[j] = array[j], array[i]
171+
is_sorted[0] = False
172+
173+
def brick_sort_parallel(array, num_threads, **kwargs):
174+
"""
175+
Implements Concurrent Brick Sort / Odd Even sorting algorithm
176+
177+
Parameters
178+
==========
179+
180+
array: Array/list
181+
The array which is to be sorted.
182+
num_threads: int
183+
The maximum number of threads
184+
to be used for sorting.
185+
start: int
186+
The starting index of the portion
187+
which is to be sorted.
188+
Optional, by default 0
189+
end: int
190+
The ending index of the portion which
191+
is to be sorted.
192+
Optional, by default the index
193+
of the last position filled.
194+
comp: lambda/function
195+
The comparator which is to be used
196+
for sorting. If the function returns
197+
False then only swapping is performed.
198+
Optional, by default, less than or
199+
equal to is used for comparing two
200+
values.
201+
202+
Examples
203+
========
204+
205+
>>> from pydatastructs import OneDimensionalArray, brick_sort_parallel
206+
>>> arr = OneDimensionalArray(int,[3, 2, 1])
207+
>>> brick_sort_parallel(arr, num_threads=5)
208+
>>> [arr[0], arr[1], arr[2]]
209+
[1, 2, 3]
210+
>>> brick_sort_parallel(arr, num_threads=5, comp=lambda u, v: u > v)
211+
>>> [arr[0], arr[1], arr[2]]
212+
[3, 2, 1]
213+
214+
References
215+
==========
216+
217+
.. [1] https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
218+
"""
219+
220+
start = kwargs.get('start', 0)
221+
end = kwargs.get('end', len(array) - 1)
222+
comp = kwargs.get("comp", lambda u, v: u <= v)
223+
224+
is_sorted = [False]
225+
with ThreadPoolExecutor(max_workers=num_threads) as Executor:
226+
while is_sorted[0] is False:
227+
is_sorted[0] = True
228+
for i in range(start + 1, end, 2):
229+
Executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result()
230+
231+
for i in range(start, end, 2):
232+
Executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result()
233+
234+
if _check_type(array, DynamicArray):
235+
array._modify(force=True)

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pydatastructs import (
22
merge_sort_parallel, DynamicOneDimensionalArray,
3-
OneDimensionalArray, brick_sort)
3+
OneDimensionalArray, brick_sort, brick_sort_parallel)
4+
45
import random
56

67
def _test_common_sort(sort, *args, **kwargs):
@@ -44,3 +45,6 @@ def test_merge_sort_parallel():
4445

4546
def test_brick_sort():
4647
_test_common_sort(brick_sort)
48+
49+
def test_brick_sort_parallel():
50+
_test_common_sort(brick_sort_parallel, num_threads=3)

0 commit comments

Comments
 (0)