Skip to content

feat: add quick select algorithm and test #6 #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 54 additions & 0 deletions sorts/quick_select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @function partition
* @description is a helper function used in the QuickSelect algorithm to partition the array around a pivot element
* @param {number[]} arr - The array to partition
* @param {number} left - The left boundary of the partition range
* @param {number} right - The right boundary of the partition range
* @param {number} pivotIndex - The index of the pivot element
* @returns {number} - The new index of the pivot element after partitioning
*/

function partition(arr: number[], left: number, right: number, pivotIndex: number): number {
const pivotValue = arr[pivotIndex];
[arr[pivotIndex], arr[right]] = [arr[right], arr[pivotIndex]];
let storeIndex = left;

for (let i = left; i < right; i++) {
if (arr[i] < pivotValue) {
[arr[i], arr[storeIndex]] = [arr[storeIndex], arr[i]];
storeIndex++;
}
}

[arr[storeIndex], arr[right]] = [arr[right], arr[storeIndex]];
return storeIndex;
}

/**
* @function quickSelect
* @description is an algorithm based on the QuickSort approach that selects the kth smallest element from an array
* @param {number[]} arr - The array from which to select the element
* @param {number} left - The left boundary of the array or subarray to consider
* @param {number} right - The right boundary of the array or subarray to consider
* @param {number} k - The index representing the kth smallest element to find
* @returns {number} - The kth smallest element from the array
*/

export function quickSelect(arr: number[], left: number, right: number, k: number): number {
if (left === right) {
return arr[left];
}

const pivotIndex = Math.floor(Math.random() * (right - left + 1)) + left;
const pivotNewIndex = partition(arr, left, right, pivotIndex);

if (k === pivotNewIndex) {
return arr[k];
} else if (k < pivotNewIndex) {
return quickSelect(arr, left, pivotNewIndex - 1, k);
} else {
return quickSelect(arr, pivotNewIndex + 1, right, k);
}
}


18 changes: 18 additions & 0 deletions sorts/test/quick_select.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { quickSelect } from "../quick_select";

describe("Quick Select", () => {

it("should return the correct value for worst case", () => {
const arr = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
const k = 2; // kth smallest element

expect(quickSelect(arr, 0, arr.length - 1, k)).toBe(3);
});

it("should return the correct value for best case", () => {
const arr = [1, 4, 2, 9, 5, 7, 3, 8, 10, 6];
const k = 5; // kth smallest element

expect(quickSelect(arr, 0, arr.length - 1, k)).toBe(6);
});
});