Skip to content

Commit

Permalink
Bricksort parallel implemented (codezonediitj#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarsheetKakar authored and Vanshika266 committed Apr 7, 2020
1 parent 7987ecf commit 30e19da
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from .algorithms import (
merge_sort_parallel,
brick_sort
brick_sort,
brick_sort_parallel
)
__all__.extend(algorithms.__all__)
72 changes: 71 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

__all__ = [
'merge_sort_parallel',
'brick_sort'
'brick_sort',
'brick_sort_parallel'
]

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

if _check_type(array, DynamicArray):
array._modify(force=True)

def _brick_sort_swap(array, i, j, comp, is_sorted):
if _comp(array[j], array[i], comp):
array[i], array[j] = array[j], array[i]
is_sorted[0] = False

def brick_sort_parallel(array, num_threads, **kwargs):
"""
Implements Concurrent Brick Sort / Odd Even sorting algorithm
Parameters
==========
array: Array/list
The array which is to be sorted.
num_threads: int
The maximum number of threads
to be used for sorting.
start: int
The starting index of the portion
which is to be sorted.
Optional, by default 0
end: int
The ending index of the portion which
is to be sorted.
Optional, by default the index
of the last position filled.
comp: lambda/function
The comparator which is to be used
for sorting. If the function returns
False then only swapping is performed.
Optional, by default, less than or
equal to is used for comparing two
values.
Examples
========
>>> from pydatastructs import OneDimensionalArray, brick_sort_parallel
>>> arr = OneDimensionalArray(int,[3, 2, 1])
>>> brick_sort_parallel(arr, num_threads=5)
>>> [arr[0], arr[1], arr[2]]
[1, 2, 3]
>>> brick_sort_parallel(arr, num_threads=5, comp=lambda u, v: u > v)
>>> [arr[0], arr[1], arr[2]]
[3, 2, 1]
References
==========
.. [1] https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
"""

start = kwargs.get('start', 0)
end = kwargs.get('end', len(array) - 1)
comp = kwargs.get("comp", lambda u, v: u <= v)

is_sorted = [False]
with ThreadPoolExecutor(max_workers=num_threads) as Executor:
while is_sorted[0] is False:
is_sorted[0] = True
for i in range(start + 1, end, 2):
Executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result()

for i in range(start, end, 2):
Executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result()

if _check_type(array, DynamicArray):
array._modify(force=True)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydatastructs import (
merge_sort_parallel, DynamicOneDimensionalArray,
OneDimensionalArray, brick_sort)
OneDimensionalArray, brick_sort, brick_sort_parallel)

import random

def _test_common_sort(sort, *args, **kwargs):
Expand Down Expand Up @@ -44,3 +45,6 @@ def test_merge_sort_parallel():

def test_brick_sort():
_test_common_sort(brick_sort)

def test_brick_sort_parallel():
_test_common_sort(brick_sort_parallel, num_threads=3)

0 comments on commit 30e19da

Please sign in to comment.