diff --git a/.gitignore b/.gitignore index e43b0f9..4d0e806 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +.vscode/settings.json diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..af20725 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,16 @@ +# Time Complexity: O(nlog(n)) +# Space Complexity: Constant. +def heapsort(array) + heap = MinHeap.new + array.each do |item| + heap.add(item, item) + end -# This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." + result = [] + until heap.empty? + result << item.remove + end + + return result end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..df1d1a4 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -13,19 +13,24 @@ def initialize @store = [] end - # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: ? O(log n) + # Space Complexity: ? O(log n)) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + node = HeapNode.new(key, value) + @store << node + heap_up(@store.length - 1) end - # 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) def remove() - raise NotImplementedError, "Method not implemented yet..." + return nil if !@store + + swap(0, @store.length - 1) + nope = @store.pop + + heap_down(0) + return nope.value end @@ -39,39 +44,52 @@ def to_s end output += @store.last.value + "]" - + return output end - # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(1) + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + !@store end private - # This helper method takes an index and - # moves it up the heap, if it is less than it's parent node. - # It could be **very** helpful for the add method. - # Time complexity: ? - # Space complexity: ? - def heap_up(index) - + # Time complexity: O(log n) + # Space complexity: Log n + def heap_up(i) + return if i == 0 + + pnode = (i-1) / 2 + if @store[i].key < @store[pnode].key + swap(i, pnode) + heap_up(pnode) + end end - # This helper method takes an index and - # moves it up the heap if it's smaller - # than it's parent node. - def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + def heap_down(i) + li = i*2+1 + ri = i*2+2 + + if ri < @store.length + + min = @store[li].key < @store[ri].key ? li : ri + + if @store[i].key > @store[min].key + swap(i, min) + heap_down(min) + end + elsif li < @store.length + if @store[i].key > @store[li].key + swap(i, li) + end + end end - # If you want a swap method... you're welcome def swap(index_1, index_2) temp = @store[index_1] @store[index_1] = @store[index_2] @store[index_2] = temp end -end \ No newline at end of file +end