mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
* value is found or the interval is empty.
|
||||
*/
|
||||
|
||||
function binarySearchRecursive (arr, x, low = 0, high = arr.length - 1) {
|
||||
function binarySearchRecursive(arr, x, low = 0, high = arr.length - 1) {
|
||||
const mid = Math.floor(low + (high - low) / 2)
|
||||
|
||||
if (high >= low) {
|
||||
@@ -28,7 +28,7 @@ function binarySearchRecursive (arr, x, low = 0, high = arr.length - 1) {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) {
|
||||
function binarySearchIterative(arr, x, low = 0, high = arr.length - 1) {
|
||||
while (high >= low) {
|
||||
const mid = Math.floor(low + (high - low) / 2)
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
function binarySearch (arr, value, floor, ceiling) {
|
||||
function binarySearch(arr, value, floor, ceiling) {
|
||||
// Middle index
|
||||
const mid = Math.floor((floor + ceiling) / 2)
|
||||
|
||||
@@ -31,7 +31,7 @@ function binarySearch (arr, value, floor, ceiling) {
|
||||
}
|
||||
}
|
||||
|
||||
function exponentialSearch (arr, length, value) {
|
||||
function exponentialSearch(arr, length, value) {
|
||||
// If value is the first element of the array return this position
|
||||
if (arr[0] === value) {
|
||||
return 0
|
||||
|
||||
@@ -57,7 +57,7 @@ export const fibonacciSearch = (arr, x, n) => {
|
||||
fib1 = fib1 - fib2
|
||||
fib2 = fibK - fib1
|
||||
} else {
|
||||
// return index for found element
|
||||
// return index for found element
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
export function interpolationSearch (arr, key) {
|
||||
export function interpolationSearch(arr, key) {
|
||||
const length = arr.length - 1
|
||||
let low = 0
|
||||
let high = length
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/* The Jump Search algorithm allows to combine a linear search with a speed optimization.
|
||||
* This means that instead of going 1 by 1, we will increase the step of √n and increase that
|
||||
* step of √n which make the step getting bigger and bigger.
|
||||
* The asymptotic analysis of Jump Search is o(√n). Like the binary search, it needs to be sorted.
|
||||
* The advantage against binary search is that Jump Search traversed back only once.
|
||||
* This means that instead of going 1 by 1, we will increase the step of √n and increase that
|
||||
* step of √n which make the step getting bigger and bigger.
|
||||
* The asymptotic analysis of Jump Search is o(√n). Like the binary search, it needs to be sorted.
|
||||
* The advantage against binary search is that Jump Search traversed back only once.
|
||||
*/
|
||||
|
||||
const jumpSearch = (arr, value) => {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* for the target value until a match is found or until all the elements
|
||||
* have been searched.
|
||||
*/
|
||||
function SearchArray (searchNum, ar, output = v => console.log(v)) {
|
||||
function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
|
||||
const position = Search(ar, searchNum)
|
||||
if (position !== -1) {
|
||||
output('The element was found at ' + (position + 1))
|
||||
@@ -14,9 +14,11 @@ function SearchArray (searchNum, ar, output = v => console.log(v)) {
|
||||
}
|
||||
|
||||
// Search “theArray” for the specified “key” value
|
||||
function Search (theArray, key) {
|
||||
function Search(theArray, key) {
|
||||
for (let n = 0; n < theArray.length; n++) {
|
||||
if (theArray[n] === key) { return n }
|
||||
if (theArray[n] === key) {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
*
|
||||
* [Reference](http://en.wikipedia.org/wiki/Quickselect)
|
||||
*/
|
||||
export function quickSelectSearch (array, k) {
|
||||
export function quickSelectSearch(array, k) {
|
||||
if (!array || array.length <= k) {
|
||||
throw new Error('Invalid arguments')
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
/**
|
||||
* Sliding Window:
|
||||
* This pattern involve creating a window which can either be
|
||||
* an array or numbers from one position to another.
|
||||
*
|
||||
* Depending on a certain condition, the window either increases
|
||||
* or closes (and a new window is created).
|
||||
*
|
||||
* Very useful for keeping track of a subset of data in an
|
||||
* array/string etc.
|
||||
*
|
||||
* Time Complexity: Best - O(n);
|
||||
*
|
||||
* Examples:
|
||||
* maxSubarraySum([1,2,5,2,8,1,5],2) // returns 10
|
||||
* maxSubarraySum([1,2,5,2,8,1,5],15) // returns null
|
||||
* maxSubarraySum([5,2,6,9],3) // returns 17
|
||||
* Sliding Window:
|
||||
* This pattern involve creating a window which can either be
|
||||
* an array or numbers from one position to another.
|
||||
*
|
||||
* Depending on a certain condition, the window either increases
|
||||
* or closes (and a new window is created).
|
||||
*
|
||||
* Very useful for keeping track of a subset of data in an
|
||||
* array/string etc.
|
||||
*
|
||||
* Time Complexity: Best - O(n);
|
||||
*
|
||||
* Examples:
|
||||
* maxSubarraySum([1,2,5,2,8,1,5],2) // returns 10
|
||||
* maxSubarraySum([1,2,5,2,8,1,5],15) // returns null
|
||||
* maxSubarraySum([5,2,6,9],3) // returns 17
|
||||
* @param {[Int]} arr - An array of integers on which we will perform the test.
|
||||
* @param {Int} num - An integer that displays the size of the window you want to check.
|
||||
* @returns {Int / Null} - Returns a total of N consecutive numbers or null
|
||||
*/
|
||||
|
||||
function slidingWindow (arr, num) {
|
||||
function slidingWindow(arr, num) {
|
||||
// Edge Case:
|
||||
// If the length of the array shorter than the window size (num) return null.
|
||||
if (arr.length < num) return null
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* String Search
|
||||
*/
|
||||
|
||||
function makeTable (str) {
|
||||
function makeTable(str) {
|
||||
// create a table of size equal to the length of `str`
|
||||
// table[i] will store the prefix of the longest prefix of the substring str[0..i]
|
||||
const table = new Array(str.length)
|
||||
@@ -35,7 +35,7 @@ function makeTable (str) {
|
||||
}
|
||||
|
||||
// Find all the words that matches in a given string `str`
|
||||
export function stringSearch (str, word) {
|
||||
export function stringSearch(str, word) {
|
||||
// find the prefix table in O(n)
|
||||
const prefixes = makeTable(word)
|
||||
const matches = []
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Reference: https://www.geeksforgeeks.org/ternary-search/
|
||||
*/
|
||||
|
||||
function ternarySearchRecursive (arr, key, low = 0, high = arr.length - 1) {
|
||||
function ternarySearchRecursive(arr, key, low = 0, high = arr.length - 1) {
|
||||
if (high >= low) {
|
||||
// find the mid1 and mid2
|
||||
const mid1 = Math.floor(low + (high - low) / 3)
|
||||
@@ -47,7 +47,7 @@ function ternarySearchRecursive (arr, key, low = 0, high = arr.length - 1) {
|
||||
}
|
||||
}
|
||||
|
||||
function ternarySearchIterative (arr, key, low = 0, high = arr.length - 1) {
|
||||
function ternarySearchIterative(arr, key, low = 0, high = arr.length - 1) {
|
||||
while (high >= low) {
|
||||
// find the mid1 and mid2
|
||||
const mid1 = Math.floor(low + (high - low) / 3)
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
*
|
||||
* you can learn more on disjoint-set / union–find data structure at https://en.wikipedia.org/wiki/Disjoint-set_data_structure
|
||||
*/
|
||||
function UnionFind (n, key) {
|
||||
function UnionFind(n, key) {
|
||||
if (!(this instanceof UnionFind)) return new UnionFind(n)
|
||||
if (key && typeof key !== 'function') {
|
||||
throw new Error('key has to be a function or else left undefined')
|
||||
@@ -22,7 +22,11 @@ function UnionFind (n, key) {
|
||||
let cnt, length
|
||||
// init Union Find with number of distinct groups. Each group will be referred to as index of the array of size 'size' starting at 0.
|
||||
// Provide an optional key function that maps these indices. I.e. for the groups starting with 1 provide function(a){return a-1;}. The default value is function(a){return a;}.
|
||||
key = key || function (a) { return a }
|
||||
key =
|
||||
key ||
|
||||
function (a) {
|
||||
return a
|
||||
}
|
||||
cnt = length = n
|
||||
const id = new Array(n)
|
||||
const sz = new Array(n)
|
||||
@@ -63,16 +67,21 @@ function UnionFind (n, key) {
|
||||
const j = this.find(q)
|
||||
if (i === j) return
|
||||
if (sz[i] < sz[j]) {
|
||||
id[i] = j; sz[j] += sz[i]
|
||||
id[i] = j
|
||||
sz[j] += sz[i]
|
||||
} else {
|
||||
id[j] = i; sz[i] += sz[j]
|
||||
id[j] = i
|
||||
sz[i] += sz[j]
|
||||
}
|
||||
cnt--
|
||||
}
|
||||
function ensureIndexWithinBounds (args) {
|
||||
function ensureIndexWithinBounds(args) {
|
||||
for (let i = arguments.length - 1; i >= 0; i--) {
|
||||
const p = arguments[i]
|
||||
if (p >= length) throw new Error('Index out of bounds. The maximum index can be length-1')
|
||||
if (p >= length)
|
||||
throw new Error(
|
||||
'Index out of bounds. The maximum index can be length-1'
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { exponentialSearch } from '../ExponentialSearch'
|
||||
|
||||
test('The Exponential Search of the Array [2, 3, 4, 10, 40, 65, 78, 100] is 6 where the value = 78', () => {
|
||||
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
|
||||
const value = 78
|
||||
const result = exponentialSearch(arr, arr.length, value)
|
||||
expect(result).toEqual(6)
|
||||
})
|
||||
|
||||
test('The Exponential Search of the Array [2, 3, 4, 10, 40, 65, 78, 100] is -1 where the value = 178', () => {
|
||||
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
|
||||
const value = 178
|
||||
const result = exponentialSearch(arr, arr.length, value)
|
||||
expect(result).toEqual(-1)
|
||||
})
|
||||
import { exponentialSearch } from '../ExponentialSearch'
|
||||
|
||||
test('The Exponential Search of the Array [2, 3, 4, 10, 40, 65, 78, 100] is 6 where the value = 78', () => {
|
||||
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
|
||||
const value = 78
|
||||
const result = exponentialSearch(arr, arr.length, value)
|
||||
expect(result).toEqual(6)
|
||||
})
|
||||
|
||||
test('The Exponential Search of the Array [2, 3, 4, 10, 40, 65, 78, 100] is -1 where the value = 178', () => {
|
||||
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
|
||||
const value = 178
|
||||
const result = exponentialSearch(arr, arr.length, value)
|
||||
expect(result).toEqual(-1)
|
||||
})
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import { fibonacciSearch } from '../FibonacciSearch'
|
||||
|
||||
test('fibonacciSearch([10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], 90, arr.length) => 9', () => {
|
||||
const arr = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
|
||||
const target = 90
|
||||
const res = fibonacciSearch(arr, target, arr.length)
|
||||
expect(res).toEqual(9)
|
||||
})
|
||||
|
||||
test('fibonacciSearch([1, 11, 55, 56, 78, 82, 104], 104, arr.length) => 6', () => {
|
||||
const arr = [1, 11, 55, 56, 78, 82, 104]
|
||||
const target = 104
|
||||
const res = fibonacciSearch(arr, target, arr.length)
|
||||
expect(res).toEqual(6)
|
||||
})
|
||||
|
||||
test('fibonacciSearch([40, 45, 50, 80, 82, 85, 90, 100]. 190, arr.length) => -1', () => {
|
||||
const arr = [40, 45, 50, 80, 82, 85, 90, 100]
|
||||
const target = 190
|
||||
const res = fibonacciSearch(arr, target, arr.length)
|
||||
expect(res).toEqual(-1)
|
||||
})
|
||||
import { fibonacciSearch } from '../FibonacciSearch'
|
||||
|
||||
test('fibonacciSearch([10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], 90, arr.length) => 9', () => {
|
||||
const arr = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
|
||||
const target = 90
|
||||
const res = fibonacciSearch(arr, target, arr.length)
|
||||
expect(res).toEqual(9)
|
||||
})
|
||||
|
||||
test('fibonacciSearch([1, 11, 55, 56, 78, 82, 104], 104, arr.length) => 6', () => {
|
||||
const arr = [1, 11, 55, 56, 78, 82, 104]
|
||||
const target = 104
|
||||
const res = fibonacciSearch(arr, target, arr.length)
|
||||
expect(res).toEqual(6)
|
||||
})
|
||||
|
||||
test('fibonacciSearch([40, 45, 50, 80, 82, 85, 90, 100]. 190, arr.length) => -1', () => {
|
||||
const arr = [40, 45, 50, 80, 82, 85, 90, 100]
|
||||
const target = 190
|
||||
const res = fibonacciSearch(arr, target, arr.length)
|
||||
expect(res).toEqual(-1)
|
||||
})
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { interpolationSearch } from '../InterpolationSearch'
|
||||
|
||||
test('interpolationSearch([2, 6, 8, 14, 122, 169], 144) => -1', () => {
|
||||
const array = [2, 6, 8, 14, 122, 169]
|
||||
const key = 144
|
||||
const res = interpolationSearch(array, key)
|
||||
expect(res).toEqual(-1)
|
||||
})
|
||||
|
||||
test('interpolationSearch([2, 6, 8, 14, 122, 169], 122) => 4', () => {
|
||||
const array = [2, 6, 8, 14, 122, 169]
|
||||
const key = 122
|
||||
const res = interpolationSearch(array, key)
|
||||
expect(res).toEqual(4)
|
||||
})
|
||||
import { interpolationSearch } from '../InterpolationSearch'
|
||||
|
||||
test('interpolationSearch([2, 6, 8, 14, 122, 169], 144) => -1', () => {
|
||||
const array = [2, 6, 8, 14, 122, 169]
|
||||
const key = 144
|
||||
const res = interpolationSearch(array, key)
|
||||
expect(res).toEqual(-1)
|
||||
})
|
||||
|
||||
test('interpolationSearch([2, 6, 8, 14, 122, 169], 122) => 4', () => {
|
||||
const array = [2, 6, 8, 14, 122, 169]
|
||||
const key = 122
|
||||
const res = interpolationSearch(array, key)
|
||||
expect(res).toEqual(4)
|
||||
})
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { ternarySearchRecursive, ternarySearchIterative } from '../TernarySearch'
|
||||
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)
|
||||
@@ -16,21 +19,33 @@ test('should return the index of a number in an array of numbers:', () => {
|
||||
})
|
||||
|
||||
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)
|
||||
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')
|
||||
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')
|
||||
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')
|
||||
const indexNumber = ternarySearchRecursive(
|
||||
['Ali', 'Cathrynli', 'Josuke', 'Thomas'],
|
||||
'Angela'
|
||||
)
|
||||
expect(indexNumber).toBe(-1)
|
||||
})
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { jumpSearch } from '../JumpSearch'
|
||||
|
||||
test('jumpSearch([0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90], 77) => 10', () => {
|
||||
const arr = [0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90]
|
||||
const res = jumpSearch(arr, 77)
|
||||
expect(res).toEqual(10)
|
||||
})
|
||||
|
||||
test('jumpSearch([11, 12, 15, 65, 78, 90], 4) => -1', () => {
|
||||
const arr = [11, 12, 15, 65, 78, 90]
|
||||
const res = jumpSearch(arr, 4)
|
||||
expect(res).toEqual(-1)
|
||||
})
|
||||
|
||||
test('jumpSearch([11, 12, 15, 65, 78, 90], 11) => 0', () => {
|
||||
const arr = [11, 12, 15, 65, 78, 90]
|
||||
const res = jumpSearch(arr, 11)
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
import { jumpSearch } from '../JumpSearch'
|
||||
|
||||
test('jumpSearch([0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90], 77) => 10', () => {
|
||||
const arr = [0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90]
|
||||
const res = jumpSearch(arr, 77)
|
||||
expect(res).toEqual(10)
|
||||
})
|
||||
|
||||
test('jumpSearch([11, 12, 15, 65, 78, 90], 4) => -1', () => {
|
||||
const arr = [11, 12, 15, 65, 78, 90]
|
||||
const res = jumpSearch(arr, 4)
|
||||
expect(res).toEqual(-1)
|
||||
})
|
||||
|
||||
test('jumpSearch([11, 12, 15, 65, 78, 90], 11) => 0', () => {
|
||||
const arr = [11, 12, 15, 65, 78, 90]
|
||||
const res = jumpSearch(arr, 11)
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user