-
Notifications
You must be signed in to change notification settings - Fork 51
Test passing #39
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?
Test passing #39
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,20 @@ | ||
|
||
from heaps.min_heap import MinHeap | ||
|
||
def heap_sort(list): | ||
""" This method uses a heap to sort an array. | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: (log n) | ||
Space Complexity: (O n) | ||
""" | ||
pass | ||
|
||
heap = MinHeap() | ||
|
||
for i in list: | ||
heap.add(i) | ||
|
||
i = 0 | ||
|
||
while not heap.empty(): | ||
list[i] = heap.remove() | ||
i += 1 | ||
Comment on lines
+14
to
+18
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 list |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,18 +19,28 @@ 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: ? (log n) | ||
Space Complexity: (O 1) | ||
Comment on lines
+22
to
+23
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. 👀 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 |
||
""" | ||
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) | ||
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 n) | ||
Space Complexity: (O N) | ||
Comment on lines
+34
to
+35
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. 👀 Similar to |
||
""" | ||
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 a helper method that can check for the heap being empty. |
||
return None | ||
self.swap(0, len(self.store) - 1) | ||
min = self.store.pop() | ||
self.heap_down(0) | ||
|
||
return min.value | ||
|
||
|
||
|
||
|
@@ -44,10 +54,11 @@ def __str__(self): | |
|
||
def empty(self): | ||
""" This method returns true if the heap is empty | ||
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: (O 1) | ||
Space complexity: (O 1) | ||
Comment on lines
+57
to
+58
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. ✨ |
||
""" | ||
pass | ||
if len(self.store) == 0: | ||
return True | ||
Comment on lines
+60
to
+61
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. 👀 This will return Python would fall off the end of the function, with the default 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): | ||
|
@@ -57,18 +68,40 @@ def heap_up(self, index): | |
property is reestablished. | ||
|
||
This could be **very** helpful for the add method. | ||
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: (log n) | ||
Space complexity: (O 1) | ||
Comment on lines
+71
to
+72
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. 👀 This function is the main source of time and space complexity for |
||
""" | ||
pass | ||
if index == 0: | ||
return index | ||
Comment on lines
+74
to
+75
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. 👀 When we reach the top, we do want to stop, but we don't need to return a particular value, so if index == 0:
return alone would be sufficient. |
||
|
||
parent_node = (index - 1) // 2 | ||
current_size = self.store | ||
if current_size[parent_node].key > current_size[index].key: | ||
self.swap(parent_node, index) | ||
self.heap_up(parent_node) | ||
|
||
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 set of conditionals to narrow in on where to swap, and then only perform the swap and continue the heapification if the key of the candidate requires it. Though not prompted, like |
||
""" 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 | ||
current_size = self.store | ||
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. 👀 To me store = self.store |
||
left_c = index * 2 + 1 | ||
right_c= index * 2 + 2 | ||
|
||
if left_c < len(self.store): | ||
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. 👀 You made your alias to if left_c < len(current_size): |
||
if right_c < len(current_size): | ||
if current_size[left_c].key < current_size[right_c].key: | ||
less = left_c | ||
else: | ||
less = right_c | ||
else: | ||
less = left_c | ||
|
||
if current_size[index].key > current_size[less].key: | ||
self.swap(index, less) | ||
self.heap_down(less) | ||
|
||
|
||
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.
👀 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 should be aware of the O(log n) space consume during each add and remove, but they aren't cumulative (each is consumed only during the individual 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).