improved algorithm on search