Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 82 additions & 21 deletions lib/using_restricted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +9 to 11
Copy link
Collaborator

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)

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down