trekhleb / trekhleb/javascript-algorithms

QuickSort : Instead of taking pivot as 1st element, we should take the pivot as last element, due to time complexity

Open
#265 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
197k
Forks
31k
PR merge metrics
No merged PRs in 30d

Description

Instead of this
const pivotElement = array.shift(); // O(n)

we should use
const pivotElement = array.pop(); // O(1)

When taking the pivot as last element 2 test cases are failing.

  1. QuickSort › should do stable sorting
  2. QuickSort › should visit NOT SORTED array element specified number of times

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the QuickSort implementation where the pivot is selected, then run the two named failing tests: stable sorting and visiting unsorted elements the specified number of times. Done means the pivot change improves the stated complexity without breaking either test.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.