FAQ Database Discussion Community


Optimizing a large if-else branch with binary search

c,optimization,binary-search,linear-search
So there is an if-else branch in my program with about 30 if-else statements. This part runs more than 100 times per second, so I saw it as an opportunity to optimize, and made it do binary search with a function pointer array (practically a balanced tree map) instead of...

Linear Search not completing successfully

ruby,arrays,loops,linear-search
Performing a basic linear search with a loop over an array, and not returning the expected value. Given: students = ["Alex", "Kyle", "Libby", "Monkey Boy"] I'm trying to do a basic linear search to see if the name "Monkey Boy" exists, and return it's index. def linear_search(array, name) i =...

How many elements need to be checked on an average in linear search?

algorithm,linear-search
Question: Consider linear search. How many elements of the input sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How do I solve this? Do I need to take the case where the...

Binary Search vs. Linear Search (data structures & algorithms)

algorithm,data-structures,computer-science,binary-search,linear-search
Trying to wrap my head around some basic and common algorithms.. my current understanding of the question is in bold. ( 1 ) Assuming we have a sorted array with n items: at most how many times will a binary search compare elements? I keep seeing ' 0(log(n)) ' pop...