-
Notifications
You must be signed in to change notification settings - Fork 41
Time - Olga #12
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?
Time - Olga #12
Changes from all commits
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,50 @@ | ||
|
|
||
|
|
||
| # This method uses a heap to sort an array. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n log n) | ||
| # Space Complexity: O(1) | ||
| def heap_sort(list) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| n = list.size | ||
| return list if n < 2 | ||
|
|
||
| # heap construction - reorganize original array into a maximum heap | ||
| # proceed from right to left, starting halfway back through the array | ||
| # O(n) | ||
| i = (n - 1) / 2 | ||
| while i >= 0 | ||
| heap_down(list, i, n) | ||
| i -= 1 | ||
| end | ||
|
|
||
| # sort | ||
| # remove the largest remaining item from the heap and | ||
| # put it into the array position vacated as the heap shrinks | ||
| # O(n log n) | ||
| while n > 0 | ||
| list[0], list[n - 1] = list[n - 1], list[0] | ||
|
|
||
| n -= 1 | ||
| heap_down(list, 0, n) | ||
|
|
||
| end | ||
| list | ||
| end | ||
|
|
||
|
|
||
| # max heap heap_down method | ||
| def heap_down(list, index, n) | ||
| while index * 2 + 1 < n | ||
| j = index * 2 + 1 # first child | ||
|
|
||
| if j < n - 1 && list[j] < list[j + 1] # greater of two children | ||
| j += 1 | ||
| end | ||
|
|
||
| if list[index] > list[j] # stop if move will violate heap order | ||
| break | ||
| end | ||
|
|
||
| list[index], list[j] = list[j], list[index] | ||
| index = j | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,18 +14,28 @@ def initialize | |
| end | ||
|
|
||
| # This method adds a HeapNode instance to the heap | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(log n) - worst case, when need heap-up, O(1) - best case | ||
| # Space Complexity: O(log n) - worst case, when need heap-up, O(1) - best case | ||
| def add(key, value = key) | ||
|
Comment on lines
+17
to
19
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. 👍 |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| new_node = HeapNode.new(key, value) | ||
| @store << new_node | ||
| heap_up(@store.size-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() | ||
|
Comment on lines
+27
to
29
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. 👍 |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| return if @store.empty? | ||
| if @store.size == 1 | ||
| return @store.pop | ||
| end | ||
| # swap the first and the last element in the underlying array | ||
| @store[-1], @store[0] = @store[0], @store[-1] | ||
| removed = @store.pop | ||
| heap_down(0) | ||
| return removed.value | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -44,28 +54,45 @@ def to_s | |
| end | ||
|
|
||
| # This method returns true if the heap is empty | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(1) | ||
| # Space complexity: O(1) | ||
| def empty? | ||
|
Comment on lines
+57
to
59
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. 👍 |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| @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: ? | ||
| # Time complexity: O(log n) | ||
| # Space complexity: O(log n) | ||
| def heap_up(index) | ||
|
Comment on lines
+68
to
70
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. 👍 |
||
|
|
||
| # base case | ||
| parent_index = (index - 1) / 2 | ||
| return if parent_index < 0 || @store[index].key >= @store[parent_index].key | ||
|
|
||
| # swap | ||
| @store[index], @store[parent_index] = @store[parent_index], @store[index] | ||
| # recursive case | ||
| heap_up(parent_index) | ||
| 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) | ||
|
Comment on lines
81
to
84
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. 👍 |
||
| raise NotImplementedError, "Method not implemented yet..." | ||
| n = @store.size | ||
| while index * 2 + 1 < n | ||
| j = index * 2 + 1 | ||
|
|
||
| j += 1 if j < n - 1 && @store[j + 1].key < @store[j].key | ||
|
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. Nice work handling the left and right children here. |
||
|
|
||
| break if @store[index].key <= @store[j].key | ||
|
|
||
| @store[index], @store[j] = @store[j], @store[index] | ||
| index = j | ||
| 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.
👍 , Nice work doing this in-place with heapsort. Using the given array as the heap. Well done.