diff --git a/src/algorithms/math/MaxRecursion/MaxRecursion.js b/src/algorithms/math/MaxRecursion/MaxRecursion.js new file mode 100644 index 0000000000..5110246854 --- /dev/null +++ b/src/algorithms/math/MaxRecursion/MaxRecursion.js @@ -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 } diff --git a/src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js b/src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js new file mode 100644 index 0000000000..0a047e0ae0 --- /dev/null +++ b/src/algorithms/math/MaxRecursion/__test__/MaxRecursion.test.js @@ -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) + }) +}) diff --git a/src/algorithms/math/SelectMedian/SelectMedian.js b/src/algorithms/math/SelectMedian/SelectMedian.js new file mode 100644 index 0000000000..6de324b549 --- /dev/null +++ b/src/algorithms/math/SelectMedian/SelectMedian.js @@ -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 } diff --git a/src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js b/src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js new file mode 100644 index 0000000000..53a54aaf4d --- /dev/null +++ b/src/algorithms/math/SelectMedian/__test__/SelectMedian.test.js @@ -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) +})