Implemented the search algorithm