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

switch shift to pop #300

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

Conversation

henrycjchen
Copy link
Contributor

according to my test, pop is faster than shift, especially when the array is very long.

according to my test, pop is faster than shift, especially when the array is very long.
Copy link

@chandrunaik chandrunaik left a 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();

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.

Copy link
Contributor Author

@henrycjchen henrycjchen May 4, 2019

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

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) .

Copy link
Contributor Author

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
Copy link

codecov bot commented May 4, 2019

Codecov Report

Merging #300 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #300   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         149    149           
  Lines        2612   2612           
  Branches      434    434           
=====================================
  Hits         2612   2612
Impacted Files Coverage Δ
src/algorithms/sorting/quick-sort/QuickSort.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc1047d...c92f983. Read the comment docs.

@codecov
Copy link

codecov bot commented May 4, 2019

Codecov Report

Merging #300 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #300   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         149    147    -2     
  Lines        2612   2591   -21     
  Branches      434    432    -2     
=====================================
- Hits         2612   2591   -21
Impacted Files Coverage Δ
src/algorithms/sorting/quick-sort/QuickSort.js 100% <100%> (ø) ⬆️
...rc/data-structures/priority-queue/PriorityQueue.js 100% <0%> (ø) ⬆️
src/algorithms/graph/dijkstra/dijkstra.js 100% <0%> (ø) ⬆️
src/algorithms/math/bits/fullAdder.js
src/algorithms/math/square-root/squareRoot.js

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 94abfec...8f3da94. Read the comment docs.

@mik-laj
Copy link

mik-laj commented Jun 22, 2019

Related issues: #265

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.

None yet

4 participants