Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/algorithms/sorting/quick-sort/QuickSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down