File tree 2 files changed +43
-0
lines changed
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Program for Linear Searching in JAVA
3
+ * return the index of searched element if found,
4
+ * else returns -1.
5
+ */
6
+ public class linear_searching
7
+ {
8
+ public static int search (int [] arr , int val )
9
+ {
10
+ int res ;
11
+ for (res = 0 ; res < arr .length ; ++res ) {
12
+ if (arr [res ] == val ) {
13
+ return res ;
14
+ }
15
+ }
16
+ return -1 ;
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments