Created
February 19, 2017 12:16
-
-
Save ammardev/7049b872d9d2536d3043e5d1542003de to your computer and use it in GitHub Desktop.
Not Complete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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