Skip to content

Commit 0fa510b

Browse files
authored
feat: add quick select algorithm and test TheAlgorithms#6 (TheAlgorithms#140)
* feat: add quick select algorithm and test TheAlgorithms#6 * feat: update quick select algorithm and test TheAlgorithms#6
1 parent 112ea29 commit 0fa510b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

sorts/quick_select.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {partition} from "./quick_sort";
2+
/**
3+
* @function QuickSelect
4+
* @description is an algorithm based on the QuickSort approach that selects the kth smallest element from an array
5+
* @param {number[]} array - The array from which to select the element
6+
* @param {number} k - The index representing the kth smallest element to find
7+
* @param {number} left - The left boundary of the array or subarray to consider (default: 0)
8+
* @param {number} right - The right boundary of the array or subarray to consider (default: array.length - 1)
9+
* @returns {number} - The kth smallest element from the array
10+
* @throws {Error} - If k is out of bounds (less than 0 or greater than or equal to array.length)
11+
*/
12+
13+
export const QuickSelect = (
14+
array: number[],
15+
k: number,
16+
left: number = 0,
17+
right: number = array.length - 1
18+
):number => {
19+
if(k < 0 || k >= array.length) {
20+
throw new Error('k is out of bounds');
21+
}
22+
if (left === right) {
23+
// If the list contains only one element, return that element
24+
return array[left];
25+
}
26+
27+
// Partition the array
28+
let pivotIndex = partition(array, left, right);
29+
30+
// The pivot is in its final sorted position
31+
if (k === pivotIndex) {
32+
return array[k];
33+
} else if (k < pivotIndex) {
34+
// If k is less than the pivot index, look left
35+
return QuickSelect(array, k, left, pivotIndex - 1);
36+
} else {
37+
// If k is greater than the pivot index, look right
38+
return QuickSelect(array, k, pivotIndex + 1, right);
39+
}
40+
};

sorts/test/quick_select.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { QuickSelect } from "../quick_select";
2+
3+
describe('QuickSelect', () => {
4+
test('should return the kth smallest element in an array', () => {
5+
const array = [8, 3, 5, 1, 4, 2];
6+
expect(QuickSelect(array, 0)).toBe(1);
7+
expect(QuickSelect(array, 1)).toBe(2);
8+
expect(QuickSelect(array, 2)).toBe(3);
9+
expect(QuickSelect(array, 3)).toBe(4);
10+
expect(QuickSelect(array, 4)).toBe(5);
11+
expect(QuickSelect(array, 5)).toBe(8);
12+
});
13+
14+
test('should work with arrays of size 1', () => {
15+
const array = [4];
16+
expect(QuickSelect(array, 0)).toBe(4);
17+
});
18+
19+
test('should work with large arrays', () => {
20+
const array = Array.from({length: 1000}, (_, i) => i + 1);
21+
expect(QuickSelect(array, 499)).toBe(500);
22+
});
23+
24+
test('should throw error when k is out of bounds', () => {
25+
const array = [8, 3, 5, 1, 4, 2];
26+
expect(() => QuickSelect(array, -1)).toThrow();
27+
expect(() => QuickSelect(array, 6)).toThrow();
28+
});
29+
});

0 commit comments

Comments
 (0)