Skip to content

Commit ff875de

Browse files
authored
binary searching in JAVA
1 parent 1b49000 commit ff875de

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

searching/binary_searching.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Program for Binary Searching in JAVA
3+
* return the index of searched element if found,
4+
* else returns -1.
5+
*
6+
* Prerequisite -- Array has to be SORTED.
7+
*/
8+
public class binary_searching
9+
{
10+
public static int searching(int[] arr, int val)
11+
{
12+
int u, l, m;
13+
for (l = 0, u = arr.length; l <= u; ) {
14+
m = (l + u) / 2;
15+
if (arr[m] == val) {
16+
return m;
17+
} else if (arr[m] < val) {
18+
u = m - 1;
19+
} else {
20+
l = m + 1;
21+
}
22+
}
23+
return -1;
24+
}
25+
}

0 commit comments

Comments
 (0)