Wednesday, October 7, 2015

Binary search Brute Force


public class BinarySearchBruteForce {

static int[] elements = {1,4,6,7,9,10,15,20,56,79,90,100};


static boolean found=false;

public static void main(String[] args) {

int search=33;int index=-1;

int start=0;

int end=elements.length-1;

while(!found && start <= end){

int mid=(start+end)/2;

if(elements[mid]==search){

found=true;

index=mid;

}else if(search<elements[mid]){

end=mid-1;

}else{

start=mid+1;

}

}

if(index==-1 || !found){

System.out.println("element is not available in list");

}else{

System.out.println(""+ search+ " element is available in list at position " + 
index);

}
}

}

No comments: