Optimize a complex search algorithm to run within logarithmic time complexity, ensuring all edge cases are handled.