-
Notifications
You must be signed in to change notification settings - Fork 56
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
Abigail C. - Rock #36
base: master
Are you sure you want to change the base?
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,19 @@ | ||
|
||
|
||
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: O(n log n) | ||
Space Complexity: O(1) | ||
""" | ||
pass | ||
heap = MinHeap() | ||
if list == []: | ||
return list | ||
|
||
for num in list: | ||
heap.add(num) | ||
|
||
index = 0 | ||
while not heap.empty(): | ||
list[index] = heap.remove() | ||
index += 1 | ||
return list |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import math | ||
class HeapNode: | ||
|
||
def __init__(self, key, value): | ||
|
@@ -24,16 +25,27 @@ def add(self, key, value = None): | |
Time Complexity: ? | ||
Space Complexity: ? | ||
""" | ||
pass | ||
if value == None: | ||
value = key | ||
|
||
node = HeapNode(key,value) | ||
self.store.append(node) | ||
self.heap_up(len(self.store) -1) | ||
Comment on lines
+28
to
+33
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. 👍 Space/time complexity? |
||
|
||
def remove(self): | ||
""" This method removes and returns an element from the heap | ||
maintaining the heap structure | ||
Time Complexity: ? | ||
Time Complexity: O(log n) | ||
Space Complexity: ? | ||
""" | ||
pass | ||
if len(self.store) == 0: | ||
return None | ||
|
||
self.swap(0, len(self.store) -1) | ||
min = self.store.pop() | ||
self.heap_down(0) | ||
Comment on lines
35
to
+46
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. 👍 Space complexity? |
||
|
||
return min.value | ||
|
||
|
||
def __str__(self): | ||
|
@@ -49,7 +61,7 @@ def empty(self): | |
Time complexity: ? | ||
Space complexity: ? | ||
""" | ||
pass | ||
return 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. 👍 |
||
|
||
|
||
def heap_up(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. 👍 Space/time complexity? |
||
|
@@ -62,16 +74,36 @@ def heap_up(self, index): | |
Time complexity: ? | ||
Space complexity: ? | ||
""" | ||
pass | ||
|
||
current= self.store[index].key | ||
index_parent = math.floor((index-1)/2) | ||
current_parent= self.store[index_parent].key | ||
|
||
while current < current_parent: | ||
if index_parent < 0: | ||
break | ||
self.swap(index, index_parent) | ||
index = index_parent | ||
index_parent = math.floor(((index_parent-1)/2)) | ||
current_parent = self.store[index_parent].key | ||
return self.store | ||
|
||
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. | ||
""" | ||
Comment on lines
90
to
95
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 work, very compact |
||
pass | ||
|
||
left_child = index * 2 + 1 | ||
right_child = index * 2 + 2 | ||
|
||
if len(self.store) > (left_child) and self.store[index].key > self.store[left_child].key: | ||
self.swap(index, (left_child)) | ||
self.heap_down((left_child)) | ||
self.heap_down(index) | ||
elif len(self.store) > (right_child) and self.store[index].key > self.store[right_child].key: | ||
self.swap(index, (right_child)) | ||
self.heap_down((right_child)) | ||
self.heap_down(index) | ||
|
||
def swap(self, index_1, index_2): | ||
""" Swaps two elements in self.store | ||
|
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.
👍 However the space complexity is O(n) because you are building up a heap of
n
nodes as the function runs.