Binary Search vs For loop

so I did this
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

console.time('findMe')

function findMe (target, start, end){

// if(start > end)
// return "Not Found" ;

const middle = Math.floor( (start + end) / 2);

if( arr[middle] === target )
return `found at index ${middle}`;

if( arr[middle] > target )
return findMe( target, start, middle-1 );

if( arr[middle] < target )
return findMe( target, middle+1, end);

}

console.log(findMe(98, 0, arr.length - 1))

console.timeEnd('findMe')



console.time('for')

for(let i=0; i<arr.length; i++){

if(arr[i] === 98)
console.log(`found at index ${i}`)
}

console.timeEnd('for')
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

console.time('findMe')

function findMe (target, start, end){

// if(start > end)
// return "Not Found" ;

const middle = Math.floor( (start + end) / 2);

if( arr[middle] === target )
return `found at index ${middle}`;

if( arr[middle] > target )
return findMe( target, start, middle-1 );

if( arr[middle] < target )
return findMe( target, middle+1, end);

}

console.log(findMe(98, 0, arr.length - 1))

console.timeEnd('findMe')



console.time('for')

for(let i=0; i<arr.length; i++){

if(arr[i] === 98)
console.log(`found at index ${i}`)
}

console.timeEnd('for')
and surprisingly the for loop did it in less time.. shouldn't binary search be faster?? 🤔 or am I doing it wrong??
3 Replies
13eck
13eck2y ago
Now do the same thing but with 10k elements and see which is faster. Then 100k. only 100 elements is a very small dataset and isn't what binary searches are optimized for No one algorithm is a silver bullet that will do everything. With only 100 elements it's taking more time to split and search than it is to find. But with a larger dataset the splitting time will make the search time that much faster. Big O is, like everything CS-related, a suggestion/guideline that doesn't take every situation into account. It's a "close enough".
glutonium
glutonium2y ago
hmm i see.. Yaa it makes sense tnx ❤️
13eck
13eck2y ago
It's similar to how for a very small dataset an array is more performant than a hashmap because iterating through a small number of elements is quicker than hashing the value to lookup in the map. But most of the time things like that are micro-optimizations that are done way after the project has launched and you've found a bottleneck and have tried, tested, and profiled the potential optimizations and made an informed decision
Want results from more Discord servers?
Add your server