We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1b49000 commit ff875deCopy full SHA for ff875de
searching/binary_searching.java
@@ -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