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

Restricted Array assignment #10

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

Conversation

acgillette
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
What is the time complexity of the length method? Provide justification. Time is O(n) because you must go through every element of the array.
What is the space complexity of the length method? Provide justification. Space is O(1) because space doesn't change as the array size changes
What is the time complexity of the print_array method? Provide justification. Time is O(n) because you go through every element of the array
What is the space complexity of the print_array method? Provide justification. O(1) because the space needed doesn't change with the size of the array
What is the time complexity of the reverse method? Provide justification. O(n2) because every element in the array is visited twice
What is the space complexity of the reverse method? Provide justification. O(1) because the space does not change with the size of the array
What is the time complexity of the search method? Provide justification. O(n) because at worst case every element is visited once in the array
What is the space complexity of the search method? Provide justification. O(1) because the space needed doesn't change with the size of the array
What is the time complexity of the delete method? Provide justification. O(n) because every element might be visited once
What is the space complexity of the delete method? Provide justification. O(1) because the space needed doesn't change with the size of the array
What is the time complexity of the empty method? Provide justification. O(n) because every element of the array is visited once
What is the space complexity of the empty method? Provide justification. O(1) because the space needed doesn't change with the size of the array
What is the time complexity of the find_largest method? Provide justification. O(n) because every element of the array is visited once
What is the space complexity of the find_largest method? Provide justification. O(1) because the space needed doesn't change with the size of the array
What is the time complexity of the insert_ascending method? Provide justification. O(n) because every element is visited once
What is the space complexity of the insert_ascending method? Provide justification. O(n) because extra space is stored. I'm not quite sure about this though to be honest.

@shrutivanw
Copy link
Collaborator

Looks good! Just 3 pieces of feedback:

In your reverse implementation, in the first iteration through the outer loop, when i = 0 is when all the reverse work happens. The inner loop takes over and does all the work until i > j - when reversing is done. Then you continue through the outer loop essentially not doing any action other than increment i further until it reaches the end. So, you could simplify this by removing the outer loop and just setting i to 0.
Even in it's current implementation, reverse will have time complexity of O(n) and not O(n^2). In the inner loop: You are visiting two elements, one from the fist half and one from the second half at once. The loop will get run n/2 times or in asymptotic terms O(n) times. The outer loop runs from i value of 0 to length, but i also gets incremented in the inner loop and only gets executed once. So overall, the loops together get run based on the value of i, i.e. n times.

Currently, your delete code fragments the array. e.g. if the ascending order sorted array was [2, 3, 4, 5] and value_to_delete was 3, then you make the array [2, SPECIAL_VALUE, 4, 5]. How would you update it so that all the SPECIAL_VALUEs are in the end?

In insert_ascending, the additional memory you're allocating are i, is_inserted and temp. This will not change if the input array size changes and hence the space complexity of insert_ascending is O(1).

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