Skip to content

Commit 1685c8c

Browse files
Merge pull request #88 from LALITH0110/master
Add Binary Search Algorithm Implementation
2 parents ccaefe4 + 787c025 commit 1685c8c

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

src/.DS_Store

6 KB
Binary file not shown.

src/Algorithms/.DS_Store

6 KB
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class BinarySearch {
2+
3+
/**
4+
* Performs a binary search on a sorted array.
5+
*
6+
* @param arr The sorted array to search in
7+
* @param target The value to search for
8+
* @return The index of the target if found, -1 otherwise
9+
*/
10+
public static int binarySearch(int[] arr, int target) {
11+
int low = 0;
12+
int high = arr.length - 1; // Fixed: high should be arr.length - 1
13+
14+
while (low <= high) {
15+
int mid = low + (high - low) / 2; // Prevents potential overflow
16+
17+
if (arr[mid] == target) {
18+
return mid; // Target found
19+
} else if (arr[mid] < target) {
20+
low = mid + 1; // Target is in the upper half
21+
} else {
22+
high = mid - 1; // Target is in the lower half
23+
}
24+
}
25+
return -1; // Target not found
26+
}
27+
28+
public static void main(String[] args) {
29+
int[] arr = {2, 3, 4, 10, 40}; // Sample sorted array
30+
int target = 10; // Value to search for
31+
32+
int result = binarySearch(arr, target);
33+
34+
if (result == -1) {
35+
System.out.println("Element " + target + " is not present in the array");
36+
} else {
37+
System.out.println("Element " + target + " found at index " + result);
38+
}
39+
}
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Binary Search Algorithm
2+
Binary search is a fast and efficient searching algorithm used to find a specific element in a sorted array. It operates on the principle of divide and conquer, repeatedly dividing the search interval in half.
3+
4+
## How Binary Search Works
5+
* The algorithm begins by comparing the target value with the middle element of the sorted array.
6+
* If the target matches the middle element, the search is successful, and the index is returned.
7+
* If the target is less than the middle element, the search continues in the lower half of the array.
8+
* If the target is greater than the middle element, the search continues in the upper half of the array.
9+
* This process repeats, halving the search interval each time, until the target is found or the search interval is empty.
10+
11+
## Time Complexity
12+
The time complexity of binary search is O(log n), where n is the number of elements in the array. This logarithmic time complexity makes binary search significantly faster than linear search for large datasets.
13+
14+
## Prerequisites
15+
The array must be sorted in ascending or descending order.
16+
If the array is not sorted, binary search will not work correctly and may return incorrect results.
17+
18+
## Implementation
19+
Here's a Python implementation of the binary search algorithm using both iterative and recursive approaches:
20+
21+
## Iterative Implementation
22+
```Java
23+
public static int binarySearch(int[] arr, int target) {
24+
int left = 0;
25+
int right = arr.length - 1;
26+
27+
while (left <= right) {
28+
int mid = left + (right - left) / 2;
29+
if (arr[mid] == target) {
30+
return mid;
31+
} else if (arr[mid] < target) {
32+
left = mid + 1;
33+
} else {
34+
right = mid - 1;
35+
}
36+
}
37+
38+
return -1; // Target not found
39+
}
40+
```
41+
42+
Recursive Implementation
43+
``` Java
44+
public static int binarySearchRecursive(int[] arr, int target, int left, int right) {
45+
if (left > right) {
46+
return -1; // Target not found
47+
}
48+
49+
int mid = left + (right - left) / 2;
50+
if (arr[mid] == target) {
51+
return mid;
52+
} else if (arr[mid] < target) {
53+
return binarySearchRecursive(arr, target, mid + 1, right);
54+
} else {
55+
return binarySearchRecursive(arr, target, left, mid - 1);
56+
}
57+
}
58+
```
59+
60+
## Advantages of Binary Search
61+
* Efficiency: Binary search is much faster than linear search for large datasets.
62+
* Logarithmic time complexity: The search space is halved in each iteration, resulting in O(log n) time complexity.
63+
* Works well with large, sorted datasets: Particularly useful when dealing with extensive, ordered collections of data.
64+
65+
## Limitations
66+
* Requires sorted data: The array must be sorted before applying binary search.
67+
* Not suitable for small datasets: For very small arrays, linear search might be faster due to less overhead.
68+
* Not applicable to unsorted data structures: Binary search cannot be directly applied to unsorted lists or arrays.
69+
70+
## Applications
71+
Binary search is widely used in various applications, including:
72+
* Searching in large databases
73+
* Finding elements in sorted arrays
74+
* Implementing efficient dictionary lookups
75+
* Optimizing other algorithms that work on sorted data
76+
77+
## Conclusion
78+
Binary search is a powerful algorithm that significantly reduces search time in sorted arrays. Its logarithmic time complexity makes it an essential tool for efficient searching in large datasets. However, it's important to remember that the prerequisite of a sorted array is crucial for the algorithm to function correctly.

0 commit comments

Comments
 (0)