-
Notifications
You must be signed in to change notification settings - Fork 53
Emily's Completed Methods for Arrays #34
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,114 @@ | |
|
|
||
| # 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) because I'm performing a linear search until the loop reaches nil in the array. | ||
| # Space complexity: O(1), because I'm only looking for 1 value, it doesn't matter what size the array is. | ||
| def length(array) | ||
| raise NotImplementedError | ||
| 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(1) because retrieving an element is constant, regardless of the size. | ||
| # Space complexity: O(1) because size of the array doesn't matter? Still retrieving one element at a time. | ||
| def print_array(array) | ||
|
Comment on lines
+21
to
23
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. 👍 , however the time complexity is O(n) because you print out |
||
| raise NotImplementedError | ||
| i = 0 | ||
| until array[i] == nil | ||
| i += 1 | ||
| puts i | ||
| 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) for an unsorted array, because of its linear search to find that value. | ||
| # Space complexity: O(1), because I'm only looking for 1 value. | ||
| def search(array, length, value_to_find) | ||
|
Comment on lines
+33
to
35
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 value_to_find == array[i] | ||
| return true | ||
| end | ||
| i += 1 | ||
| 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) because it involves a linear search for an unsorted array. O(1), if it's sorted because the search will end if it's the first element (largest_value = array[0]). | ||
| # Space complexity: O(1), because I'm only looking for 1 value. | ||
| def find_largest(array, length) | ||
|
Comment on lines
+48
to
50
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 = 1 | ||
| largest_value = array[0] | ||
|
|
||
| while i < length | ||
| if array[i] > largest_value | ||
| largest_value = array[i] | ||
| end | ||
| i += 1 | ||
| end | ||
| return largest_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) because it involves a linear search for an unsorted array. O(1), if it's sorted because the search will end if it's the first element (smallest_val = array[0]). | ||
| # Space complexity: O(1), because I'm only looking for 1 value. | ||
| def find_smallest(array, length) | ||
|
Comment on lines
+65
to
67
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 = 1 | ||
| smallest_value = array[0] | ||
| while i < length | ||
| if array[i] < smallest_value | ||
| smallest_value = array[i] | ||
| end | ||
| i += 1 | ||
| end | ||
| return smallest_value | ||
| end | ||
|
|
||
| # Reverses the values in the integer array in place | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(1), because I'm retrieving one element at a time? | ||
| # Space complexity: O(1) | ||
| def reverse(array, length) | ||
|
Comment on lines
+80
to
82
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. 👍 , however the time complexity is O(n) because you loop n/2 times. |
||
| raise NotImplementedError | ||
| i = 0 | ||
| last_val = length - 1 | ||
|
|
||
| while i < last_val | ||
| temp = array[i] | ||
| array[i] = array[last_val] | ||
| array[last_val] = temp | ||
| i += 1 | ||
| last_val -= 1 | ||
| 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: O(log n), to narrow down my search by n/2 after an iteration, and then n/4 if there's another iteration that's needed to find the value. | ||
| # Space complexity: O(1), because I'm only looking for 1 value. | ||
| def binary_search(array, length, value_to_find) | ||
|
Comment on lines
+99
to
101
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 | ||
| low_val = 0 | ||
| high_val = length - 1 | ||
|
|
||
| while low_val <= high_val | ||
| mid = (low_val + high_val) / 2 | ||
|
|
||
| if array[mid] == value_to_find | ||
| return true | ||
| elsif array[mid] > value_to_find | ||
| high_val = mid - 1 | ||
| elsif array[mid] < value_to_find | ||
| low_val = mid + 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.
👍