-
Notifications
You must be signed in to change notification settings - Fork 37
Earth - Jasmine #10
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?
Earth - Jasmine #10
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,59 @@ | ||
|
|
||
|
|
||
| # This method uses a heap to sort an array. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def heap_sort(list) | ||
| raise NotImplementedError, "Method not implemented yet..." | ||
| # Time Complexity: O(n * log(n)) where n is the number of elements. | ||
| # Space Complexity: O(log(n)) worsecase because of recursion inside of heapify? | ||
| # list = [5, 27, 3, 16, -50] | ||
| def heapsort(list) | ||
| return list if list.empty? || list.length == 1 | ||
|
|
||
| # i = the parent of the last element | ||
| i = (list.size - 1) / 2 | ||
| # initial heapify of list | ||
| while i >= 0 | ||
| heapify(list, i, list.length) | ||
| i -= 1 | ||
| end | ||
|
|
||
| # list = [-50, 5, 3, 16, 27 ] | ||
| i = 0 | ||
| j = list.length - 1 | ||
| while j >= 0 | ||
| swap(i, j, list) | ||
| j -= 1 | ||
| heapify(list, 0, j) | ||
| end | ||
|
|
||
| return list | ||
| end | ||
|
|
||
| def swap(index1, index2, list) | ||
| temp = list[index1] | ||
| list[index1] = list[index2] | ||
| list[index2] = temp | ||
| end | ||
|
|
||
| def heapify(list, index, size) | ||
| # index here will be the "root" node | ||
| largest = index | ||
| left = 2 * index + 1 | ||
| right = 2 * index + 2 | ||
|
|
||
| # check if left child is greater than parent | ||
| if left < size && list[left] > list[largest] | ||
| largest = left | ||
| end | ||
|
|
||
| # also check if right child is greater than parent | ||
| if right < size && list[right] > list[largest] | ||
| largest = right | ||
| end | ||
|
|
||
| # if the largest no longer equals it's original value | ||
| if largest != index | ||
| # swap the root and the found largest value | ||
| swap(largest, index, list) | ||
| # make sure the order is still maintained for the children of that new index | ||
| heapify(list, largest, size) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,18 +14,27 @@ def initialize | |
| end | ||
|
|
||
| # This method adds a HeapNode instance to the heap | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(log(n)) | ||
| # Space Complexity: O(log(n) due to the stack call in heap_up, could be O(1) using a loop | ||
| 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..." | ||
| node = HeapNode.new(key, value) | ||
| @store.push(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)) - where n is the number of elements and log(n) reps the levels | ||
| # Space Complexity: O(log(n) due to the stack call in heap_down, could be O(1) using a loop | ||
| 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..." | ||
| if @store.length == 0 | ||
| raise "the heap is empty" | ||
| end | ||
|
|
||
| swap(0, @store.length - 1) | ||
| removed = @store.pop | ||
| heap_down(0) | ||
| return removed.value | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -44,28 +53,48 @@ 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
+56
to
58
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 @store.length == 0 | ||
| 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)) - n for number of elements. log(n) represents the number of levels. | ||
| # Space complexity: O(log(n)) - the stack for the resursive calls could be as tall as the number of levels. | ||
| def heap_up(index) | ||
|
Comment on lines
+67
to
69
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. 👍 |
||
|
|
||
| return if index == 0 | ||
| parent_idx = (index - 1) / 2 | ||
| if @store[parent_idx].key > @store[index].key | ||
| swap(parent_idx, index) | ||
| heap_up(parent_idx) | ||
| 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) | ||
|
Comment on lines
78
to
81
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..." | ||
| left = 2 * index + 1 | ||
| right = 2 * index + 2 | ||
| if @store[left] && @store[right] # if both left and right are not nil | ||
| child_idx = @store[left].key > @store[right].key ? right : left | ||
| elsif @store[left] # there's no case where the left would be null and the right have a value | ||
| child_idx = left | ||
| else # if they're both null stop the function and return. | ||
| return | ||
| end | ||
|
|
||
| if @store[child_idx].key < @store[index].key | ||
| swap(child_idx, index) | ||
| heap_down(child_idx) | ||
| 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.
👍 Really nice. However you're using a lot of one letter variable names.