-
Notifications
You must be signed in to change notification settings - Fork 53
Restricted-Arrays #29
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,56 +6,117 @@ | |
|
|
||
| # 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^1), where n is the number of elements in the array. | ||
| # Space complexity: O(1) since the additional storage needed does not change based on input array size | ||
| def length(array) | ||
| raise NotImplementedError | ||
| i = 0 | ||
| length = 0 | ||
| while array[i] != nil | ||
| length += 1 | ||
| i += 1 | ||
| end | ||
| return length | ||
|
|
||
| end | ||
|
|
||
| # Prints each integer values in the array | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^1) , where n is the number of elements in the array. | ||
| # Space complexity: O(1) | ||
| def print_array(array) | ||
|
Comment on lines
+23
to
25
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 | ||
| j = 0 | ||
| while array[j] != nil | ||
| print array[j] | ||
| j += 1 | ||
| end | ||
| end | ||
|
|
||
| # For an unsorted array, searches for 'value_to_find'. | ||
| # Returns true if found, false otherwise. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2), where n is the number of elements in the array. | ||
| # Space complexity: O(1) since the additional storage needed does not depend on input array size | ||
| def search(array, length, value_to_find) | ||
|
Comment on lines
+35
to
37
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. 👍 , why do you think the time complexity is O(n^2)? |
||
| raise NotImplementedError | ||
| k = 0 | ||
| while k <= length | ||
| if array[k] == value_to_find | ||
| return true | ||
| else | ||
| k += 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: O(n^2), where n is the number of elements in the array. | ||
| # Space complexity: O(1) since the additional storage needed does not depend on input array size | ||
| def find_largest(array, length) | ||
|
Comment on lines
+51
to
53
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. 👍 , why do you think the time complexity is O(n^2)? |
||
| raise NotImplementedError | ||
| max_value = array[0] | ||
| m = 0 | ||
| while m <= length - 1 | ||
| if array[m] > max_value | ||
| max_value = array[m] | ||
| end | ||
| m += 1 | ||
| end | ||
| return max_value | ||
| end | ||
|
|
||
|
|
||
| # Finds and returns the smallest integer value in the array | ||
| # Assumes that the array is not sorted. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2), where n is the number of elements in the array. | ||
| # Space complexity: O(1) since the additional storage needed does not depend on input array size | ||
| def find_smallest(array, length) | ||
|
Comment on lines
+68
to
70
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. 👍 , why do you think the time complexity is O(n^2)? |
||
| raise NotImplementedError | ||
| min_value = array[0] | ||
| n = 0 | ||
| while n <= length - 1 | ||
| if array[n] < min_value | ||
| min_value = array[n] | ||
| end | ||
| n += 1 | ||
| end | ||
| return min_value | ||
| end | ||
|
|
||
| # Reverses the values in the integer array in place | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) where n is the number of elements in the array. | ||
| # Space complexity: O(n^1) | ||
| def reverse(array, length) | ||
|
Comment on lines
+83
to
85
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. 👍 , why do you think the time complexity is O(n^2)? |
||
| raise NotImplementedError | ||
| if length <= 1 | ||
| array = array | ||
| else | ||
| i = 0 | ||
| j = length - 1 | ||
| while i < j | ||
| temp = array[i] | ||
| array[i] = array[j] | ||
| array[j] = temp | ||
| i += 1 | ||
| j -= 1 | ||
| end | ||
| end | ||
| return array | ||
| end | ||
|
|
||
| # For an array sorted in ascending order, searches for 'value_to_find'. | ||
| # Returns true if found, false otherwise. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: log(n) where n is the number of elements in the array. | ||
| # Space complexity: O(1) since the additional storage needed does not depend on input array size | ||
| def binary_search(array, length, value_to_find) | ||
| raise NotImplementedError | ||
| lower_bound = 0 | ||
| upper_bound = length - 1 | ||
| while lower_bound <= upper_bound | ||
| i = (lower_bound + upper_bound)/2 | ||
| if array[i] == value_to_find | ||
| return true | ||
| elsif array[i] < value_to_find | ||
| lower_bound = i + 1 | ||
| elsif array[i] > value_to_find | ||
| upper_bound = i - 1 | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| # Helper method provided to sort the array in ascending order | ||
|
|
||
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 could just say O(n) instead of O(n^1)