Skip to content

Quick select tests #697

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 8 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 5 additions & 8 deletions Data-Structures/Array/QuickSelect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* QuickSelect is an algorithm to find the kth smallest number
* [QuickSelect](https://www.geeksforgeeks.org/quickselect-algorithm/) is an algorithm to find the kth smallest number
*
* Notes:
* -QuickSelect is related to QuickSort, thus has optimal best and average
Expand All @@ -13,18 +13,13 @@

function QuickSelect (items, kth) { // eslint-disable-line no-unused-vars
if (kth < 1 || kth > items.length) {
return 'Index Out of Bound'
throw Error('Index Out of Bound')
}

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

function RandomizedSelect (
items,
left,
right,
i
) {
function RandomizedSelect (items, left, right, i) {
if (left === right) return items[left]

const pivotIndex = RandomizedPartition(items, left, right)
Expand Down Expand Up @@ -76,3 +71,5 @@ function Swap (arr, x, y) {
// "Index Out of Bound"
// > QuickSelect([1, 4, 2, -2, 4, 5], 7)
// "Index Out of Bound"

export { QuickSelect }
49 changes: 49 additions & 0 deletions Data-Structures/Array/QuickSelect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { QuickSelect } from './QuickSelect'

describe('QuickSelect tests', () => {
it('should return the only element of a list of length 1', () => {
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
expect(QuickSelect([100], 1)).toEqual(100)
expect(QuickSelect([-23], 1)).toEqual(-23)
expect(QuickSelect([2007.102], 1)).toEqual(2007.102)
expect(QuickSelect([0.9], 1)).toEqual(0.9)
expect(QuickSelect([-0.075], 1)).toEqual(-0.075)
expect(QuickSelect([0], 1)).toEqual(0)
expect(QuickSelect([1], 1)).toEqual(1)
})

it('should throw an Error when k is greater than the length of the list', () => {
expect(() => QuickSelect([100, 2], 5)).toThrow('Index Out of Bound')
})

it('should throw an Error when k is less than 1', () => {
expect(() => QuickSelect([100, 2], 0)).toThrow('Index Out of Bound')
expect(() => QuickSelect([100, 2], -1)).toThrow('Index Out of Bound')
})

describe('varieties of list composition', () => {
it('should return the kth smallest element of a list that is in increasing order', () => {
expect(QuickSelect([10, 22, 33, 44, 55], 1)).toEqual(10)
expect(QuickSelect([10, 22, 33, 44, 55], 2)).toEqual(22)
expect(QuickSelect([10, 22, 33, 44, 55], 3)).toEqual(33)
expect(QuickSelect([10, 22, 33, 44, 55], 4)).toEqual(44)
expect(QuickSelect([10, 22, 33, 44, 55], 5)).toEqual(55)
})

it('should return the kth smallest element of an input list that is in decreasing order', () => {
expect(QuickSelect([82, 33.12, 4.0, 1], 1)).toEqual(1)
expect(QuickSelect([82, 33.12, 4.0, 1], 2)).toEqual(4.0)
expect(QuickSelect([82, 33.12, 4.0, 1], 2)).toEqual(4)
expect(QuickSelect([82, 33.12, 4.0, 1], 3)).toEqual(33.12)
expect(QuickSelect([82, 33.12, 4.0, 1], 4)).toEqual(82)
})

it('should return the kth smallest element of an input list that is no particular order', () => {
expect(QuickSelect([123, 14231, -10, 0, 15], 3)).toEqual(15)
expect(QuickSelect([0, 15, 123, 14231, -10], 3)).toEqual(15)
expect(QuickSelect([-10, 15, 123, 14231, 0], 3)).toEqual(15)
expect(QuickSelect([14231, 0, 15, 123, -10], 3)).toEqual(15)
expect(QuickSelect([14231, 0, 15, -10, 123], 3)).toEqual(15)
})
})
})