Finished version1 of search algorithm