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

Cynthia Cobb - Restricted Array #6

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

Conversation

cynthiacd
Copy link

@cynthiacd cynthiacd commented Aug 17, 2017

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. linear - depends on size of array
What is the space complexity of the length method? Provide justification. constant - only assigning/storing an integer value no matter size
What is the time complexity of the print_array method? Provide justification. linear - depends on array size
What is the space complexity of the print_array method? Provide justification. constant - no new data types assigned
What is the time complexity of the reverse method? Provide justification. linear - depends on array size
What is the space complexity of the reverse method? Provide justification. constant - modifying original array not assigning a new copy
What is the time complexity of the search method? Provide justification. linear - depends on size of array
What is the space complexity of the search method? Provide justification. constant - not creating any new ADT - just returning a Boonlean
What is the time complexity of the delete method? Provide justification. linear - depends on array size
What is the space complexity of the delete method? Provide justification. constant - array stays the same size & no new ADT created in method
What is the time complexity of the empty method? Provide justification. linear - depends on size of array
What is the space complexity of the empty method? Provide justification. constant - array stays the same size & no new ADT was created in method
What is the time complexity of the find_largest method? Provide justification. linear - depends on size of array
What is the space complexity of the find_largest method? Provide justification. you make one new variable integer assignment to hold the value of the largest
What is the time complexity of the insert_ascending method? Provide justification. n^2 because there are nested times loops that depend on the size of the array
What is the space complexity of the insert_ascending method? Provide justification. constant - the array is modified & no new ADT were created - just couple integers to track indices

@shrutivanw
Copy link
Collaborator

Nice work!

A few comments:

  • Space complexity of find_largest: you're right, you make one new variable integer assignment to hold the value of the largest. So, this is constant space since it does not depend on array length.

  • In your current implementation of delete, you're leaving the array fragmented. e.g. if the array was [2, 5, 6, 8, 9] and value_to_delete was 6, after delete method, the array will look like [2, 5, SPECIAL_VALUE, 8, 9]. How can you update it to have all the SPECIAL_VALUEs in the end?

  • In find_largest, the array is not expected to be sorted. In your conditional to update largest, you compare with array[0] instead of with the current value of largest. This may not always get you what you are looking for e.g. [6, 15, 9, 3]=> array[1] is greater than array[0], so largest will get updated to 15. Then, in the next iteration, array[2], which is 9 will also be greater than array[0] and largest will get updated to 9 and override 15. The method will return 9, when it should be returning 15. How will you fix this bug?

  • Minor: In insert_ascending, you actually don't need to search. Since you know the array is sorted in ascending order, you just need to check if array[length-1] is SPECIAL_VALUE. If it is, there is at least one element room to insert. This will make it so that the time complexity for checking if there is room to insert will be constant instead of O(n).

  • For insert_ascending, although there are nested loops, the inner loop gets executed only once and starts at the index where the outer loop is at. After the inner loop executes, the outer loop does not continue. Overall, in between the two loops, each element gets examined/shifted once. Leading to an overall 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