Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bricksort parallel implemented #221

Merged
merged 17 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__)
75 changes: 74 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,75 @@ def brick_sort(array, **kwargs):

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

def _brick_sort_comp(array, i, j, comp):
if(_comp(array[j], array[i], comp)):
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
array[i], array[j] = array[j], array[i]
return False
return True
HarsheetKakar marked this conversation as resolved.
Show resolved Hide resolved

def brick_sort_parallel(array, num_threads, **kwargs):
"""
Implements Concurrent Brick Sort / Odd Even sorting algorithm

Parameters
==========

array: Array
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
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
========
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
>>> 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
==========
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
.. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-sort/
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
"""

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 is False:
is_sorted = True
futures = []
for i in range(start+1, end, 2):
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
futures.append(executor.submit(_brick_sort_comp, array, i, i+1, comp))
is_sorted = all(i.result() for i in futures)

futures = []
for i in range(start, end, 2):
futures.append(executor.submit(_brick_sort_comp, array, i, i+1, comp))
is_sorted = all(i.result() for i in futures)

if _check_type(array, DynamicArray):
array._modify(force=True)
HarsheetKakar marked this conversation as resolved.
Show resolved Hide resolved
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)