Skip to content

Commit ff36dad

Browse files
Merge pull request OpenGuide#1 from chinmoy159/master
added a few simple codes in JAVA
2 parents 8203398 + ff875de commit ff36dad

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-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+
}

searching/linear_searching.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)