import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int [] Numbers={-10,0,90,70,40,50,20,30,80,60,100};
System.out.print("Enter an integer number that u want to search between 0 to 100");
int searchItem=input.nextInt();
int first =0;
int last= Numbers.length - 1 ;
boolean found=false;
int middle;
while(first<= last && !found){
middle=(first + last)/2;
if (searchItem==Numbers[middle] ){
found=true;
System.out.println("The number is in Numbers[] array index" + middle + ".");
}else if (searchItem < Numbers[middle]){
last=middle-1;
} else {
first=middle + 1;}
}
}
}
class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int [] Numbers={-10,0,90,70,40,50,20,30,80,60,100};
System.out.print("Enter an integer number that u want to search between 0 to 100");
int searchItem=input.nextInt();
int first =0;
int last= Numbers.length - 1 ;
boolean found=false;
int middle;
while(first<= last && !found){
middle=(first + last)/2;
if (searchItem==Numbers[middle] ){
found=true;
System.out.println("The number is in Numbers[] array index" + middle + ".");
}else if (searchItem < Numbers[middle]){
last=middle-1;
} else {
first=middle + 1;}
}
}
}
Комментариев нет:
Отправить комментарий