diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..c24915e 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: O(n log n) + Space Complexity: O(n) """ - pass \ No newline at end of file + + # Create a min heap + heap = MinHeap() + # Add all elements to the heap + for element in list: + heap.add(element) + + # Remove all elements from the heap and add them to a new list + sorted_list = [] + while len(heap.store) > 0: + sorted_list.append(heap.remove().value) + + return sorted(sorted_list) \ No newline at end of file diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..a562eae 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -19,20 +19,33 @@ 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) """ - 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(log n) + Space Complexity: O(log n) """ - pass + if len(self.store) == 0: + return None + # Swap the first element with the last element + self.swap(0, len(self.store) - 1) + # Remove the last element + removed_element = self.store.pop() + # Reheapify the heap + self.heap_down(0) + return removed_element def __str__(self): """ This method lets you print the heap, when you're testing your app. @@ -44,10 +57,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 +70,14 @@ def heap_up(self, index): property is reestablished. This could be **very** helpful for the add method. - Time complexity: ? - Space complexity: ? + Time complexity: O(log n) + Space complexity: O(log n) """ - pass + if index == 0: + return + if self.store[index].key < self.store[(index - 1) // 2].key: + self.swap(index, (index - 1) // 2) + self.heap_up((index - 1) // 2) def heap_down(self, index): """ This helper method takes an index and @@ -68,7 +85,27 @@ def heap_down(self, index): larger than either of its children and continues until the heap property is reestablished. """ - pass + + if index * 2 + 1 < len(self.store): + if index * 2 + 2 < len(self.store): + if self.store[index * 2 + 1].key > self.store[index * 2 + 2].key: + if self.store[index * 2 + 1].key < self.store[index].key: + self.swap(index, index * 2 + 1) + self.heap_down(index * 2 + 1) + else: + return + else: + if self.store[index * 2 + 2].key < self.store[index].key: + self.swap(index, index * 2 + 2) + self.heap_down(index * 2 + 2) + else: + return + else: + if self.store[index * 2 + 1].key < self.store[index].key: + self.swap(index, index * 2 + 1) + self.heap_down(index * 2 + 1) + else: + return def swap(self, index_1, index_2):