Skip to content
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

Elizabeth Deutsch - Restricted Array Methods #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

edeutschie
Copy link

Restricted Array

Congratulations! You're submitting your assignment.

Comprehension Questions

What is the time and space complexity for each method you implemented? Provide justification.

Question Answer
NOTE: for the methods that start with checking if the array[0] == nil, we also have an element of O(1) which drops away as the greater complexity of the later steps outweigh it.
What is the time complexity of the length method? Provide justification. O(n) because in the worst case, the method has to go through and count each element of the array.
What is the space complexity of the length method? Provide justification. O(1) because the only thing the method stores are the variables "count" and "i" and it does not depend on input size of the array. That's two constants, so it reduces down to O(1).
What is the time complexity of the print_array method? Provide justification. O(n) because in the worst case, the method has to go through and print each element of the array.
What is the space complexity of the print_array method? Provide justification. O(1) because the one thing the method stores is the variable "i" and it does not depend on input size of the array.
What is the time complexity of the reverse method? Provide justification. O(n). The method only has one while loop. It goes through the loop once, and does touch every element of the array though it seems like only half of the events happen since if the array has 10 elements, there are only 5 swaps. Still I think the time complexity is linear.
What is the space complexity of the reverse method? Provide justification. I think it's O(1) again. There are more variables this time - i, j, & temp - but again it's 3 constants, so I think it reduces down to O(1) .
What is the time complexity of the search method? Provide justification. O(n) because in the worst case, the method has to go through and check each element of the array.
What is the space complexity of the search method? Provide justification. O(1) because the one thing the method stores is the variable "i" and it does not depend on input size of the array.
What is the time complexity of the delete method? Provide justification. The time complexity to go through the array and find the value_to_delete is O(n) because in the worst case scenario, the method will need to check each element of the array. The sort method I wrote is along the lines of selection sort which has a time complexity of O(n^2) or quadratic. So the time complexity would be O(n) + O(n^2) which boils down to O(n^2).
What is the space complexity of the delete method? Provide justification. The sort method and the wrapping delete method only have space complexities of O(1) - with the variables "i", "j", and "min_index" in the sort method, and the variable "i" in the delete method. So, the space complexity of the delete method is O(1) or constant space.
What is the time complexity of the empty method? Provide justification. O(n) because the method has to go through and change the value of each element in the array to the SPECIAL_VALUE.
What is the space complexity of the empty method? Provide justification. O(1) because the one thing the method stores is the variable "i" and it does not depend on input size of the array.
What is the time complexity of the find_largest method? Provide justification. O(n) because the method has to go through and check the value of each element in the array to see if it is the largest.
What is the space complexity of the find_largest method? Provide justification. The space complexity is O(1) because the one thing the method stores is the variable "max_val" and it does not depend on input size of the array. And maybe it keeps track of "i" though I don't set that variable. Still, that would result in O(1).
What is the time complexity of the insert_ascending method? Provide justification. For this method we have time complexities of O(n) for the until loop, O(1) for checking if there is a SPECIAL_VALUE, and O(n^2) for the sort. So, final time complexity of O(n^2). Would like to look at refactoring for a more efficient way to reorder the array after insertion.
What is the space complexity of the insert_ascending method? Provide justification. Like the delete method, the sort method and wrapping insert_ascending methods only have space complexities of O(1) - with the variables "i", "j", and "min_index" in the sort method, and the variable "i" in the insert_ascending method. So, the space complexity of the insert_ascending method is O(1) or constant space.

@@ -8,28 +8,94 @@

## Calculates the length of the restricted integer array_size
def length(array)
puts "NOT IMPLEMENTED"
if array[0] == nil
return nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return 0. The length will be 0 if the array is empty.

until j == length
if array[min_index] > array[j]
min_index = j
j += 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: you could end the conditional statement after updating min_index. And have j += 1 outside the conditional since it applies in both the if case and the else case.

@shrutivanw
Copy link
Collaborator

Apart from the minor inline comments;

  • Can you avoid calling sort in the delete method and author a delete algorithm which has a time complexity of O(n)?

  • Can you avoid calling sort in the insert_ascending method and author an insert_ascending algorithm which has a time complexity of O(n)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants