public class ArryToString2 {
public static void main(String[] args) {
int arr[]={23,45,56,67,78,90};
int key=searchSort(arr,57);
System.out.println(key);
}
public static int searchSort(int arr[],int key) {
int min=0,max=arr.length-1;
int mid=0;
mid=(min+max)/2;
//for (int i = 0; i < arr.length; i++) {
while(arr[mid]!=key){
if(key>arr[mid]){
min=mid+1;
}
else if(key<arr[max]){
max=mid-1;
}
mid=(min+max)/2;
if(min>max){
return -1;
}
}
return mid;
}
}
|
|