Skip to content

Commit bbacdca

Browse files
Heapsort (#243)
1 parent ffc9cf9 commit bbacdca

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

pydatastructs/linear_data_structures/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
merge_sort_parallel,
2525
brick_sort,
2626
brick_sort_parallel,
27+
heapsort,
2728
matrix_multiply_parallel
2829
)
2930
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'merge_sort_parallel',
99
'brick_sort',
1010
'brick_sort_parallel',
11+
'heapsort',
1112
'matrix_multiply_parallel'
1213
]
1314

@@ -235,6 +236,64 @@ def brick_sort_parallel(array, num_threads, **kwargs):
235236
if _check_type(array, DynamicArray):
236237
array._modify(force=True)
237238

239+
def heapsort(array, **kwargs):
240+
"""
241+
Implements Heapsort algorithm.
242+
243+
Parameters
244+
==========
245+
246+
array: Array
247+
The array which is to be sorted.
248+
start: int
249+
The starting index of the portion
250+
which is to be sorted.
251+
Optional, by default 0
252+
end: int
253+
The ending index of the portion which
254+
is to be sorted.
255+
Optional, by default the index
256+
of the last position filled.
257+
258+
Examples
259+
========
260+
261+
>>> from pydatastructs import OneDimensionalArray, heapsort
262+
>>> arr = OneDimensionalArray(int,[3, 2, 1])
263+
>>> heapsort(arr)
264+
>>> [arr[0], arr[1], arr[2]]
265+
[1, 2, 3]
266+
267+
References
268+
==========
269+
270+
.. [1] https://en.wikipedia.org/wiki/Heapsort
271+
272+
Note
273+
====
274+
275+
This function does not support custom comparators as is the case with
276+
other sorting functions in this file.
277+
"""
278+
from pydatastructs.trees.heaps import BinaryHeap
279+
280+
start = kwargs.get('start', 0)
281+
end = kwargs.get('end', len(array) - 1)
282+
283+
h = BinaryHeap(heap_property="min")
284+
for i in range(start, end+1):
285+
if array[i] is not None:
286+
h.insert(array[i])
287+
array[i] = None
288+
289+
i = start
290+
while not h.is_empty:
291+
array[i] = h.extract().key
292+
i += 1
293+
294+
if _check_type(array, DynamicArray):
295+
array._modify(force=True)
296+
238297
def _matrix_multiply_helper(m1, m2, row, col):
239298
s = 0
240299
for i in range(len(m1)):

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pydatastructs import (
22
merge_sort_parallel, DynamicOneDimensionalArray,
33
OneDimensionalArray, brick_sort, brick_sort_parallel,
4-
matrix_multiply_parallel)
4+
heapsort, matrix_multiply_parallel)
55
from pydatastructs.utils.raises_util import raises
66
import random
77

@@ -50,6 +50,9 @@ def test_brick_sort():
5050
def test_brick_sort_parallel():
5151
_test_common_sort(brick_sort_parallel, num_threads=3)
5252

53+
def test_heapsort():
54+
_test_common_sort(heapsort)
55+
5356
def test_matrix_multiply_parallel():
5457
ODA = OneDimensionalArray
5558

0 commit comments

Comments
 (0)