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

Add iterative quick sort #215

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

Conversation

yavorski
Copy link
Contributor

@yavorski yavorski commented Sep 27, 2018

Hello,

Iterative quick sort is a popular interview question, so i think it would be nice to have it here.

@codecov
Copy link

codecov bot commented Sep 27, 2018

Codecov Report

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

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #215   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         143    144    +1     
  Lines        2556   2587   +31     
  Branches      422    427    +5     
=====================================
+ Hits         2556   2587   +31
Impacted Files Coverage Δ
...lgorithms/sorting/quick-sort/QuickSortIterative.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 afa4948...152971f. Read the comment docs.

/**
* Replace recursion with auxiliary stack
*/
const stack = new Stack();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice addition! Just to nitpick, it might be a little cleaner to store arrays on the stack. That way, each item in the stack corresponds to a single iteration.
e.g.

    let lowIndex = inputLowIndex;
    let highIndex = inputHighIndex;
    let partitionIndex;
    
    stack.push([ lowIndex, highIndex ]);

    while (!stack.isEmpty()) {
      [ lowIndex, highIndex ] = stack.pop();
      partitionIndex = partitionArray(lowIndex, highIndex);
      if (partitionIndex - 1 > lowIndex) {
        stack.push([ lowIndex, partitionIndex - 1 ]);
      }
      if (partitionIndex + 1 < highIndex) {
        stack.push([ partitionIndex + 1, highIndex ]);
      }
    }

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

2 participants