Skip to content
Merged
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
18 changes: 16 additions & 2 deletions sorts/quick_sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const partition = (
left: number = 0,
right: number = array.length - 1
) => {
const pivot = array[Math.floor((right + left) / 2)];
const pivot = array[choosePivot(left,right)];
let i = left;
let j = right;

Expand All @@ -33,6 +33,20 @@ export const partition = (
return i;
};

/**
* @function choosePivot
* @description Chooses a pivot element randomly within the subarray.
* @param {number} left - The left index of the subarray.
* @param {number} right - The right index of the subarray.
* @returns {number} - The index of the chosen pivot element.
*/
const choosePivot = (
left: number,
right: number
): number => {
return Math.floor(Math.random() * (right - left + 1)) + left
};

/**
* Quicksort implementation
*
Expand All @@ -55,7 +69,7 @@ export const QuickSort = (
array: number[],
left: number = 0,
right: number = array.length - 1
) => {
): number[] => {
let index;

if (array.length > 1) {
Expand Down