diff --git a/pydatastructs/linear_data_structures/__init__.py b/pydatastructs/linear_data_structures/__init__.py index c9fb91692..1f660038d 100644 --- a/pydatastructs/linear_data_structures/__init__.py +++ b/pydatastructs/linear_data_structures/__init__.py @@ -24,6 +24,7 @@ merge_sort_parallel, brick_sort, brick_sort_parallel, + heapsort, matrix_multiply_parallel ) __all__.extend(algorithms.__all__) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index f7db3820d..f5eb2358d 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -8,6 +8,7 @@ 'merge_sort_parallel', 'brick_sort', 'brick_sort_parallel', + 'heapsort', 'matrix_multiply_parallel' ] @@ -235,6 +236,64 @@ def brick_sort_parallel(array, num_threads, **kwargs): if _check_type(array, DynamicArray): array._modify(force=True) +def heapsort(array, **kwargs): + """ + Implements Heapsort 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. + + Examples + ======== + + >>> from pydatastructs import OneDimensionalArray, heapsort + >>> arr = OneDimensionalArray(int,[3, 2, 1]) + >>> heapsort(arr) + >>> [arr[0], arr[1], arr[2]] + [1, 2, 3] + + References + ========== + + .. [1] https://en.wikipedia.org/wiki/Heapsort + + Note + ==== + + This function does not support custom comparators as is the case with + other sorting functions in this file. + """ + from pydatastructs.trees.heaps import BinaryHeap + + start = kwargs.get('start', 0) + end = kwargs.get('end', len(array) - 1) + + h = BinaryHeap(heap_property="min") + for i in range(start, end+1): + if array[i] is not None: + h.insert(array[i]) + array[i] = None + + i = start + while not h.is_empty: + array[i] = h.extract().key + i += 1 + + if _check_type(array, DynamicArray): + array._modify(force=True) + def _matrix_multiply_helper(m1, m2, row, col): s = 0 for i in range(len(m1)): diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index 24ad6a0a9..1fc24e8ea 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -1,7 +1,7 @@ from pydatastructs import ( merge_sort_parallel, DynamicOneDimensionalArray, OneDimensionalArray, brick_sort, brick_sort_parallel, - matrix_multiply_parallel) + heapsort, matrix_multiply_parallel) from pydatastructs.utils.raises_util import raises import random @@ -50,6 +50,9 @@ def test_brick_sort(): def test_brick_sort_parallel(): _test_common_sort(brick_sort_parallel, num_threads=3) +def test_heapsort(): + _test_common_sort(heapsort) + def test_matrix_multiply_parallel(): ODA = OneDimensionalArray