Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Modified Binary Search
- //Darren Mock
- import java.util.*;
- public class BinaryS{
- public static void main(String [] args){
- int[] A;
- A[0] = -14;
- A[1] = -10;
- A[2] = 2;
- A[3] = 108;
- A[4] = 108;
- A[5] = 243;
- A[6] = 285;
- A[7] = 285;
- A[8] = 285;
- A[9] = 401;
- }
- int binary(int[] A, int index){
- int index = A.length/2 - 1;
- for (i = 0; i < Math.log(A.length + 1)/Math.log(2); i++) {
- if(A[index] > key){
- index = index - A.length/(Math.pow(2,(i + 1)));
- }
- else if(A[index] < key){
- index = index + A.length/(Math.pow(2,(i + 1)));
- }
- else if (A[index] == key){
- if (index != 0 && A[index - 1] == key){
- index = index - 1;
- }
- return index;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement