write a program to find the ascending no of the array
Answers
Answered by
1
import java.util.*;
class BinarySort{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int num[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int first = 0;
int last = num.length-1;
int mid = (first+last) / 2;
System.out.println("Enter the number to be searched");
int n = in.nextInt();
while(first<=last){
if(num[mid] > n){
last = mid - 1;
} else if(num[mid] == n){
System.out.println("Found at " + (mid + 1));
break;
} else{
first = mid + 1;
}
mid = (first + last) / 2;
}
if(first > last){
System.out.println("Not Found");
}
}
}
Hope this helps,
ira.
Similar questions