1
- import java .util .Scanner ;
2
-
3
- /*
4
- * Java Program to implement binary search without using recursion
5
- */
6
- public class BinarySearch {
7
-
8
- public static void main (String [] args ) {
9
-
10
- Scanner commandReader = new Scanner (System .in );
11
- System .out .println ("Welcome to Java Program to perform
12
- binary search on int array" );
13
- System .out .println ("Enter total number of elements : " );
14
- int length = commandReader .nextInt ();
15
- int [] input = new int [length ];
16
-
17
- System .out .printf ("Enter %d integers %n" , length );
18
- for (int i = 0 ; i < length ; i ++) {
19
- input [i ] = commandReader .nextInt ();
1
+ import java .util .*;
2
+ import javax .lang .model .element .Element ;
3
+ import javax .lang .model .util .ElementScanner14 ;
4
+ import javax .swing .text .html .StyleSheet ;
5
+ class BinarySearch {
6
+ public static void main (String arg []){
7
+ Scanner sc = new Scanner (System .in );
8
+ System .out .println ("enter size of array:" );
9
+ int n =sc .nextInt ();
10
+ int []a =new int [n ];
11
+ int high =a .length -1 ;
12
+ int low =0 ;
13
+ for (int i =0 ;i <n ;i ++){
14
+ a [i ]=i +1 ;
20
15
}
21
-
22
- System .out .println ("Please enter number to be searched in array
23
- (sorted order)" );
24
- int key = commandReader .nextInt ();
25
-
26
- int index = performBinarySearch (input , key );
27
-
28
- if (index == -1 ) {
29
- System .out .printf ("Sorry, %d is not found in array %n" , key );
30
- } else {
31
- System .out .printf ("%d is found in array at index %d %n" , key ,
32
- index );
33
- }
34
-
35
- commandReader .close ();
36
-
16
+ System .out .println ("enter number to find in array between 1 - " +n +":" );
17
+ int key =sc .nextInt ();
18
+ long s =System .nanoTime ();
19
+ binary (a ,key ,low ,high );
20
+ long e =System .nanoTime ();
21
+ System .out .println ("total time is: " +(e -s ));
37
22
}
38
-
39
- /**
40
- * Java method to perform binary search. It accept an integer array and a
41
- * number and return the index of number in the array. If number doesn't
42
- * exists in array then it return -1
43
- *
44
- * @param input
45
- * @param number
46
- * @return index of given number in array or -1 if not found
47
- */
48
- public static int performBinarySearch (int [] input , int number ) {
49
- int low = 0 ;
50
- int high = input .length - 1 ;
51
-
52
- while (high >= low ) {
53
- int middle = (low + high ) / 2 ;
54
- if (input [middle ] == number ) {
55
- return middle ;
56
- } else if (input [middle ] < number ) {
57
- low = middle + 1 ;
58
- } else if (input [middle ] > number ) {
59
- high = middle - 1 ;
60
- }
61
- }
62
- return -1 ;
23
+ static void binary (int a [],int key ,int low ,int high ){
24
+ int mid = (high +low )/2 ;
25
+ int found =0 ;
26
+ if (low > high ){
27
+ System .out .println ("number is not found in array." );
28
+ }
29
+ else if (key ==a [mid ]){
30
+ System .out .println ("number is find at index: " +mid );
31
+ }
32
+ else if (key >a [mid ]){
33
+ binary (a ,key ,mid +1 ,high );
34
+ }
35
+ else {
36
+ binary (a ,key ,low ,mid -1 );
37
+ }
63
38
}
64
-
65
- }
39
+ }
0 commit comments