Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
binary search
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.Scanner; import java.lang.*; class Rextester { public static void main(String args[]) { Scanner in=new Scanner(System.in); int a[]={725,1936,1,24,34,2,54,67,3,98,26,07,65,46}; System.out.println("enter the element to be search"); int n=in.nextInt(); for(int i=0;i<a.length;i++){ for(int j=0;j<a.length-i-1;j++){ if(a[j]>a[j+1]){ int t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(int k=0;k<a.length;k++) System.out.print(a[k]+"\t"); System.out.println(); int mid=a.length/2; if(n<a[mid]) { for(int i=0;i<mid;i++) if(n==a[i]) System.out.println("the position of "+n+"="+i); } else{ for(int i=mid;i<a.length;i++) if(n==a[i]) System.out.println("the position of "+n+"="+i); } } }
run
|
edit
|
history
|
help
0
1a
Day 2
ChangePI
Java Class and Object
Armstrong no
Problem: binary
hextoepeen
document
BiseccionJava
Meena