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

Sai's restricted array hw #22

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

Sai's restricted array hw #22

wants to merge 1 commit into from

Conversation

ssamant
Copy link

@ssamant ssamant commented Aug 26, 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. O(n) -- You have to visit each element in the array.
What is the space complexity of the length method? Provide justification. O(1) -- you only need to create one count var.
What is the time complexity of the print_array method? Provide justification. O(n) -- Visit every element in the array to print it
What is the space complexity of the print_array method? Provide justification. O(1) -- index variable only needed to print
What is the time complexity of the reverse method? Provide justification. O(n)/2 -- The two indices move in from each end of the array to the middle and the process stops when they meet
What is the space complexity of the reverse method? Provide justification. O(1) -- The array is reversed in place and does not require any ADTs -- only the temp vars.
What is the time complexity of the search method? Provide justification. O(n) -- in the worst case, the element is in the last position of the array
What is the space complexity of the search method? Provide justification. O(1) -- Only an index var is needed
What is the time complexity of the sort method? Provide justification. something like O((n*n-1)/2)-- For each index i, in the worst case j visits all the elements following it but does not have to visit elements that precede it.
What is the space complexity of the sort method? Provide justification. O(1) -- Sort in place so no ADT needed.
What is the time complexity of the delete method? Provide justification. O(n) -- Either you have to go to the end of the array to find the element to delete or once you find it you have to shift all the elements down and that still requires going to the end of the array
What is the space complexity of the delete method? Provide justification. O(1) -- only an index needed
What is the time complexity of the empty method? Provide justification. O(n) -- Need to visit all the elements in the array to empty them.
What is the space complexity of the empty method? Provide justification. O(1) -- Only need an index counter
What is the time complexity of the find_largest method? Provide justification. O(n) -- Need to visit entire array to know that you've found the largest element.
What is the space complexity of the find_largest method? Provide justification. O(1) -- Only need an index counter.
What is the time complexity of the insert_ascending method? Provide justification. O(n) -- In the worst case the element needs to be inserted in the front of the array and all following elements will need to be shifted down.
What is the space complexity of the insert_ascending method? Provide justification. O(1) -- No ADTs, only counters

# puts "****i: #{i} array#{i}: #{array[i]}"
while j < length do
# puts "j: #{j} array#{j}: #{array[j]}"
if array[j] < array[i]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you've gotten the basics right, just to make you think of further on another optimization: Imagine a case where the input array is sorted in descending order (opposite of what you are looking to do). In this case, you will swap at every step in the inner loop. Could we avoid or reduce this?

Since swapping is three times more work than saving a value, often, a min_index will be tracked. The min_index will be set to be the same as the value of i in the outer loop. In the inner loop, it will get updated to the value of j if array[j] < array[i]. After the inner loop completes, if i and min_index are not the same, only then, do the swap. Let me know if that makes sense.

# puts "value to insert: #{value_to_insert}"
has_room = false
i = 0
until array[i] == nil || has_room == true do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another optimization suggestion: You could eliminate this loop (which is O(n) operations) since you have access to the length of the array. You could instead check on array[length-1] == SPECIAL_VALUE to set has_room to true. (This would be O(1) operation since length is provided.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Aha! I see what you're doing - going backwards to insert. Nice trick! Ignore my previous comment.

@shrutivanw
Copy link
Collaborator

Nice work! Since you have the code already neat and tidy, I've made one suggestion for further optimization.

You also got all the space and time complexities right. For sorting, you're thinking on the right track. Although the inner iterator, j wouldn't look at items before the current value of i, it's upper bound is still determined by the size of the input. (It iterates through n-1 elements during the first iteration of the outer loop, n-2 elements in the second iteration of the outer loop and so on.)
Putting it all together, since the outer loop is of the order of n i.e. O(n), and the inner loop is also determined by and of the order of n i.e. O(n). Overall, this will be O(n^2).

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