100 algos in 100 days (Day 16)
Paolo Ventura
Posted on September 30, 2021
This was a tuesday dedicated to Binary search.
I watched videos and (re)learnt the common iterative implementation of this.
export const iterativeFunction = function (arr, x) {
let start = 0, end = arr.length - 1;
// Iterate while start not meets end
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (arr[mid] === x) return true;
// Else look in left or right half accordingly
if (arr[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return false;
}
Then did the practice question on find the rotation point of a partially sorted dictionary.
Learning points being:
- Data only needs to be partially sorted to use binary search
- If it is not sorted numbers you might have to look for a pattern or compare other indexes that you know. For example, the first and last.
π πͺ π
π©
Paolo Ventura
Posted on September 30, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
privacy Caught in the Crunch My Journey from Snacks to 2 Million Exposed Users Privacy
November 30, 2024