forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add quick select algorithm and test TheAlgorithms#6 (TheAlgorit…
…hms#140) * feat: add quick select algorithm and test TheAlgorithms#6 * feat: update quick select algorithm and test TheAlgorithms#6
- Loading branch information
1 parent
112ea29
commit 0fa510b
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {partition} from "./quick_sort"; | ||
/** | ||
* @function QuickSelect | ||
* @description is an algorithm based on the QuickSort approach that selects the kth smallest element from an array | ||
* @param {number[]} array - The array from which to select the element | ||
* @param {number} k - The index representing the kth smallest element to find | ||
* @param {number} left - The left boundary of the array or subarray to consider (default: 0) | ||
* @param {number} right - The right boundary of the array or subarray to consider (default: array.length - 1) | ||
* @returns {number} - The kth smallest element from the array | ||
* @throws {Error} - If k is out of bounds (less than 0 or greater than or equal to array.length) | ||
*/ | ||
|
||
export const QuickSelect = ( | ||
array: number[], | ||
k: number, | ||
left: number = 0, | ||
right: number = array.length - 1 | ||
):number => { | ||
if(k < 0 || k >= array.length) { | ||
throw new Error('k is out of bounds'); | ||
} | ||
if (left === right) { | ||
// If the list contains only one element, return that element | ||
return array[left]; | ||
} | ||
|
||
// Partition the array | ||
let pivotIndex = partition(array, left, right); | ||
|
||
// The pivot is in its final sorted position | ||
if (k === pivotIndex) { | ||
return array[k]; | ||
} else if (k < pivotIndex) { | ||
// If k is less than the pivot index, look left | ||
return QuickSelect(array, k, left, pivotIndex - 1); | ||
} else { | ||
// If k is greater than the pivot index, look right | ||
return QuickSelect(array, k, pivotIndex + 1, right); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { QuickSelect } from "../quick_select"; | ||
|
||
describe('QuickSelect', () => { | ||
test('should return the kth smallest element in an array', () => { | ||
const array = [8, 3, 5, 1, 4, 2]; | ||
expect(QuickSelect(array, 0)).toBe(1); | ||
expect(QuickSelect(array, 1)).toBe(2); | ||
expect(QuickSelect(array, 2)).toBe(3); | ||
expect(QuickSelect(array, 3)).toBe(4); | ||
expect(QuickSelect(array, 4)).toBe(5); | ||
expect(QuickSelect(array, 5)).toBe(8); | ||
}); | ||
|
||
test('should work with arrays of size 1', () => { | ||
const array = [4]; | ||
expect(QuickSelect(array, 0)).toBe(4); | ||
}); | ||
|
||
test('should work with large arrays', () => { | ||
const array = Array.from({length: 1000}, (_, i) => i + 1); | ||
expect(QuickSelect(array, 499)).toBe(500); | ||
}); | ||
|
||
test('should throw error when k is out of bounds', () => { | ||
const array = [8, 3, 5, 1, 4, 2]; | ||
expect(() => QuickSelect(array, -1)).toThrow(); | ||
expect(() => QuickSelect(array, 6)).toThrow(); | ||
}); | ||
}); |