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

Erica Case - Restricted array #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

EricaJCasePhD
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 complexity is O(n) as we loop through the array once.
What is the space complexity of the length method? Provide justification.
Space complexity is O(1)...the array is not copied.
What is the time complexity of the print_array method? Provide justification.
Time complexity is O(n) as we loop through the array once. |

| What is the space complexity of the print_array method? Provide justification. |
Space complexity is O(1)...the array is not copied. |
| What is the time complexity of the reverse method? Provide justification. |
Time complexity is O(n)...technically O(n)/2 I believe. We visit each element once.|
| What is the space complexity of the reverse method? Provide justification. |
Space complexity is O(1)...no array copies. |
| What is the time complexity of the search method? Provide justification. |
O(n). We visit each element once.|
| What is the space complexity of the search method? Provide justification. |
O(1)...no additional storage. |
| What is the time complexity of the delete method? Provide justification. |
O(n^2). We have a nested loop. |
| What is the space complexity of the delete method? Provide justification. |
O(1). No additional storage. |
| What is the time complexity of the empty method? Provide justification. |
O(n) each element visited once.|
| What is the space complexity of the empty method? Provide justification. |
O(1)...no additional storage. |
| What is the time complexity of the find_largest method? Provide justification. | O(n)...loop through once |
| What is the space complexity of the find_largest method? Provide justification. | O(1) no additional storage |
| What is the time complexity of the insert_ascending method? Provide justification. | O(n) we visit each element in the array twice not nested = O(2N) = O(n) |
| What is the space complexity of the insert_ascending method? Provide justification. | O(1)..no additional storage |

@shrutivanw
Copy link
Collaborator

shrutivanw commented Aug 21, 2017

Nice work!

Just one comment:
Delete - How can you update the delete algorithm such that even thought there are nested loops, the overall time complexity remains O(n)?
Considerations:
(i) If your method assumes that all values are unique, then after the inner loop gets executed, you can return. The outer loop executes until you find the element to delete or end of array. The inner loop executes only once, if the element to delete is found and it scans through the rest of the array making it overall, O(n) time complexity.
(ii) If your method accounts for deletes all repeating values, then after finding the first value_to_delete, count number of entries to delete and instead of array[j] = array[j+1] in the inner loop, you'll have array[j] = array[j+count_to_delete]. Overall, the inner loop will get executed just once, when the first element to delete is found and the overall time complexity will still be 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