-
Notifications
You must be signed in to change notification settings - Fork 31
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
base: master
Are you sure you want to change the base?
Conversation
# puts "****i: #{i} array#{i}: #{array[i]}" | ||
while j < length do | ||
# puts "j: #{j} array#{j}: #{array[j]}" | ||
if array[j] < array[i] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
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.) |
Restricted Array
Congratulations! You're submitting your assignment.
Comprehension Questions
What is the time and space complexity for each method you implemented? Provide justification.