binsearch replace for loop