-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
switch shift to pop #300
base: master
Are you sure you want to change the base?
switch shift to pop #300
Conversation
according to my test, pop is faster than shift, especially when the array is very long.
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.
Invalid usage.
@@ -19,12 +19,12 @@ export default class QuickSort extends Sort { | |||
const rightArray = []; | |||
|
|||
// Take the first element of array as a pivot. | |||
const pivotElement = array.shift(); | |||
const pivotElement = array.pop(); |
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.
shift and pop are used for different purposes. shift removes element from the front of the array and pop removes from the end. hence pop cannot used to get the first element in the array.
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.
in this sutiation, it can work well no matter the element is taken from the front of the array or the end
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.
@chandrunaik and @mrbeannnnn , in this case pop is better than shift because pop is O(1) (constant operation) where as shift() is O(n) .
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.
quick-sort should work well not metter the element is get from the top or from the end.
while in JS, pop is faster than shift, as talentedandrew said above.
so I think pop is better here.
Codecov Report
@@ Coverage Diff @@
## master #300 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 149 149
Lines 2612 2612
Branches 434 434
=====================================
Hits 2612 2612
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #300 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 149 147 -2
Lines 2612 2591 -21
Branches 434 432 -2
=====================================
- Hits 2612 2591 -21
Continue to review full report at Codecov.
|
Related issues: #265 |
according to my test, pop is faster than shift, especially when the array is very long.