diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..340e386 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: ? """ - pass \ No newline at end of file + if len(list) <= 1: # returns list of empty or only has one node + return list + + heap = MinHeap() + + for value in list: + heap.add(value) + + result = [] # unpacking the heap into a new result list to avoid side effects + while not heap.empty(): + result.append(heap.remove()) + + return result diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..b26d80d 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -1,5 +1,4 @@ class HeapNode: - def __init__(self, key, value): self.key = key self.value = value @@ -10,71 +9,95 @@ def __str__(self): def __repr__(self): return str(self.value) -class MinHeap: +class MinHeap: def __init__(self): self.store = [] - - 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: ? + 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: O(logn) + Space Complexity: O(logn) - heap_up is recursive, stack soace being consumed """ - 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: ? + """This method removes and returns an element from the heap + maintaining the heap structure + Time Complexity: O(logn) + Space Complexity: O(logn) - heap_down is recursive, stack soace being consumed """ - pass + if self.empty(): + return None + + self.swap(0, len(self.store) - 1) # swap first w/ last + removed = self.store.pop() # remove the last node + self.heap_down(0) # restructure the heap + return removed.value - def __str__(self): - """ This method lets you print the heap, when you're testing your app. - """ + """This method lets you print the heap, when you're testing your app.""" if len(self.store) == 0: return "[]" return f"[{', '.join([str(element) for element in self.store])}]" - def empty(self): - """ This method returns true if the heap is empty - Time complexity: ? - Space complexity: ? + """This method returns true if the heap is empty + Time complexity: O(1) + Space complexity: O(1) """ - pass - + return not self.store def heap_up(self, index): - """ This helper method takes an index and - moves the corresponding element up the heap, if - it is less than it's parent node until the Heap - property is reestablished. - - This could be **very** helpful for the add method. - Time complexity: ? - Space complexity: ? + """This helper method takes an index and + moves the corresponding element up the heap, if + it is less than it's parent node until the Heap + property is reestablished. + + This could be **very** helpful for the add method. + Time complexity: O(logn) + Space complexity: O(logn) """ - pass + if index == 0: + return + + check = (index - 1) // 2 + if self.store[index].key < self.store[check].key: + self.swap(index, check) + self.heap_up(check) 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. + """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 + left = index * 2 + 1 + right = index * 2 + 2 + + if left < len(self.store): + if right < len(self.store): + if self.store[left].key < self.store[right].key: + less = left + else: + less = right + else: + less = left + + if self.store[index].key > self.store[less].key: + self.swap(index, less) + self.heap_down(less) - def swap(self, index_1, index_2): - """ Swaps two elements in self.store - at index_1 and index_2 - used for heap_up & heap_down + """Swaps two elements in self.store + at index_1 and index_2 + used for heap_up & heap_down """ temp = self.store[index_1] self.store[index_1] = self.store[index_2]