-
Notifications
You must be signed in to change notification settings - Fork 53
Restricted Arrays #33
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,56 +6,152 @@ | |
|
|
||
| # Calculates the length of the restricted array. All values are integers. | ||
| # The restricted_array is terminated by 'nil' i.e. array[length] = nil | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def length(array) | ||
| raise NotImplementedError | ||
| end | ||
| i = 0 | ||
|
|
||
| until array[i] == nil | ||
| i += 1 | ||
| end | ||
|
|
||
| return i | ||
| end | ||
|
|
||
| # Prints each integer values in the array | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def print_array(array) | ||
|
Comment on lines
+22
to
24
Collaborator
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 | ||
| i = 0 | ||
|
|
||
| until array[i] == nil | ||
| puts i | ||
| i += 1 | ||
| end | ||
| end | ||
|
|
||
| # For an unsorted array, searches for 'value_to_find'. | ||
| # Returns true if found, false otherwise. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: | ||
| # sorted: O(n) | ||
| # unsorted: O(n) | ||
| # Space complexity: O(1) | ||
| def search(array, length, value_to_find) | ||
|
Comment on lines
+35
to
39
Collaborator
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 | ||
| i = 0 | ||
|
|
||
| while i < length | ||
| if array[i] == value_to_find | ||
| return true | ||
| else | ||
| i += 1 | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
|
|
||
| # Finds and returns the largest integer value the array | ||
| # Assumes that the array is not sorted. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: | ||
| # sorted: O(1) | ||
| # unsorted: O(n) | ||
| # Space complexity: O(1) | ||
| def find_largest(array, length) | ||
|
Comment on lines
+55
to
59
Collaborator
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. 👍 Actually this algorithm is always O(n) in time complexity |
||
| raise NotImplementedError | ||
| end | ||
| i = 0 | ||
|
|
||
| if length == 0 | ||
| return nil | ||
| else | ||
| max = array[0] | ||
|
|
||
| while i < length | ||
| if array[i] > max | ||
| max = array[i] | ||
| end | ||
| i += 1 | ||
| end | ||
|
|
||
| return max | ||
| end | ||
| end | ||
|
|
||
| # Finds and returns the smallest integer value in the array | ||
| # Assumes that the array is not sorted. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: | ||
| # sorted: O(1) | ||
| # unsorted: O(n) | ||
| # Space complexity: O(1) | ||
| def find_smallest(array, length) | ||
|
Comment on lines
+80
to
84
Collaborator
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 | ||
| i = 0 | ||
|
|
||
| if length == 0 | ||
| return nil | ||
| else | ||
| min = array[0] | ||
| while i < length | ||
| if array[i] < min | ||
| min = array[i] | ||
| end | ||
| i += 1 | ||
| end | ||
|
|
||
| return min | ||
| end | ||
| end | ||
|
|
||
| # Reverses the values in the integer array in place | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def reverse(array, length) | ||
|
Comment on lines
+103
to
105
Collaborator
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. 👍 , this works but since you're making an auxiliary array it has O(n) space complexity. Can you think of a way to do this without the other array? |
||
| raise NotImplementedError | ||
| i = 0 | ||
| j = length - 1 | ||
| temp_array = Array.new(length) | ||
|
|
||
| while i < length | ||
| temp_array[i] = array[j] | ||
| i += 1 | ||
| j -= 1 | ||
| end | ||
|
|
||
| i = 0 | ||
| while i < length | ||
| array[i] = temp_array[i] | ||
| i += 1 | ||
| end | ||
| end | ||
|
|
||
| # For an array sorted in ascending order, searches for 'value_to_find'. | ||
| # Returns true if found, false otherwise. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def binary_search(array, length, value_to_find) | ||
|
Comment on lines
125
to
127
Collaborator
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 | ||
| i = 0 | ||
|
|
||
| if length == 0 | ||
| return false | ||
| end | ||
|
|
||
| low = 0 | ||
| high = length - 1 | ||
|
|
||
| while low <= high | ||
| mid = (low + high) / 2 | ||
| # calculate the index value between low and high | ||
| if array[mid] == value_to_find | ||
| # compare the element at index mid with the value to find | ||
| return true | ||
| elsif array[mid] > value_to_find | ||
| # value to find is less than the value at mid index | ||
| # eliminate the second half | ||
| high = mid - 1 | ||
| elsif array[mid] < value_to_find | ||
| # value to find is greater than the value at mid index | ||
| # eliminate the first half | ||
| low = mid + 1 | ||
| end | ||
| end | ||
| # value not found in the array | ||
| return false | ||
| end | ||
|
|
||
| # Helper method provided to sort the array in ascending order | ||
|
|
@@ -89,4 +185,4 @@ def sort(array, length) | |
| end | ||
| end | ||
| end | ||
| ## --- END OF METHODS --- | ||
| # --- END OF METHODS --- | ||
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.
👍