#100DaysOfCode Day 9
As most of you know, algorithms can get very hard and is quite a large topic, so I took it upon myself today outside of class to learn more about
#algorithms
There are tons of known algorithms. This one in particular is called the
#binarysearch.
- In a nutshell, we have two parameters: x and y.
- x takes in the sorted numbers array, while y is the number we want to find.
- s is the start of the search point, e is the end of the search point, (range) and m is the midpoint.
- The algorithm begins the search from the midpoint (m = Math.floor((s + e) / 2);) and searches through the left or right portions of the array to find a match according to the logic from where the search should begin.
- If no match is not found, it will return false.
I am using jest to test the algorithm as followed:
test('binarySearch should return true', () => {
let testArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]
expect(b(testArray, 7)).toEqual(true)
})
test('binarySearch should return false because 5643 is not in the array', () => {
let testArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]
expect(b(testArray, 5643)).toEqual(false)
})
Additionally, I have started watching a YouTube algorithms series and looking forward to dive deeper into complex algorithms.
#100Devs
#computerscience
#javascript