Added TernarySearch Function

This commit is contained in:
josuke00
2021-07-24 22:48:18 +08:00
parent 38c5cca036
commit 0182e31f74
2 changed files with 140 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import { ternarySearchRecursive, ternarySearchIterative } from '../TernarySearch'
test('should return the index of a number in an array of numbers:', () => {
const indexNumber = ternarySearchRecursive([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)
expect(indexNumber).toBe(2)
})
test('should return the index of a number in an array of numbers:', () => {
const indexNumber = ternarySearchIterative([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 8)
expect(indexNumber).toBe(7)
})
test('should return the index of a number in an array of numbers:', () => {
const indexNumber = ternarySearchRecursive([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0)
expect(indexNumber).toBe(-1)
})
test('should return the index of a number in an array of numbers:', () => {
const indexNumber = ternarySearchIterative([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 12)
expect(indexNumber).toBe(-1)
})
test('should return the index of a string in an array of strings:', () => {
const indexNumber = ternarySearchRecursive(['Ali', 'Cathrynli', 'Josuke', 'Thomas'], 'Cathrynli')
expect(indexNumber).toBe(1)
})
test('should return the index of a string in an array of strings:', () => {
const indexNumber = ternarySearchRecursive(['Ali', 'Cathrynli', 'Josuke', 'Thomas'], 'Josuke')
expect(indexNumber).toBe(2)
})
test('should return the index of a string in an array of strings:', () => {
const indexNumber = ternarySearchRecursive(['Ali', 'Cathrynli', 'Josuke', 'Thomas'], 'Angela')
expect(indexNumber).toBe(-1)
})