Skip to content

Heapsort #243

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

Merged
merged 18 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
merge_sort_parallel,
brick_sort,
brick_sort_parallel,
heapsort,
matrix_multiply_parallel
)
__all__.extend(algorithms.__all__)
59 changes: 59 additions & 0 deletions pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'merge_sort_parallel',
'brick_sort',
'brick_sort_parallel',
'heapsort',
'matrix_multiply_parallel'
]

Expand Down Expand Up @@ -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)):
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down