Skip to content

Commit

Permalink
refactor: optimize quick sort
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhermawan committed Sep 12, 2023
1 parent c1de73b commit 2a7d46c
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/quick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@ import { CompareFn } from "./types";

export function quickSort<T>(arr: T[], compareFn: CompareFn<T>): T[] {
const sortedArray = arr.slice();
sortHelper(sortedArray, 0, sortedArray.length - 1, compareFn);

if (sortedArray.length <= 1) return sortedArray;

const pivotIndex = sortedArray.length - 1;
const pivot = sortedArray[pivotIndex];
let partitionIndex = 0;
return sortedArray;
}

for (let i = 0; i < pivotIndex; i++) {
const element = sortedArray[i];
if (compareFn(element, pivot) < 0) {
const temp = sortedArray[i];
sortedArray[i] = sortedArray[partitionIndex];
sortedArray[partitionIndex] = temp;
function sortHelper<T>(
arr: T[],
start: number,
end: number,
compareFn: CompareFn<T>
): void {
if (start < end) {
const partitionIndex = partition(arr, start, end, compareFn);
sortHelper(arr, start, partitionIndex - 1, compareFn);
sortHelper(arr, partitionIndex + 1, end, compareFn);
}
}

function partition<T>(
arr: T[],
start: number,
end: number,
compareFn: CompareFn<T>
): number {
const pivot = arr[end];
let partitionIndex = start;

for (let i = start; i < end; i++) {
if (compareFn(arr[i], pivot) < 0) {
[arr[i], arr[partitionIndex]] = [arr[partitionIndex], arr[i]];
partitionIndex++;
}
}

const temp = sortedArray[pivotIndex];
sortedArray[pivotIndex] = sortedArray[partitionIndex];
sortedArray[partitionIndex] = temp;

const left = sortedArray.slice(0, partitionIndex);
const right = sortedArray.slice(partitionIndex + 1);
[arr[partitionIndex], arr[end]] = [arr[end], arr[partitionIndex]];

return [...quickSort(left, compareFn), pivot, ...quickSort(right, compareFn)];
return partitionIndex;
}

0 comments on commit 2a7d46c

Please sign in to comment.