1.

Apply Binary search and search for element 18. 11, 12, 13, 14, 15, 16, 17, 18, 19.​

Answer»

Program:-import java.util.*;public class Main{ public static void main(String ARGS[]) { Scanner in=new Scanner(System.in); int a[]={11,12,13,14,15,16,17,18,19}; int lb=0,ub=8,p=0,ns,f=0; System.out.println("Enter the number to be searched"); ns=in.nextInt(); while(lb<=ub) { p=(lb+ub)/2; if(a[p]ns) ub=p-1; if(a[p]==ns) { f=1; break; } } if(f==1) System.out.println("Search SUCCESSFUL the number "+ns+" is present"); else System.out.println("Search unsuccessful the number "+ns+" is not present"); }}Note:-Binary Search is COMPARATIVELY faster than Linear search.Only disadvantage of Binary Search is that it only works for sorted arrays.



Discussion

No Comment Found