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
43 changes: 38 additions & 5 deletions heaps/heap_sort.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
from heapq import heappush, heappop
from heaps.min_heap import MinHeap


def heap_sort(list):
def heap_sort(list): # O(n)
""" This method uses a heap to sort an array.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n log n)
Space Complexity: O(n)
Comment on lines +6 to +7

Choose a reason for hiding this comment

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

✨ Great. Since sorting using a heap reduces down to building up a heap of n items one-by-one (each taking O(log n)), then pulling them back out again (again taking O(log n) for each of n items), we end up with a time complexity of O(2n log n) → O(n log n). While for the space, we do need to worry about the O(log n) space consume during each add and remove, but they aren't cumulative (each is consumed only during the call to add or remove). However, the internal store for the MinHeap does grow with the size of the input list. So the maximum space would be O(n + log n) → O(n), since n is a larger term than log n.

Note that a fully in-place solution (O(1) space complexity) would require both avoiding the recursive calls, as well as working directly with the originally provided list (no internal store).

"""
pass
heap = MinHeap()
for num in list:
# has heap_up inside and sorts it in heap everytime new node is added
heap.add(num)

# while the heap is not empty, it removes the most minimal value which is always the root
index = 0
while not heap.empty():
# puts sorted nums back in list using index
# remove is O(log n)
list[index] = heap.remove()
index += 1

return list
Comment on lines +15 to +22
Copy link

@anselrognlie anselrognlie Jul 21, 2022

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


# from class
# Time: n log n
# space: n
def heapsort(unsorted):
heap = []
for item in unsorted:
heappush(heap, item)
# heapq is putting all of this together. append will treat it like any other list and does order it

ordered = []
while len(heap) > 0:
value = heappop(heap)
ordered.append(value)

# returns ordered elements
return ordered

print(heapsort([4,7,1,33,0,-8]))
64 changes: 51 additions & 13 deletions heaps/min_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@ 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: ?
Space Complexity: ?
Time Complexity: O(log n)
Space Complexity: O(1)
Comment on lines +22 to +23

Choose a reason for hiding this comment

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

👀 In the worst case, the new value we're inserting is the new root of the heap, meaning it would need to move up the full height of the heap (which is log n levels deep) leading to O(log n) time complexity. Your implementation of the heap_up helper is recursive, meaning that for each recursive call (up to log n of them) there is stack space being consumed. So the space complexity would also be O(log n). If heap_up were implemented iteratively, this could be reduced to O(1) 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)
self.store.append(node)
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: ?
Space Complexity: ?
Time Complexity: O(log n)
Space Complexity: O(1)
Comment on lines +35 to +36

Choose a reason for hiding this comment

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

👀 In the worst case, the value that got swapped to the top will need to move all the way back down to a leaf, meaning it would need to move down the full height of the heap (which is log n levels deep) leading to O(log n) time complexity. Your implementation of the heap_down helper is recursive, meaning that for each recursive call (up to log n of them) there is stack space being consumed. So the space complexity would also be O(log n). If heap_down were implemented iteratively, this could be reduced to O(1) space complexity.

"""
pass
if self.empty():

Choose a reason for hiding this comment

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

✨ Nice use of your own helper method!

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

return min.value



Expand All @@ -44,10 +55,11 @@ def __str__(self):

def empty(self):
""" This method returns true if the heap is empty
Time complexity: ?
Space complexity: ?
Time complexity: O(n)
Space complexity: O(1)
Comment on lines +58 to +59

Choose a reason for hiding this comment

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

👀 Getting the length of a list is a constant time operation, so the overall time complexity will be constant time as well.

"""
pass
if len(self.store) == 0:
return True
Comment on lines +61 to +62

Choose a reason for hiding this comment

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

👀 This will return True when the list is empty, 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 len(self.store) == 0:
            return True
        else:
            return False

which simplifies to

        return len(self.store) == 0

We could also remember that empty lists are falsy, and write this as

        return not self.store



def heap_up(self, index):
Expand All @@ -57,18 +69,44 @@ def heap_up(self, index):
property is reestablished.

This could be **very** helpful for the add method.
Time complexity: ?
Space complexity: ?
Time complexity: O(log n)
Space complexity: O(1)
Comment on lines +72 to +73

Choose a reason for hiding this comment

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

👀 This function is the main source of time and space complexity for add, so refer back to that note.


check my value vs parent's value
if parent is larger
swap with parent
recursively call heap_up from new location
Comment on lines +75 to +78

Choose a reason for hiding this comment

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

✨ Nice explanation of the approach

"""
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)

def heap_down(self, index):
""" 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

Choose a reason for hiding this comment

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

👀 This is identical to the instructor solution and has no additions that would indicate to me that time was spent time understanding this approach.

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)


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