Skip to content

Commit 345d371

Browse files
authored
Update heap_sort.py
Added explanation for how heap-sort works and corresponds to the method heapify().
1 parent dfb83df commit 345d371

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

pygorithm/sorting/heap_sort.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
def sort(_list):
1414
"""
1515
heap sort algorithm
16+
Create the heap using heapify().
17+
This is an implementation of max-heap, so after bullding the heap, the max element is at the top (_list[0]).
18+
We move it to the end of the list (_list[end]), which will later become the sorted list.
19+
After moving this element to the end, we take the element in the end to the top and shift it down to its right location in the heap.
20+
We proceed to do the same for all elements in the heap, such that in the end we're left with the sorted list.
1621
1722
:param _list: list of values to sort
1823
:return: sorted values
1924
"""
20-
# TODO: Add description of how this works!
2125

2226
# create the heap
2327
heapify(_list)

0 commit comments

Comments
 (0)