From 53d51de06ef3b0ba47065febcc8d24b4e1e71158 Mon Sep 17 00:00:00 2001 From: Nathan Chin-Lue Date: Tue, 16 Jun 2026 15:52:52 -0400 Subject: [PATCH 1/2] Use last element as QuickSort pivot --- src/algorithms/sorting/quick-sort/QuickSort.js | 15 ++++++++------- .../sorting/quick-sort/__test__/QuickSort.test.js | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/algorithms/sorting/quick-sort/QuickSort.js b/src/algorithms/sorting/quick-sort/QuickSort.js index 1c63c86e8b..026006c0f0 100644 --- a/src/algorithms/sorting/quick-sort/QuickSort.js +++ b/src/algorithms/sorting/quick-sort/QuickSort.js @@ -18,14 +18,12 @@ export default class QuickSort extends Sort { const leftArray = []; const rightArray = []; - // Take the first element of array as a pivot. - const pivotElement = array.shift(); - const centerArray = [pivotElement]; + // Take the last element of array as a pivot. + const pivotElement = array.pop(); + const centerArray = []; // Split all array elements between left, center and right arrays. - while (array.length) { - const currentElement = array.shift(); - + array.forEach((currentElement) => { // Call visiting callback. this.callbacks.visitingCallback(currentElement); @@ -36,7 +34,10 @@ export default class QuickSort extends Sort { } else { rightArray.push(currentElement); } - } + }); + + // Put the pivot after all equal elements that appeared before it to keep the sort stable. + centerArray.push(pivotElement); // Sort left and right arrays. const leftArraySorted = this.sort(leftArray); diff --git a/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js b/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js index 71c1fe71e0..819114e0a8 100644 --- a/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js +++ b/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js @@ -9,7 +9,7 @@ import { // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 190; -const NOT_SORTED_ARRAY_VISITING_COUNT = 62; +const NOT_SORTED_ARRAY_VISITING_COUNT = 85; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 190; const EQUAL_ARRAY_VISITING_COUNT = 19; From e9cad4a8f3c7021fe16e8b939607de73cb85fe5c Mon Sep 17 00:00:00 2001 From: Nathan Chin-Lue Date: Fri, 19 Jun 2026 16:14:47 -0400 Subject: [PATCH 2/2] Fix Kruskal MST vertex order assertion --- src/algorithms/graph/kruskal/__test__/kruskal.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/graph/kruskal/__test__/kruskal.test.js b/src/algorithms/graph/kruskal/__test__/kruskal.test.js index bf96fa388c..6df30e8d5a 100644 --- a/src/algorithms/graph/kruskal/__test__/kruskal.test.js +++ b/src/algorithms/graph/kruskal/__test__/kruskal.test.js @@ -86,6 +86,6 @@ describe('kruskal', () => { expect(minimumSpanningTree.getWeight()).toBe(3); expect(minimumSpanningTree.getAllVertices().length).toBe(graph.getAllVertices().length); expect(minimumSpanningTree.getAllEdges().length).toBe(graph.getAllVertices().length - 1); - expect(minimumSpanningTree.toString()).toBe('A,B,C,D'); + expect(minimumSpanningTree.getAllVertices().map((vertex) => vertex.getKey()).sort()).toEqual(['A', 'B', 'C', 'D']); }); });