Skip to content

Commit 23dd9c6

Browse files
authored
Merge pull request #31 from Pika1998/master
Changed README.md and also added Binary Search Algorithm
2 parents 9ef7243 + 44cc540 commit 23dd9c6

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "CodeLens (Launch) - BinarySearch",
6+
"request": "launch",
7+
"mainClass": "BinarySearch",
8+
"projectName": "Java-Algorithms_881fe886"
9+
}
10+
]
11+
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# Java Algorithms
22
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors)
33

4+
5+
### Hub for **All Java Algorithms** arranged as per their categories
6+
7+
Provides a huge range of algorithm categories such as **sorting, searching, backtracking and much more**.
8+
9+
## Collaboration
10+
11+
**Open to collaboration and support**
12+
413
## Contributors ✨
514

15+
616
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
717

818
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

Searching/BinarySearch.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class BinarySearch {
2+
int Search(int arr[], int l, int r, int search_element)
3+
{
4+
if (r >= l) {
5+
int mid = l + (r - l) / 2;
6+
7+
// If the element is present at the
8+
// middle itself
9+
if (arr[mid] == search_element)
10+
return mid;
11+
12+
// If element is smaller than mid, then
13+
// it can only be present in left subarray
14+
if (arr[mid] > search_element)
15+
return Search(arr, l, mid - 1, search_element);
16+
17+
// Else the element can only be present
18+
// in right subarray
19+
return Search(arr, mid + 1, r, search_element);
20+
}
21+
22+
return -1;
23+
}
24+
25+
public static void main(String args[])
26+
{
27+
BinarySearch ob = new BinarySearch();
28+
int arr[] = { 2, 3, 4, 10, 40 };
29+
int n = arr.length-1;
30+
int search_element = 10;
31+
int result = ob.Search(arr, 0, n, search_element);
32+
if (result == -1)
33+
System.out.println("Element not present");
34+
else
35+
System.out.println("Element found at index " + result);
36+
}
37+
}

0 commit comments

Comments
 (0)