-
Notifications
You must be signed in to change notification settings - Fork 34
Tatiana - sorry it's so late! #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1f6a05f
4e5fd85
0fa0a33
2c20019
e6ac9e0
faa288c
190e0c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,24 @@ | ||
|
|
||
| require 'pry' | ||
| require_relative 'min_heap' | ||
|
|
||
| # This method uses a heap to sort an array. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) followed by O(log n) beacause going through entire array for first operation | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's actually |
||
| # Space Complexity: I am attempting to do it in O(1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're creating a heap and building it up. This is O(n) space complexity. |
||
| def heap_sort(list) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| if list.nil? || list.empty? || list.length == 1 | ||
| return list | ||
| end | ||
|
|
||
| min_heap = MinHeap.new | ||
|
|
||
| until list.empty? | ||
| min_heap.add(list.pop) | ||
| end | ||
|
|
||
| until min_heap.empty? | ||
| retrieved = min_heap.remove | ||
| list.push(retrieved) | ||
| end | ||
|
|
||
| return list | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| require 'pry' | ||
| class HeapNode | ||
| attr_reader :key, :value | ||
|
|
||
|
|
@@ -14,18 +15,26 @@ def initialize | |
| end | ||
|
|
||
| # This method adds a HeapNode instance to the heap | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(log n) bc it's a tree | ||
| # Space Complexity: O(1) | ||
| def add(key, value = key) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| @store << HeapNode.new(key, value) | ||
| heap_up(@store.length-1) | ||
| end | ||
|
|
||
| # This method removes and returns an element from the heap | ||
| # maintaining the heap structure | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def remove() | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| # Time Complexity: O(log n) bc it's a tree | ||
| # Space Complexity: O(n log n) bc of recursive calls | ||
| def remove | ||
| index = 0 | ||
| return nil if @store.empty? | ||
|
|
||
| swap(0, -1) | ||
| return_value = @store.pop | ||
| heap_down(index) | ||
|
|
||
| return return_value.value | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -44,28 +53,52 @@ def to_s | |
| end | ||
|
|
||
| # This method returns true if the heap is empty | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(1) - Uncertain if Ruby under the hood implements an array in the strictest sense, then by inference it allocates a certain number of spaces to an array and retrieving the length would be constant. Otherwise it is O(n) due to check each element until it becomes nil. | ||
| # Space complexity: O(1) | ||
| def empty? | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return @store.empty? | ||
| 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: ? | ||
| # 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: O(logn) | ||
| # Space complexity: Constant | ||
| def heap_up(index) | ||
|
|
||
| while index > 0 | ||
| parent_index = ((index - 1) / 2) | ||
| if @store[parent_index].key > @store[index].key | ||
| swap(parent_index, index) | ||
| index = parent_index | ||
| else | ||
| return | ||
| end | ||
| 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..." | ||
| child_index_left = ((index * 2) + 1) | ||
| child_index_right = ((index * 2) + 2) | ||
|
|
||
| #if there are no children, return | ||
| if @store[child_index_left].nil? | ||
| return | ||
| elsif @store[child_index_right].nil? | ||
| swap(index, child_index_left) | ||
|
Comment on lines
+91
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should always compare the element at |
||
| else | ||
| if @store[child_index_left].key < @store[child_index_right].key | ||
| swap(index, child_index_left) | ||
| heap_down(child_index_left) | ||
| else | ||
| swap(index, child_index_right) | ||
| heap_down(child_index_right) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # If you want a swap method... you're welcome | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can take out pry when submitting things.