javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

choose pivot from median of three elements for quicksort

Open larskotthoff opened this issue 7 years ago • 9 comments

The method for choosing the pivot as implemented at the moment results in degenerate behaviour when the array is already sorted or reverse sorted. A better way is to choose the median of three elements (first, mid, last) as the pivot. See Robert Sedgewick. Implementing Quicksort Programs. Commun. ACM, 1(10):847–857, October 1978.

larskotthoff avatar Aug 15 '18 23:08 larskotthoff

Test failures seem to be unrelated? Not sure what's going on there.

larskotthoff avatar Aug 17 '18 15:08 larskotthoff

I think it is critical to choose appropriate pivot. If you fix the conflict, it would be really helpful!

orist22 avatar Dec 07 '18 05:12 orist22

image The code bot says that you exceeded the maximum size of array. I hope it would be helpful!

orist22 avatar Dec 07 '18 05:12 orist22

Codecov Report

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

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #171   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         147    147           
  Lines        2590   2595    +5     
  Branches      432    434    +2     
=====================================
+ Hits         2590   2595    +5
Impacted Files Coverage Δ
src/algorithms/sorting/quick-sort/QuickSort.js 100% <100%> (ø) :arrow_up:
src/algorithms/graph/kruskal/kruskal.js 100% <100%> (ø) :arrow_up:

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 d9946c1...478abba. Read the comment docs.

codecov[bot] avatar Dec 11 '18 16:12 codecov[bot]

Codecov Report

Merging #171 into master will decrease coverage by 0.41%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #171      +/-   ##
==========================================
- Coverage     100%   99.58%   -0.42%     
==========================================
  Files         147      128      -19     
  Lines        2590     2429     -161     
  Branches      432      413      -19     
==========================================
- Hits         2590     2419     -171     
- Misses          0        9       +9     
- Partials        0        1       +1
Impacted Files Coverage Δ
src/algorithms/sorting/quick-sort/QuickSort.js 100% <100%> (ø) :arrow_up:
src/algorithms/graph/kruskal/kruskal.js 44.44% <0%> (-55.56%) :arrow_down:
...algorithms/sorting/selection-sort/SelectionSort.js 100% <0%> (ø) :arrow_up:
...hms/math/euclidean-algorithm/euclideanAlgorithm.js 100% <0%> (ø) :arrow_up:
src/data-structures/stack/Stack.js 100% <0%> (ø) :arrow_up:
src/utils/comparator/Comparator.js 100% <0%> (ø) :arrow_up:
src/algorithms/sorting/bubble-sort/BubbleSort.js 100% <0%> (ø) :arrow_up:
...rc/algorithms/search/binary-search/binarySearch.js 100% <0%> (ø) :arrow_up:
src/data-structures/trie/Trie.js 100% <0%> (ø) :arrow_up:
src/data-structures/heap/MinHeap.js 100% <0%> (ø) :arrow_up:
... and 32 more

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 d9946c1...f4668db. Read the comment docs.

codecov[bot] avatar Dec 11 '18 16:12 codecov[bot]

Should be fixed -- I wasn't choosing pivots the right way, and it turned out Kruskal's algorithm was broken...

larskotthoff avatar Dec 11 '18 17:12 larskotthoff

@trekhleb Could you have a look please? This makes quicksort better and fixes a bug in Kruskal's algorithm that breaks it in some cases.

larskotthoff avatar Dec 11 '18 18:12 larskotthoff

The method for choosing the pivot as implemented at the moment results in degenerate behaviour when the array is already sorted or reverse sorted. A better way is to choose the median of three elements (first, mid, last) as the pivot. See Robert Sedgewick. Implementing Quicksort Programs. Commun. ACM, 1(10):847–857, October 1978.

What a nice improvement. @larskotthoff Thanks a lot.

The fact that this PR is not merged in is a loss for the repository.

RayJune avatar Dec 20 '21 03:12 RayJune

The repo is mostly dead.

lazarljubenovic avatar Jan 11 '22 15:01 lazarljubenovic