-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_iterative.java
More file actions
68 lines (54 loc) · 2.27 KB
/
Copy pathbinary_search_iterative.java
File metadata and controls
68 lines (54 loc) · 2.27 KB
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
65
66
67
68
//Implement binary search without recursion
import java.util.Scanner;
//Class contains the array and multiple elements searching logic
class BinaryLoop{
int[] arr; //instance variable holds the value
int n; //size of an array
//Method to read the size and elements from the user
//Input array(must be sorted)
void inputArray(Scanner sc){
System.out.println("Enter the size of the array: ");
n = sc.nextInt(); //Read the size of an array
arr = new int[n]; //Initialize an array
System.out.println("Enter " + n + " elements");
for(int i = 0 ; i<n ; i++){
arr[i] = sc.nextInt(); //Read the array elements
}
}
//Iterative method to perform binary search on an sorted array
int binarySearch(int key){
int low = 0 , high = n-1; //initialize low and high pointers
//Loop until the search space is valid
if(low<=high){
int mid = (low+high) / 2; //Find the middle index
if(arr[mid] == key){ //Check if mid element is the key
return mid; //key found, return index
}
else if(arr[mid] < key){
low = mid + 1; //key is in the right half
}
else{
high = mid - 1; //key is in the left half
}
}
return -1; //key not found in an array
}
}
public class binary_search_iterative{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Scanner object to read input
BinaryLoop obj = new BinaryLoop(); //create an object for BinaryLoop class
obj.inputArray(sc); //Read array elements from the user
System.out.println("Enter the element to search: ");
int key = sc.nextInt(); //Enter the element to search as key
int result = obj.binarySearch(key); //store the key index if found
//Display result
if(result != -1){
System.out.println("Element " + key + " found at index " + result);
}
else{
System.out.println("Element " + key + " not found in the array");
}
sc.close(); //Close the scanner to prevent resouce leak
}
}