-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
64 lines (54 loc) · 1.41 KB
/
BinarySearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*Code Binary Search
You have been given a sorted(in ascending order) integer array/list(ARR) of size N and an element X. Write a function to search this element in the given input array/list using 'Binary Search'. Return the index of the element in the input array/list. In case the element is not present in the array/list, then return -1.*/
package PracticeMore;
import java.util.Scanner;
public class BinarySearch
{
public static int binarySearch(int[]arr, int x)
{
int min = 0;
int max = arr.length-1;
for(int i=0;i<arr.length;i++)
{
int mid = (min+max)/2;
if(arr[mid]<x)
{
min= mid+1;
}
else if(arr[mid]>x)
{
max= mid-1;
}
else
{
return mid;
}
}
return -1;
}
public static void printArray(int arr[])
{
for(int i= 0; i<arr.length; i++)
{
System.out.print(arr[i]+ ", ");
}
System.out.println();
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array : ");
int size = sc.nextInt();
System.out.print("Enter the number for search : ");
int number = sc.nextInt();
int[] input_arr = new int[size];
for(int i=0; i<size; i++)
{
System.out.print("Enter the element at "+ i + "th term : ");
input_arr[i] = sc.nextInt();
}
printArray(input_arr);
int index = binarySearch(input_arr, number);
System.out.print("Index of given number in array : "+index);
}
}