Skip to content

Commit f071220

Browse files
chore: merged "Quick select tests" (#697)
* add link to function description * fix formatting for consistency * export function so it can be imported for testing * throw error on invalid input * add tests for QuickSelect.js * remove unnecessary comment * Update Data-Structures/Array/QuickSelect.js Co-authored-by: Rak Laptudirm <[email protected]> Co-authored-by: Rak Laptudirm <[email protected]>
1 parent 36603e7 commit f071220

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

Data-Structures/Array/QuickSelect.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* QuickSelect is an algorithm to find the kth smallest number
2+
* [QuickSelect](https://www.geeksforgeeks.org/quickselect-algorithm/) is an algorithm to find the kth smallest number
33
*
44
* Notes:
55
* -QuickSelect is related to QuickSort, thus has optimal best and average
@@ -13,18 +13,13 @@
1313

1414
function QuickSelect (items, kth) { // eslint-disable-line no-unused-vars
1515
if (kth < 1 || kth > items.length) {
16-
return 'Index Out of Bound'
16+
throw new RangeError('Index Out of Bound')
1717
}
1818

1919
return RandomizedSelect(items, 0, items.length - 1, kth)
2020
}
2121

22-
function RandomizedSelect (
23-
items,
24-
left,
25-
right,
26-
i
27-
) {
22+
function RandomizedSelect (items, left, right, i) {
2823
if (left === right) return items[left]
2924

3025
const pivotIndex = RandomizedPartition(items, left, right)
@@ -76,3 +71,5 @@ function Swap (arr, x, y) {
7671
// "Index Out of Bound"
7772
// > QuickSelect([1, 4, 2, -2, 4, 5], 7)
7873
// "Index Out of Bound"
74+
75+
export { QuickSelect }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { QuickSelect } from './QuickSelect'
2+
3+
describe('QuickSelect tests', () => {
4+
it('should return the only element of a list of length 1', () => {
5+
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
6+
expect(QuickSelect([100], 1)).toEqual(100)
7+
expect(QuickSelect([-23], 1)).toEqual(-23)
8+
expect(QuickSelect([2007.102], 1)).toEqual(2007.102)
9+
expect(QuickSelect([0.9], 1)).toEqual(0.9)
10+
expect(QuickSelect([-0.075], 1)).toEqual(-0.075)
11+
expect(QuickSelect([0], 1)).toEqual(0)
12+
expect(QuickSelect([1], 1)).toEqual(1)
13+
})
14+
15+
it('should throw an Error when k is greater than the length of the list', () => {
16+
expect(() => QuickSelect([100, 2], 5)).toThrow('Index Out of Bound')
17+
})
18+
19+
it('should throw an Error when k is less than 1', () => {
20+
expect(() => QuickSelect([100, 2], 0)).toThrow('Index Out of Bound')
21+
expect(() => QuickSelect([100, 2], -1)).toThrow('Index Out of Bound')
22+
})
23+
24+
describe('varieties of list composition', () => {
25+
it('should return the kth smallest element of a list that is in increasing order', () => {
26+
expect(QuickSelect([10, 22, 33, 44, 55], 1)).toEqual(10)
27+
expect(QuickSelect([10, 22, 33, 44, 55], 2)).toEqual(22)
28+
expect(QuickSelect([10, 22, 33, 44, 55], 3)).toEqual(33)
29+
expect(QuickSelect([10, 22, 33, 44, 55], 4)).toEqual(44)
30+
expect(QuickSelect([10, 22, 33, 44, 55], 5)).toEqual(55)
31+
})
32+
33+
it('should return the kth smallest element of an input list that is in decreasing order', () => {
34+
expect(QuickSelect([82, 33.12, 4.0, 1], 1)).toEqual(1)
35+
expect(QuickSelect([82, 33.12, 4.0, 1], 2)).toEqual(4.0)
36+
expect(QuickSelect([82, 33.12, 4.0, 1], 2)).toEqual(4)
37+
expect(QuickSelect([82, 33.12, 4.0, 1], 3)).toEqual(33.12)
38+
expect(QuickSelect([82, 33.12, 4.0, 1], 4)).toEqual(82)
39+
})
40+
41+
it('should return the kth smallest element of an input list that is no particular order', () => {
42+
expect(QuickSelect([123, 14231, -10, 0, 15], 3)).toEqual(15)
43+
expect(QuickSelect([0, 15, 123, 14231, -10], 3)).toEqual(15)
44+
expect(QuickSelect([-10, 15, 123, 14231, 0], 3)).toEqual(15)
45+
expect(QuickSelect([14231, 0, 15, 123, -10], 3)).toEqual(15)
46+
expect(QuickSelect([14231, 0, 15, -10, 123], 3)).toEqual(15)
47+
})
48+
})
49+
})

0 commit comments

Comments
 (0)