-
Notifications
You must be signed in to change notification settings - Fork 53
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
K Johnson Restricted Arrays Assignment #39
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,58 +6,113 @@ | |
|
||
# 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: linear O(n). n represents the size/length of the array. | ||
# Space complexity: O(1) -- constant | ||
def length(array) | ||
raise NotImplementedError | ||
length = 0 | ||
while array[length] != nil | ||
length += 1 | ||
end | ||
return length | ||
end | ||
|
||
# Prints each integer values in the array | ||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: linear O(n). n is the size/length of array | ||
# Space complexity: O(1) -- constant | ||
def print_array(array) | ||
Comment on lines
+20
to
22
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 | ||
counter = 0 | ||
while array[counter] != nil | ||
print "#{array[counter]} " | ||
counter += 1 | ||
end | ||
# raise NotImplementedError | ||
end | ||
|
||
# For an unsorted array, searches for 'value_to_find'. | ||
# Returns true if found, false otherwise. | ||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: linear O(n) - n represents length of the array | ||
# Space complexity: O(1) | ||
def search(array, length, value_to_find) | ||
Comment on lines
+33
to
35
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 | ||
|
||
length.times do |i| | ||
return true if array[i] == value_to_find | ||
end | ||
return false | ||
end | ||
|
||
# Finds and returns the largest integer value the array | ||
# Finds and returns the largest integer value in the array | ||
# Assumes that the array is not sorted. | ||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) - n represents length of the array | ||
# Space complexity: 0(1) | ||
def find_largest(array, length) | ||
Comment on lines
+45
to
47
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 | ||
largest_number = array[0] | ||
i = 0 | ||
while i < length | ||
if array[i] > largest_number | ||
largest_number = array[i] | ||
end | ||
i += 1 | ||
end | ||
return largest_number | ||
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) - n represents length of array | ||
# Space complexity: O(1) | ||
def find_smallest(array, length) | ||
Comment on lines
+62
to
64
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 | ||
smallest_number = array[0] | ||
i = 0 | ||
while i < length | ||
if array[i] < smallest_number | ||
smallest_number = array[i] | ||
end | ||
i += 1 | ||
end | ||
return smallest_number | ||
end | ||
|
||
# Reverses the values in the integer array in place | ||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) -- still the length of the array | ||
# Space complexity: O(1) | ||
def reverse(array, length) | ||
Comment on lines
+77
to
79
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 | ||
first = 0 | ||
last = length - 1 | ||
|
||
while first < last | ||
array_reverse = array[last] | ||
array[last] = array[first] | ||
array[first] = array_reverse | ||
first += 1 | ||
last -= 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) - n is length of array | ||
# Space complexity: O(1) | ||
def binary_search(array, length, value_to_find) | ||
Comment on lines
+95
to
97
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 = 0 | ||
high = length - 1 | ||
|
||
while low <= high | ||
mid = (low + high) / 2 | ||
if array[mid] == value_to_find | ||
return true | ||
elsif array[mid] < value_to_find | ||
low = mid + 1 | ||
else | ||
high = mid - 1 | ||
end | ||
end | ||
|
||
return false | ||
end | ||
|
||
|
||
# Helper method provided to sort the array in ascending order | ||
# Implements selection sort | ||
# Time complexity = O(n^2), where n is the number of elements in the array. | ||
|
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.
👍