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
30 changes: 30 additions & 0 deletions src/algorithms/math/MaxRecursion/MaxRecursion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* @function maxRecursion
* @description This algorithm will find the maximum value of a array of numbers.
*
* @param {Integer[]} arr Array of numbers
* @param {Integer} left Index of the first element
* @param {Integer} right Index of the last element
*
* @return {Integer} Maximum value of the array
*/

function maxRecursion(arr, left, right) {
const len = arr.length
if (len === 0 || !arr) {
return undefined
}
if (left >= len || left < -len || right >= len || right < -len) {
throw new Error('Index out of range')
}
if (left === right) {
return arr[left]
}
// n >> m is equivalent to floor(n / pow(2, m)), floor(n / 2) in this case, which is the mid index
const mid = (left + right) >> 1
const leftMax = maxRecursion(arr, left, mid)
const rightMax = maxRecursion(arr, mid + 1, right)
// Return the maximum
return Math.max(leftMax, rightMax)
}
export { maxRecursion }
39 changes: 39 additions & 0 deletions src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { maxRecursion } from '../MaxRecursion'

describe('Test maxRecursion function', () => {
const positiveArray = [1, 2, 4, 5]

const negativeArray = [-1, -2, -4, -5]

const positiveAndNegativeArray = [1, 2, 4, 5, -1, -2, -4, -5]

const zeroArray = [0, 0, 0, 0]

const emptyArray = []

it('Testing with positive arrays', () => {
expect(maxRecursion(positiveArray, 0, positiveArray.length - 1)).toBe(5)
})

it('Testing with negative arrays', () => {
expect(maxRecursion(negativeArray, 0, negativeArray.length - 1)).toBe(-1)
})

it('Testing with positive and negative arrays', () => {
expect(
maxRecursion(
positiveAndNegativeArray,
0,
positiveAndNegativeArray.length - 1
)
).toBe(5)
})

it('Testing with zero arrays', () => {
expect(maxRecursion(zeroArray, 0, zeroArray.length - 1)).toBe(0)
})

it('Testing with empty arrays', () => {
expect(maxRecursion(emptyArray, 0, emptyArray.length - 1)).toBe(undefined)
})
})
29 changes: 29 additions & 0 deletions src/algorithms/math/SelectMedian/SelectMedian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @function selectMedian
* @description This algorithm will find the median of the array
*
* @param {Integer[]}
*
* @return {median}
*/

const selectMedian = (sourceArrayOfNumbers) => {
let numbers = [...sourceArrayOfNumbers]
let median = 0
const numLength = numbers.length
numbers = numbers.sort(sortNumbers)

if (numLength % 2 === 0) {
median = (numbers[numLength / 2 - 1] + numbers[numLength / 2]) / 2
} else {
median = numbers[(numLength - 1) / 2]
}

return median
}

const sortNumbers = (num1, num2) => {
return num1 - num2
}

export { selectMedian }
21 changes: 21 additions & 0 deletions src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { selectMedian } from '../SelectMedian'

test('should return the median of an array of numbers:', () => {
const medianValue = selectMedian([1, 3, 6, 4, 5])
expect(medianValue).toBe(4)
})

test('should return the median of an array of numbers:', () => {
const medianValue = selectMedian([8, 9, 1, 3, 5, 10, 11])
expect(medianValue).toBe(8)
})

test('should return the median of an array of numbers:', () => {
const medianValue = selectMedian([15, 18, 3, 9, 13, 5])
expect(medianValue).toBe(11)
})

test('should return the median of an array of numbers:', () => {
const medianValue = selectMedian([1, 3, 3, 4, 6, 8])
expect(medianValue).toBe(3.5)
})