Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 22 additions & 16 deletions Search/QuickSelectSearch.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
/*
* Places the `k` smallest elements in `array` in the first `k` indices: `[0..k-1]`
* Modifies the passed in array *in place*
* Returns a slice of the wanted elements for convenience
* Efficient mainly because it never performs a full sort.
/**
* Quickselect algorithm to place the `k`-th smallest element in its final sorted index `k`,
* partitioning elements in-place: all elements at indices < `k` are <= array[k], and
* all elements at indices > `k` are >= array[k].
*
* The only guarantees are that:
* Supported Boundary Contract:
* - Precondition check established at Line 15: `if (!array || array.length <= k)`
* - Supported domain:
* - `array`: Non-empty Array of comparable numbers (`array.length >= 1`).
* - `k`: 0-based integer index within `[0, array.length - 1]`.
* - Smallest valid input boundary:
* - Single-element array (`array.length === 1`) with `k === 0`.
* - Upper valid domain boundary:
* - Any non-empty array with `k === array.length - 1`.
* - Unsupported domain (throws `new Error('Invalid arguments')` at Line 16):
* - Falsy arrays (`!array`): `null`, `undefined`.
* - Empty arrays (`array.length === 0 <= k`).
* - Out-of-bounds index (`k >= array.length`).
*
* - The `k`th element is in its final sort index (if the array were to be sorted)
* - All elements before index `k` are smaller than the `k`th element
* @param {Array<number>} array - The input array to partition in-place.
* @param {number} k - The 0-based target index to position (0 <= k < array.length).
* @returns {Array<number>} The mutated array with the k-th element in its final sorted index.
* @throws {Error} 'Invalid arguments' when array is falsy or array.length <= k (Line 15).
*
* [Reference](http://en.wikipedia.org/wiki/Quickselect)
* Reference: http://en.wikipedia.org/wiki/Quickselect
*/
export function quickSelectSearch(array, k) {
if (!array || array.length <= k) {
Expand Down Expand Up @@ -46,10 +59,3 @@ export function quickSelectSearch(array, k) {
}
return array
}

/* ---------------------------------- Test ---------------------------------- */

// const arr = [1121111, 21, 333, 41, 5, 66, 7777, 28, 19, 11110]
// quickSelectSearch(arr, 5) // [ 19, 21, 28, 41, 5, 66, 333, 11110, 1121111, 7777 ]
// quickSelectSearch(arr, 2) // [ 19, 5, 21, 41, 28, 333, 11110, 1121111, 7777, 66 ]
// quickSelectSearch(arr, 7) // [ 19, 5, 21, 41, 28, 66, 333, 7777, 11110, 1121111 ]
114 changes: 114 additions & 0 deletions Search/test/QuickSelectSearch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { quickSelectSearch } from '../QuickSelectSearch'

describe('quickSelectSearch', () => {
describe('Stated Fixture: Smallest valid input and ordinary input', () => {
it('handles the smallest valid input: 1-element array with k = 0 (Precondition line 15: array.length > k)', () => {
// Smallest valid input: single element array [42], target index k = 0
const input = [42]
const k = 0
// Independently calculated expected output: [42] where result[0] === 42
const expected = [42]
const result = quickSelectSearch(input, k)

expect(result).toBe(input)
expect(result[0]).toBe(expected[0])
expect(result).toEqual(expected)
})

it('handles one ordinary input: unsorted array with median target index k = 4 (Precondition line 15: array.length > k)', () => {
// Ordinary input: 9 unsorted distinct elements
const input = [9, 2, 7, 4, 5, 6, 3, 8, 1]
const k = 4
// Independently calculated expected output:
// Sorted elements would be [1, 2, 3, 4, 5, 6, 7, 8, 9]
// Element at sorted index k=4 is 5
// Partition guarantee: all elements at indices < 4 are <= 5, all elements at indices > 4 are >= 5
const result = quickSelectSearch(input, k)

expect(result).toBe(input)
expect(result[4]).toBe(5)
// Check left partition (< index 4)
for (let i = 0; i < 4; i++) {
expect(result[i]).toBeLessThanOrEqual(5)
}
// Check right partition (> index 4)
for (let i = 5; i < result.length; i++) {
expect(result[i]).toBeGreaterThanOrEqual(5)
}
})
})

describe('Supported Boundary Cases (Valid side of contract)', () => {
it('asserts lower supported boundary: k = 0 on multi-element array (Precondition line 15: array.length > k)', () => {
const input = [5, 1, 9, 3, 7]
const k = 0
// Independently calculated expected: smallest element (1) at index 0
const result = quickSelectSearch(input, k)

expect(result[0]).toBe(1)
for (let i = 1; i < result.length; i++) {
expect(result[i]).toBeGreaterThanOrEqual(1)
}
})

it('asserts upper supported boundary: k = array.length - 1 (Precondition line 15: array.length > k)', () => {
const input = [5, 1, 9, 3, 7]
const k = 4 // array.length - 1
// Independently calculated expected: largest element (9) at index 4
const result = quickSelectSearch(input, k)

expect(result[4]).toBe(9)
for (let i = 0; i < 4; i++) {
expect(result[i]).toBeLessThanOrEqual(9)
}
})

it('asserts two-element array boundaries: k = 0 and k = 1', () => {
const arr1 = [20, 10]
const res1 = quickSelectSearch(arr1, 0)
expect(res1[0]).toBe(10)
expect(res1[1]).toBe(20)

const arr2 = [20, 10]
const res2 = quickSelectSearch(arr2, 1)
expect(res2[0]).toBe(10)
expect(res2[1]).toBe(20)
})

it('asserts domain boundary with duplicate elements', () => {
const input = [3, 1, 3, 2, 3]
const k = 2
// Sorted: [1, 2, 3, 3, 3] -> element at index 2 is 3
const result = quickSelectSearch(input, k)

expect(result[2]).toBe(3)
for (let i = 0; i < 2; i++) {
expect(result[i]).toBeLessThanOrEqual(3)
}
for (let i = 3; i < result.length; i++) {
expect(result[i]).toBeGreaterThanOrEqual(3)
}
})
})

describe('Explicitly Unsupported Boundaries (Invalid side of contract, Line 15-16)', () => {
it('throws Error("Invalid arguments") when k equals array.length (just beyond upper boundary, Line 15)', () => {
const input = [10, 20, 30]
expect(() => quickSelectSearch(input, 3)).toThrow('Invalid arguments')
})

it('throws Error("Invalid arguments") when k exceeds array.length (Line 15)', () => {
const input = [10, 20, 30]
expect(() => quickSelectSearch(input, 5)).toThrow('Invalid arguments')
})

it('throws Error("Invalid arguments") when array is empty (array.length <= k, Line 15)', () => {
expect(() => quickSelectSearch([], 0)).toThrow('Invalid arguments')
})

it('throws Error("Invalid arguments") when array is null or undefined (!array, Line 15)', () => {
expect(() => quickSelectSearch(null, 0)).toThrow('Invalid arguments')
expect(() => quickSelectSearch(undefined, 0)).toThrow('Invalid arguments')
})
})
})
Loading