Fix sort testcase input sorted unintentionally#256
Conversation
A slice is a references to its underlying array. If a slice is passed as an argument and modified in a function, the original slice is also modified. As getSortedVersion and other sorting functions modify a given slice, testcase inputs are unintentionally sorted beforehand. To avoid that, passes a cloned slices to these functions.
tjgurwara99
left a comment
There was a problem hiding this comment.
All seems good to me. Maybe add a comment on top of getSortedVersion and cloneIntSlice to make it easier to understand what is happening, even though it's mostly self explanatory.
This timeout is caused by fixing an issue that slices of testcases were unintentionally being sorted. If array is sorted, the complexities of bubble sort and insertion sort are O(n), but in general they are O(n^2) and 500k is large enough to cause timeout.
125e574 to
0bbc1db
Compare
|
@tjgurwara99 Now that |
tjgurwara99
left a comment
There was a problem hiding this comment.
Good work catching that unintentional sorting!
Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
|
This issue has been resolved recently when I was restructuring the repo - I completely forgot that there was a PR open on that exact same issue of test cases being sorted resulting in false passes. If I remembered I would have merged your PR but since this has been resolved, I am closing this now. |
As
getSortedVersionand other sorting functions modify a given slice, testcase inputs are unintentionally sorted beforehand.To avoid that, passes a cloned slices to these functions.