Skip to content

Instantly share code, notes, and snippets.

@ammardev
Created February 19, 2017 12:16
Show Gist options
  • Save ammardev/7049b872d9d2536d3043e5d1542003de to your computer and use it in GitHub Desktop.
Save ammardev/7049b872d9d2536d3043e5d1542003de to your computer and use it in GitHub Desktop.
Not Complete
public class Search {
public void binarySearch(int find, List<Integer> ds){ // Use when the data is sorted only
int first = 0, mid, last=ds.size()-1;
int count = 0;
while (true){
count++;
mid = first + (last - first)/2;
if (find > ds.get(mid))
first = mid+1;
else if (find < ds.get(mid))
last = mid-1;
else {
System.out.println("Found in : " + mid);
System.out.println("Found after " + count + " step");
break;
}
if (first > last){
System.out.println("Not found");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment