From d611170bdf70e663c29cae074262105ffd1b7b0e Mon Sep 17 00:00:00 2001 From: Veronica Osei Date: Mon, 25 Jul 2022 22:00:58 -0500 Subject: [PATCH] Test passing --- heaps/heap_sort.py | 20 ++++++++++++---- heaps/min_heap.py | 59 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..594f1b3 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -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 \ No newline at end of file + + heap = MinHeap() + + for i in list: + heap.add(i) + + i = 0 + + while not heap.empty(): + list[i] = heap.remove() + i += 1 + + return list \ No newline at end of file diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..39717bf 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -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) """ - pass + if value == 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 n) + Space Complexity: (O N) """ - pass + if len(self.store) == 0: + 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) """ - pass + if len(self.store) == 0: + return True def heap_up(self, index): @@ -57,10 +68,17 @@ 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) """ - pass + if index == 0: + return index + + 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): """ This helper method takes an index and @@ -68,7 +86,22 @@ def heap_down(self, index): larger than either of its children and continues until the heap property is reestablished. """ - pass + current_size = self.store + left_c = index * 2 + 1 + right_c= index * 2 + 2 + + if left_c < len(self.store): + 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):