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

Lynn Trickey's Restricted Array implementations #21

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

Lynn Trickey's Restricted Array implementations #21

wants to merge 4 commits into from

Conversation

ltrickey
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. The length method has linear time complexity or 0(n). This is because it has to visit each element of the array and add it to the count, so the time it takes directly corresponds to the length of the array.
What is the space complexity of the length method? Provide justification. The Space complexity is 0(1) or constant. Because it only creates two variables no matter how large or small the array is.
What is the time complexity of the print_array method? Provide justification. The print array also has time complexity of 0(n) - also increases depending on the size linearly because it has to visit each element one time.
What is the space complexity of the print_array method? Provide justification. Space complexity is constant because no matter what size of array it only creates one variable.
What is the time complexity of the reverse method? Provide justification. Reverse also has time complexity of o(n) because with the two iteration variables - i & j - you only have to visit each element in the array only once. When they cross the method stops. Meaning again the time the method takes to run directly corresponds to the length of the array.
What is the space complexity of the reverse method? Provide justification. Space complexity again is 0(1) constant because the only thing we're creating are those two variables i & j.
What is the time complexity of the search method? Provide justification. In my search implementation of Java I use my own length method to find the length which is 0(n). Then I have to check (worst case) every element in the array which could be 0(n) as well, meaning the time complexity works down to 0(2n) or 0(n) linear. Even though we go through twice, the time still directly corresponds to the length of the array.
What is the space complexity of the search method? Provide justification. Again the space complexity is 0(1) constant because no matter the input size we're always only creating the iteration variable and the length variable.
What is the time complexity of the delete method? Provide justification. This one I'm a little confused about. I think in my Java implementation I may be missing a return - I think it could also be 0(n) because once we find the element to delete and remove it, we're simply moving every other item from that point to the end of the array up by one space. So no matter if the element to replace is the first or last, we're only visiting each element once? But this is assuming there is only ONE element to replace - if there could be more than one we'd have to revisit each element of the array after that initial shifting of elements, making it 0(n^2)
What is the space complexity of the delete method? Provide justification. Again constant - same variables are created no matter what size of array we're calling the method on.
What is the time complexity of the empty method? Provide justification. Empty is 0(n) - visit each element once, so time is based on length of array. Linear.
What is the space complexity of the empty method? Provide justification. Constant - same variables are created no matter what size of array.
What is the time complexity of the find_largest method? Provide justification. 0(n) - only visiting each element one time.
What is the space complexity of the find_largest method? Provide justification. 0(1) - same variables - largest, iteration variables - created no matter what size.
What is the time complexity of the insert_ascending method? Provide justification. I believe this is 0(n^2) because every time the method visits the next item in the array it has to compare it to the remaining items in the array, meaning that it does a loop within a loop. So the time complexity increases to the second power with the size of the array.
What is the space complexity of the insert_ascending method? Provide justification. Again constant as we're only creating variables

@shrutivanw
Copy link
Collaborator

Nicely done, with setting up everything in Java. When you create an int array in Java, you're essentially working with a restricted i.e. native array.

The only pedantic, code optimization, I'd suggest is to see if you can avoid one variable while swapping elements in the array. i.e. save valueAtI and avoid saving valueAtJ.

Comments on time complexity:

  • For the delete method, you're explanation is correct. It will be O(n) time complexity. Even if there were repeating elements, the array given is sorted, so you will then have a count of number of elements to delete and offset the moving of elements by those many elements (i.e. j will start at (i + countOfElementsToDete)). So, you'll still have O(n) time complexity. If there are multiple elements of different values to delete, the delete method will get called once for each. And the time complexity of the delete method will still remain O(n).

  • insert_ascending should be O(n). It's actually similar to delete algorithm. In that once you discover the value to insert at, you move elements forward by one step there onwards. In other words, i keeps getting incremented in the inner loop and when i reaches length, both loops terminate. Overall, you visit each element once.

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