Skip to content
Open
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
24 changes: 20 additions & 4 deletions heaps/heap_sort.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@


def heap_sort(list):
from heaps.min_heap import MinHeap
def heap_sort(nums):
""" This method uses a heap to sort an array.
Time Complexity: ?
Space Complexity: ?
Comment on lines 4 to 5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 What are the time and space complexities for heap_sort?

Your approach looks good. To heap sort, we build a heap from the data to be sorted, then we remove the values from the heap, which gives them back to us in order. What does this mean for the time and space complexity?

"""
pass


heap = MinHeap()

for num in nums:
heap.add(num)

#adding everything to the minheap, which sorts everything

index = 0

#then we are printing them in order
while not heap.empty():
nums[index] = heap.remove()
index += 1
Comment on lines +19 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the since this isn't a fully in-place solution (the MinHeap has a O(n) internal store), we don't necessarily need to modify the passed in list. The tests are written to check the return value, so we could unpack the heap into a new result list to avoid mutating the input.

    result = []
    while not heap.empty():
        result.append(heap.remove())

    return result



return nums
74 changes: 65 additions & 9 deletions heaps/min_heap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import re


class HeapNode:

def __init__(self, key, value):
Expand All @@ -19,21 +22,46 @@ def __init__(self):
def add(self, key, value = None):
""" This method adds a HeapNode instance to the heap
If value == None the new node's value should be set to key
Time Complexity: ?
Time Complexity: O(log n)
Space Complexity: ?
Comment on lines +25 to 26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Your time complexity looks good, but what about the space complexity?

"""
pass
if value == None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Prefer using is to compare to None

value = key
node = HeapNode(key, value)
#store is my array, which holds the heap
#so here i am adding the node to the end of my heap
self.store.append(node)
#but wait! there's more!
#this means that the heap is out of order
#we need to "heap up" aka bubble up this node until it's in the correct position
#the heap up method is recursive
self.heap_up(len(self.store) - 1)


def remove(self):
""" This method removes and returns an element from the heap
maintaining the heap structure
Time Complexity: ?
(It removes the smallest item)
And remember that since this is a min heap, we have the root element at index 0

Time Complexity: O(log n)
Space Complexity: ?
Comment on lines +47 to 48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Again, time complexity looks good, but what about the space complexity?

"""
pass

if len(self.store) == 0:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 We have an empty helper we could use here

return None

#best way to do this is to swap the last with first,
#pop the new last elem,
#heap down from there

self.swap(0, len(self.store) - 1)
min = self.store.pop()
self.heap_down(0)

return min.value



def __str__(self):
""" This method lets you print the heap, when you're testing your app.
"""
Expand All @@ -47,8 +75,8 @@ def empty(self):
Time complexity: ?
Space complexity: ?
Comment on lines 75 to 76

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 What are the time and space complexities for empty?

"""
pass

if not self.store:
return True
Comment on lines +78 to +79

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 An empty list is falsy, but what would happens if the list were not empty?

Python would fall off the end of the function, with the default None return value. But since we're returning a boolean from one path, we should be consistent everywhere.

        if not self.store:
            return True
        else:
            return False

which simplifies to

        return not self.store


def heap_up(self, index):
""" This helper method takes an index and
Expand All @@ -60,15 +88,43 @@ def heap_up(self, index):
Time complexity: ?
Space complexity: ?
Comment on lines 88 to 89

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 What are the time and space complexities for heap_up?

"""
pass
if index == 0:
return

parent = (index - 1) // 2
store = self.store
if store[parent].key > store[index].key:
self.swap(parent, index)
self.heap_up(parent)
#we do this recursively
#so we check until everything is in the right place



def heap_down(self, index):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice approach of first determining the candidate child, then deciding whether the swap is required.

Though not prompted, consider what the time and space complexity is for heap_down. Does it differ from heap_up at all? What impact does the recursive call have on the complexity? Could this be improved?

""" This helper method takes an index and
moves the corresponding element down the heap if it's
larger than either of its children and continues until
the heap property is reestablished.
"""
pass
left_child = index * 2 + 1
right_child = index * 2 + 2
store = self.store

if left_child < len(self.store):
if right_child < len(self.store):
if store[left_child].key < store[right_child].key:
smaller = left_child
else:
smaller = right_child
else:
smaller = left_child

if store[index].key > store[smaller].key:
self.swap(index, smaller)
self.heap_down(smaller)
#another recursive check



def swap(self, index_1, index_2):
Expand Down