Java:
public class İkiliArama {
//Eşitliği Sağlamayan İkili Arama
public static int binarySearch(int target,int[] dizi){
int bottom = 0;
int top = dizi.length-1;
int middle;
while(bottom < top){
middle = (bottom+top)/2;
if(dizi[middle] < target){
bottom = middle+1;
}
else
top = middle;
}
if(top < bottom){
return -1;
}
else{
if(dizi[bottom] == target)
return bottom;
return -1;
}
}
//Eşitliği Sağlayan İkli Arama
public static int binarySearch2(int target,int[] dizi){
int bottom = 0;
int top = dizi.length-1;
int middle = 0;
boolean found = false;
while(found == false && bottom <= top){
middle = (bottom+top)/2;
if(dizi[middle] == target){
found = true;
}
else if(dizi[middle] < target){
bottom = middle+1;
}
else
top = middle-1;
}
if(found)
return middle;
else
return -1;
}
//Eşitliği Sağlayan Recursive İkili Arama
public static int binarySearch3(int target,int dizi[],int bottom,int top){
int middle;
if(bottom < top){
middle = (bottom+top)/2;
if(dizi[middle] == target)
return middle;
else if(dizi[middle] < target){
return binarySearch3(target, dizi, middle+1, top);
}
else
return binarySearch3(target, dizi, bottom, middle-1);
}
else
return -1;
}
}