From 064c9ff150fc244e90cdbea3b47da45f2fb03493 Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Tue, 24 Mar 2020 17:40:57 +0530 Subject: [PATCH 01/15] brick_sort implemented --- .../linear_data_structures/__init__.py | 3 +- .../linear_data_structures/algorithms.py | 65 ++++++++++++++++++- .../tests/test_algorithms.py | 16 ++++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/pydatastructs/linear_data_structures/__init__.py b/pydatastructs/linear_data_structures/__init__.py index f5dcce94c..510c421e3 100644 --- a/pydatastructs/linear_data_structures/__init__.py +++ b/pydatastructs/linear_data_structures/__init__.py @@ -21,6 +21,7 @@ __all__.extend(linked_lists.__all__) from .algorithms import ( - merge_sort_parallel + merge_sort_parallel, + brick_sort ) __all__.extend(algorithms.__all__) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 8bca46feb..e19a0ea26 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -5,7 +5,8 @@ from math import log, floor __all__ = [ - 'merge_sort_parallel' + 'merge_sort_parallel', + 'brick_sort' ] def _merge(array, sl, el, sr, er, end, comp): @@ -104,3 +105,65 @@ def merge_sort_parallel(array, num_threads, **kwargs): if _check_type(array, DynamicArray): array._modify(force=True) + +def brick_sort(array: OneDimensionalArray, **kwargs): + """ + Implements Brick Sort / Odd Even sorting algorithm + + Parameters + ========== + + array: Array + The array which is to be sorted. + 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 + >>> arr = OneDimensionalArray(int,[3, 2, 1]) + >>> brick_sort(arr) + >>> print(list(arr)) + [1, 2, 3] + >>> brick_sort(arr, comp=lambda u, v: u > v) + >>> print(list(arr)) + [3, 2, 1] + + References + ========== + .. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-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 + + while is_sorted is False: + + is_sorted = True + + for i in range(start+1, end, 2): + if comp(array[i+1], array[i]): + array[i], array[i+1] = array[i+1], array[i] + is_sorted = False + + for i in range(start, end, 2): + if comp(array[i+1], array[i]): + array[i], array[i+1] = array[i+1], array[i] + is_sorted = False diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index d33f802c5..93a47d2f9 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -1,6 +1,6 @@ from pydatastructs import ( merge_sort_parallel, DynamicOneDimensionalArray, - OneDimensionalArray) + OneDimensionalArray, brick_sort) import random def test_merge_sort_parallel(): @@ -31,3 +31,17 @@ def test_merge_sort_parallel(): 709, 910] merge_sort_parallel(arr, 5, start=2, end=5) assert arr._data == expected_arr + +def test_brick_sort(): + + random.seed(1000) + n = 10 + arr = OneDimensionalArray(int, [random.randint(1,1000) for _ in range(n)]) + b = sorted(arr) + brick_sort(arr) + assert b == list(arr) + + arr = OneDimensionalArray(int, [random.randint(1,1000) for _ in range(n)]) + brick_sort(arr, comp=lambda u, v: u>v) + b = list(reversed(sorted(arr))) + assert b == list(arr) From 457b9f8c50d469a0edd0c8be4575769c833d4c1c Mon Sep 17 00:00:00 2001 From: Harsheet Kakar <42893005+HarsheetKakar@users.noreply.github.com> Date: Wed, 25 Mar 2020 00:53:23 +0530 Subject: [PATCH 02/15] Update pydatastructs/linear_data_structures/algorithms.py Co-Authored-By: Gagandeep Singh --- pydatastructs/linear_data_structures/algorithms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index e19a0ea26..3bb0bbc4a 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -106,7 +106,7 @@ def merge_sort_parallel(array, num_threads, **kwargs): if _check_type(array, DynamicArray): array._modify(force=True) -def brick_sort(array: OneDimensionalArray, **kwargs): +def brick_sort(array, **kwargs): """ Implements Brick Sort / Odd Even sorting algorithm From 2324a6aba9c556c548629172983d0ab5c5d15685 Mon Sep 17 00:00:00 2001 From: Harsheet Kakar <42893005+HarsheetKakar@users.noreply.github.com> Date: Wed, 25 Mar 2020 00:53:42 +0530 Subject: [PATCH 03/15] Update pydatastructs/linear_data_structures/algorithms.py Co-Authored-By: Gagandeep Singh --- pydatastructs/linear_data_structures/algorithms.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 3bb0bbc4a..f8a0e6a07 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -147,22 +147,17 @@ def brick_sort(array, **kwargs): ========== .. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-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 - while is_sorted is False: - is_sorted = True - for i in range(start+1, end, 2): if comp(array[i+1], array[i]): array[i], array[i+1] = array[i+1], array[i] is_sorted = False - for i in range(start, end, 2): if comp(array[i+1], array[i]): array[i], array[i+1] = array[i+1], array[i] From 7d5ad0e050fffb5c2fe26388947cef409aa33c1f Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Wed, 25 Mar 2020 01:11:08 +0530 Subject: [PATCH 04/15] Dynamic array implementation added --- pydatastructs/linear_data_structures/algorithms.py | 5 ++++- .../linear_data_structures/tests/test_algorithms.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index f8a0e6a07..683e25e4d 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -148,7 +148,7 @@ def brick_sort(array, **kwargs): .. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-sort/ """ start = kwargs.get('start', 0) - end = kwargs.get('end', len(array) - 1) + end = kwargs.get('end', array._last_pos_filled if _check_type(array, DynamicArray) else len(array) - 1) comp = kwargs.get("comp", lambda u, v: u <= v) is_sorted = False @@ -162,3 +162,6 @@ def brick_sort(array, **kwargs): if comp(array[i+1], array[i]): array[i], array[i+1] = array[i+1], array[i] is_sorted = False + + if _check_type(array, DynamicArray): + array._modify(force=True) diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index 93a47d2f9..0ad0f29a0 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -45,3 +45,12 @@ def test_brick_sort(): brick_sort(arr, comp=lambda u, v: u>v) b = list(reversed(sorted(arr))) assert b == list(arr) + + arr = DynamicOneDimensionalArray(int, 0) + for i in range(n): + arr.append(random.randint(1,1000)) + + x = [arr[i] for i in range(arr._last_pos_filled+1)] + b = sorted(x) + brick_sort(arr) + assert b == [arr[i] for i in range(arr._last_pos_filled+1)] From 14a0c5286647b40c7e5f43c118697db947505220 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 25 Mar 2020 21:47:54 +0530 Subject: [PATCH 05/15] Apply suggestions from code review --- pydatastructs/linear_data_structures/algorithms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 683e25e4d..108278242 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -148,18 +148,18 @@ def brick_sort(array, **kwargs): .. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-sort/ """ start = kwargs.get('start', 0) - end = kwargs.get('end', array._last_pos_filled if _check_type(array, DynamicArray) else len(array) - 1) + end = kwargs.get('end', len(array) - 1) comp = kwargs.get("comp", lambda u, v: u <= v) is_sorted = False while is_sorted is False: is_sorted = True for i in range(start+1, end, 2): - if comp(array[i+1], array[i]): + if _comp(array[i+1], array[i], comp): array[i], array[i+1] = array[i+1], array[i] is_sorted = False for i in range(start, end, 2): - if comp(array[i+1], array[i]): + if _comp(array[i+1], array[i], comp): array[i], array[i+1] = array[i+1], array[i] is_sorted = False From 9eaebbeb3bb4217dadadd8d741580ddeeb7c3dec Mon Sep 17 00:00:00 2001 From: Harsheet Kakar <42893005+HarsheetKakar@users.noreply.github.com> Date: Wed, 25 Mar 2020 21:51:08 +0530 Subject: [PATCH 06/15] Update pydatastructs/linear_data_structures/algorithms.py Co-Authored-By: Gagandeep Singh --- pydatastructs/linear_data_structures/algorithms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 108278242..6f53a154a 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -137,7 +137,7 @@ def brick_sort(array, **kwargs): >>> from pydatastructs import OneDimensionalArray, brick_sort >>> arr = OneDimensionalArray(int,[3, 2, 1]) >>> brick_sort(arr) - >>> print(list(arr)) + >>> [arr[0], arr[1], arr[2]] [1, 2, 3] >>> brick_sort(arr, comp=lambda u, v: u > v) >>> print(list(arr)) From 00864c785f609a2b3bfb3cecceec41034e9db951 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 25 Mar 2020 21:52:27 +0530 Subject: [PATCH 07/15] Update pydatastructs/linear_data_structures/algorithms.py --- pydatastructs/linear_data_structures/algorithms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 6f53a154a..c5622c306 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -140,7 +140,7 @@ def brick_sort(array, **kwargs): >>> [arr[0], arr[1], arr[2]] [1, 2, 3] >>> brick_sort(arr, comp=lambda u, v: u > v) - >>> print(list(arr)) + >>> [arr[0], arr[1], arr[2]] [3, 2, 1] References From 7724e2951aafd0fd3393b7476fddf8462c0cb59b Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Wed, 25 Mar 2020 22:47:51 +0530 Subject: [PATCH 08/15] common test added --- .../tests/test_algorithms.py | 34 +++++-------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index 7d2d7d15b..ef14b2ec4 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -3,8 +3,7 @@ OneDimensionalArray, brick_sort) import random -def test_merge_sort_parallel(): - +def _test_common_sort(sort, *args, **kwargs): random.seed(1000) n = random.randint(10, 20) @@ -19,9 +18,9 @@ def test_merge_sort_parallel(): None, None, None, None, None, None, None, None, None, None, None, None, None, None] - merge_sort_parallel(arr, 5, start=2, end=10) + sort(arr, *args, **kwargs, start=2, end=10) assert arr._data == expected_arr - merge_sort_parallel(arr, 5) + sort(arr, *args, **kwargs) expected_arr = [102, 134, 228, 247, 362, 373, 448, 480, 548, 686, 688, 696, 779, None, None, None, None, None, None, @@ -37,28 +36,11 @@ def test_merge_sort_parallel(): expected_arr = [42, 695, 147, 500, 768, 998, 473, 732, 728, 426, 709, 910] - merge_sort_parallel(arr, 5, start=2, end=5) + sort(arr, *args, **kwargs, start=2, end=5) assert arr._data == expected_arr -def test_brick_sort(): - - random.seed(1000) - n = 10 - arr = OneDimensionalArray(int, [random.randint(1,1000) for _ in range(n)]) - b = sorted(arr) - brick_sort(arr) - assert b == list(arr) - - arr = OneDimensionalArray(int, [random.randint(1,1000) for _ in range(n)]) - brick_sort(arr, comp=lambda u, v: u>v) - b = list(reversed(sorted(arr))) - assert b == list(arr) - - arr = DynamicOneDimensionalArray(int, 0) - for i in range(n): - arr.append(random.randint(1,1000)) +def test_merge_sort_parallel(): + _test_common_sort(merge_sort_parallel, num_threads=5) - x = [arr[i] for i in range(arr._last_pos_filled+1)] - b = sorted(x) - brick_sort(arr) - assert b == [arr[i] for i in range(arr._last_pos_filled+1)] +def test_brick_sort(): + _test_common_sort(brick_sort) From 1b68e8bcfb8c8a346441acf938e847b42894e91f Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Fri, 27 Mar 2020 12:16:04 +0530 Subject: [PATCH 09/15] Parallel bricksort implemented --- .../linear_data_structures/__init__.py | 3 +- .../linear_data_structures/algorithms.py | 69 ++++++++++++++++++- .../tests/test_algorithms.py | 5 +- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/pydatastructs/linear_data_structures/__init__.py b/pydatastructs/linear_data_structures/__init__.py index 510c421e3..eb9cee095 100644 --- a/pydatastructs/linear_data_structures/__init__.py +++ b/pydatastructs/linear_data_structures/__init__.py @@ -22,6 +22,7 @@ from .algorithms import ( merge_sort_parallel, - brick_sort + brick_sort, + brick_sort_parallel ) __all__.extend(algorithms.__all__) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index b556d7e7f..e8cf08adb 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -6,7 +6,8 @@ __all__ = [ 'merge_sort_parallel', - 'brick_sort' + 'brick_sort', + 'brick_sort_parallel' ] def _merge(array, sl, el, sr, er, end, comp): @@ -163,3 +164,69 @@ def brick_sort(array, **kwargs): if _check_type(array, DynamicArray): array._modify(force=True) + +def _brick_sort_parallel_helper(array, start, end, comp, o): + is_sorted = True + for i in range(start+o, end, 2): + if _comp(array[i+1], array[i], comp): + array[i], array[i+1] = array[i+1], array[i] + is_sorted = False + return is_sorted + +def brick_sort_parallel(array, **kwargs): + """ + Implements Concurrent Brick Sort / Odd Even sorting algorithm + + Parameters + ========== + + array: Array + The array which is to be sorted. + 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) + >>> [arr[0], arr[1], arr[2]] + [1, 2, 3] + >>> brick_sort_parallel(arr, comp=lambda u, v: u > v) + >>> [arr[0], arr[1], arr[2]] + [3, 2, 1] + + References + ========== + .. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-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 + while is_sorted is False: + is_sorted = True + with ThreadPoolExecutor(max_workers=2) as executor: + t1 = executor.submit(_brick_sort_parallel_helper, array, start, end, comp, 1) + t2 = executor.submit(_brick_sort_parallel_helper, array, start, end, comp, 0) + + is_sorted = t1.result() and t2.result() + + if _check_type(array, DynamicArray): + array._modify(force=True) diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index ef14b2ec4..63523cff1 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -1,6 +1,6 @@ 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): @@ -44,3 +44,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) From b89b0402f3b06ab983a8d5669ae60fab018fa0b9 Mon Sep 17 00:00:00 2001 From: Harsheet Kakar <42893005+HarsheetKakar@users.noreply.github.com> Date: Fri, 27 Mar 2020 12:25:13 +0530 Subject: [PATCH 10/15] Update test_algorithms.py --- .../linear_data_structures/tests/test_algorithms.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index b49038ec8..bed3e5673 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -1,10 +1,7 @@ from pydatastructs import ( merge_sort_parallel, DynamicOneDimensionalArray, -<<<<<<< HEAD OneDimensionalArray, brick_sort, brick_sort_parallel) -======= - OneDimensionalArray, brick_sort) ->>>>>>> 6cc969c0c55ce348097025d917060d585f8054dd + import random def _test_common_sort(sort, *args, **kwargs): @@ -50,4 +47,4 @@ def test_brick_sort(): _test_common_sort(brick_sort) def test_brick_sort_parallel(): - _test_common_sort(brick_sort_parallel) \ No newline at end of file + _test_common_sort(brick_sort_parallel) From a7d1e9cc9dbf01fb7b8c391122dc7af75f9a8c7f Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Mon, 30 Mar 2020 15:14:08 +0530 Subject: [PATCH 11/15] optimized algo updated --- .../linear_data_structures/algorithms.py | 42 +++++++++++-------- .../tests/test_algorithms.py | 2 +- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index e8cf08adb..d6844b7b3 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -165,15 +165,13 @@ def brick_sort(array, **kwargs): if _check_type(array, DynamicArray): array._modify(force=True) -def _brick_sort_parallel_helper(array, start, end, comp, o): - is_sorted = True - for i in range(start+o, end, 2): - if _comp(array[i+1], array[i], comp): - array[i], array[i+1] = array[i+1], array[i] - is_sorted = False - return is_sorted - -def brick_sort_parallel(array, **kwargs): +def _brick_sort_comp(array, i, j, comp): + if(_comp(array[j], array[i], comp)): + array[i], array[j] = array[j], array[i] + return False + return True + +def brick_sort_parallel(array, num_threads, **kwargs): """ Implements Concurrent Brick Sort / Odd Even sorting algorithm @@ -182,6 +180,9 @@ def brick_sort_parallel(array, **kwargs): array: Array 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. @@ -203,10 +204,10 @@ def brick_sort_parallel(array, **kwargs): ======== >>> from pydatastructs import OneDimensionalArray, brick_sort_parallel >>> arr = OneDimensionalArray(int,[3, 2, 1]) - >>> brick_sort_parallel(arr) + >>> brick_sort_parallel(arr, num_threads=5) >>> [arr[0], arr[1], arr[2]] [1, 2, 3] - >>> brick_sort_parallel(arr, comp=lambda u, v: u > v) + >>> brick_sort_parallel(arr, num_threads=5, comp=lambda u, v: u > v) >>> [arr[0], arr[1], arr[2]] [3, 2, 1] @@ -220,13 +221,18 @@ def brick_sort_parallel(array, **kwargs): comp = kwargs.get("comp", lambda u, v: u <= v) is_sorted = False - while is_sorted is False: - is_sorted = True - with ThreadPoolExecutor(max_workers=2) as executor: - t1 = executor.submit(_brick_sort_parallel_helper, array, start, end, comp, 1) - t2 = executor.submit(_brick_sort_parallel_helper, array, start, end, comp, 0) - - is_sorted = t1.result() and t2.result() + 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): + 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) diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index bed3e5673..f590b5628 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -47,4 +47,4 @@ def test_brick_sort(): _test_common_sort(brick_sort) def test_brick_sort_parallel(): - _test_common_sort(brick_sort_parallel) + _test_common_sort(brick_sort_parallel, num_threads=3) From 3845c11955382f02aa01a38675ec66c142a387fd Mon Sep 17 00:00:00 2001 From: Harsheet-saxena Date: Mon, 30 Mar 2020 17:28:25 +0530 Subject: [PATCH 12/15] _brick_sort_swap updated --- .../linear_data_structures/algorithms.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index d6844b7b3..0fc74953e 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -165,11 +165,10 @@ def brick_sort(array, **kwargs): if _check_type(array, DynamicArray): array._modify(force=True) -def _brick_sort_comp(array, i, j, comp): +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] - return False - return True + is_sorted[0] = False def brick_sort_parallel(array, num_threads, **kwargs): """ @@ -220,19 +219,15 @@ def brick_sort_parallel(array, num_threads, **kwargs): end = kwargs.get('end', len(array) - 1) comp = kwargs.get("comp", lambda u, v: u <= v) - is_sorted = False + is_sorted = [False] with ThreadPoolExecutor(max_workers=num_threads) as executor: - while is_sorted is False: - is_sorted = True - futures = [] + while is_sorted[0] is False: + is_sorted[0] = True for i in range(start+1, end, 2): - futures.append(executor.submit(_brick_sort_comp, array, i, i+1, comp)) - is_sorted = all(i.result() for i in futures) + executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result() - 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) + executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result() if _check_type(array, DynamicArray): array._modify(force=True) From aac9f281ea5e226ed216c8e9aa98a3839d26ef5b Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 30 Mar 2020 19:20:00 +0530 Subject: [PATCH 13/15] Apply suggestions from code review --- pydatastructs/linear_data_structures/algorithms.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 0fc74953e..9ce86bfd9 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -166,7 +166,7 @@ def brick_sort(array, **kwargs): array._modify(force=True) def _brick_sort_swap(array, i, j, comp, is_sorted): - if(_comp(array[j], array[i], comp)): + if _comp(array[j], array[i], comp): array[i], array[j] = array[j], array[i] is_sorted[0] = False @@ -177,7 +177,7 @@ def brick_sort_parallel(array, num_threads, **kwargs): Parameters ========== - array: Array + array: Array/list The array which is to be sorted. num_threads: int The maximum number of threads @@ -212,7 +212,8 @@ def brick_sort_parallel(array, num_threads, **kwargs): References ========== - .. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-sort/ + + .. [1] https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort """ start = kwargs.get('start', 0) @@ -223,7 +224,7 @@ def brick_sort_parallel(array, num_threads, **kwargs): 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): + 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): From b6fa4c8e6e3f5382add1809922e415fe0a77be01 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 30 Mar 2020 19:21:43 +0530 Subject: [PATCH 14/15] Apply suggestions from code review --- pydatastructs/linear_data_structures/algorithms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 9ce86bfd9..654f00221 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -221,14 +221,14 @@ def brick_sort_parallel(array, num_threads, **kwargs): comp = kwargs.get("comp", lambda u, v: u <= v) is_sorted = [False] - with ThreadPoolExecutor(max_workers=num_threads) as executor: + 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() + 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() + Executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result() if _check_type(array, DynamicArray): array._modify(force=True) From 278b0477c88dbe50ebd6e2b7e16c52450309e7c9 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 30 Mar 2020 19:49:23 +0530 Subject: [PATCH 15/15] Apply suggestions from code review --- pydatastructs/linear_data_structures/algorithms.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 654f00221..dc3d8d6ff 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -201,6 +201,7 @@ def brick_sort_parallel(array, num_threads, **kwargs): Examples ======== + >>> from pydatastructs import OneDimensionalArray, brick_sort_parallel >>> arr = OneDimensionalArray(int,[3, 2, 1]) >>> brick_sort_parallel(arr, num_threads=5)