-
Notifications
You must be signed in to change notification settings - Fork 51
Reid C16 Spruce #28
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
base: master
Are you sure you want to change the base?
Reid C16 Spruce #28
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: ? | ||
""" | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import re | ||
|
||
|
||
class HeapNode: | ||
|
||
def __init__(self, key, value): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 Prefer using |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 We have an |
||
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. | ||
""" | ||
|
@@ -47,8 +75,8 @@ def empty(self): | |
Time complexity: ? | ||
Space complexity: ? | ||
Comment on lines
75
to
76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 What are the time and space complexities for |
||
""" | ||
pass | ||
|
||
if not self.store: | ||
return True | ||
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 | ||
|
@@ -60,15 +88,43 @@ def heap_up(self, index): | |
Time complexity: ? | ||
Space complexity: ? | ||
Comment on lines
88
to
89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 What are the time and space complexities for |
||
""" | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
""" 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): | ||
|
There was a problem hiding this comment.
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?