diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..e64e08c 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -1,8 +1,13 @@ - +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) where n is the number of items in the list + Space Complexity: O(n) where n is the number of items in the list """ - pass \ No newline at end of file + heap = MinHeap() + for item in list: + heap.add(item) + for i in range(len(list)): + list[i] = heap.remove() + return list diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..1a351a1 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -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(log n) due to recursive stack calls """ - pass + if value is None: + value = key + self.store.append(HeapNode(key, value)) + 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(log n) due to recursive stack calls """ - pass + if self.empty(): + return None + last_index = len(self.store) - 1 + root_index = 0 + self.swap(last_index, root_index) + removed = self.store.pop() + self.heap_down(root_index) + return removed.value + @@ -44,10 +55,10 @@ 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) """ - pass + return len(self.store) == 0 def heap_up(self, index): @@ -57,10 +68,13 @@ def heap_up(self, index): property is reestablished. This could be **very** helpful for the add method. - Time complexity: ? - Space complexity: ? + Time complexity: O(1) + Space complexity: O(1) """ - pass + parent_index = (index - 1) // 2 + if parent_index >= 0 and self.store[parent_index].key > self.store[index].key: + self.swap(parent_index, index) + self.heap_up(parent_index) def heap_down(self, index): """ This helper method takes an index and @@ -68,7 +82,18 @@ def heap_down(self, index): larger than either of its children and continues until the heap property is reestablished. """ - pass + left_index = (index * 2) + 1 + right_index = (index * 2) + 2 + if left_index < len(self.store) and right_index < len(self.store): + smallest = left_index + if self.store[left_index].key > self.store[right_index].key: + smallest = right_index + if self.store[smallest].key < self.store[index].key: + self.swap(smallest, index) + self.heap_down(smallest) + elif left_index < len(self.store) and self.store[left_index].key < self.store[index].key: + self.swap(left_index, index) + self.heap_down(left_index) def swap(self, index_1, index_2):