Skip to content

Commit 6e238ad

Browse files
Merge pull request #1 from Sphesihlebjamile/search/binary-search
Binary Search Algorithm
2 parents 30428c8 + f83f2dc commit 6e238ad

4 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎src/com/sketch/Main.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package com.sketch;
22

3+
import com.sketch.search.binarysearch.BinarySearch;
4+
35
public class Main {
46
public static void main(String[] args){
5-
System.out.println("Hello World!");
7+
int[] sortedArray = { 1, 2, 3, 5, 7, 9, 11, 14, 17, 26, 18, 37, 42, 43, 44, 47, 48, 49, 78, 227, 5556, 8976 };
8+
int valueToFind = 18;
9+
10+
int binarySearchIndex = BinarySearch.searchRecursive(sortedArray, valueToFind, 0, sortedArray.length - 1);
11+
12+
System.out.println("Expected Value Index: 20");
13+
System.out.println("Actual Value Index: " + binarySearchIndex);
614
}
715
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.sketch.search.binarysearch;
2+
3+
import java.util.Arrays;
4+
5+
public class BinarySearch {
6+
public static int search(int[] sortedNumbers, int numberToFind){
7+
int low = 0;
8+
int high = sortedNumbers.length - 1;
9+
10+
while (low <= high){
11+
int middlePosition = (low + high) / 2;
12+
int middleNumber = sortedNumbers[middlePosition];
13+
if(middleNumber == numberToFind){
14+
return middlePosition;
15+
}
16+
else if(middleNumber < numberToFind){
17+
low = middlePosition + 1;
18+
}
19+
else {
20+
high = middlePosition - 1;
21+
}
22+
}
23+
24+
return -1;
25+
}
26+
27+
public static int searchDefault(int[] sortedNumbers, int numberToFind){
28+
return Arrays.binarySearch(sortedNumbers, numberToFind);
29+
}
30+
31+
public static int searchRecursive(int[] sortedNumbers, int numberToFind, int low, int high){
32+
if(low > high){
33+
return -1;
34+
}
35+
int middlePosition = (low + high) / 2;
36+
int middleNumber = sortedNumbers[middlePosition];
37+
if(middleNumber == numberToFind){
38+
return middlePosition;
39+
}
40+
else if(middleNumber < numberToFind){
41+
low = middlePosition + 1;
42+
return searchRecursive(sortedNumbers, numberToFind, low, high);
43+
}
44+
else {
45+
high = middlePosition - 1;
46+
return searchRecursive(sortedNumbers, numberToFind, low, high);
47+
}
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Binary Search Algorithm
2+
3+
**Binary Search** is a searching algorithm that operates on a sorted or monotonic search space, repeatedly dividing it into halves to
4+
find a target value or optimal answer in logarithmic time **O(log N**.
5+
6+
![img.png](img.png)
7+
8+
Implementation: [BinarySearch.java](./BinarySearch.java)
9+
10+
The Binary Search algorithm can only be applied to data-structures that:
11+
- are sorted,
12+
- and access to any element of the data-structure takes constant time **O(1)**.
13+
14+
How does it work:
15+
- Divide the data-structure into two halves by finding the middle index '`mid`'
16+
- Compare the value at `mid` with the `key`
17+
- If the `key` is equals to the value at `mid`, then terminate the process
18+
- If the `key` is not found at the middle, choose which half hald will be used as the next search space.
19+
- If the `key` is smaller than the middle, then the `left` side is used for the next search.
20+
- If the `key` is larger than the middle, then the `right` side is used for the next search.
21+
- This process is continues until the `key` is found or the total search space is exhausted.
22+
23+
## Complexity Analysis
24+
- **Time Complexity**
25+
- Best Case: O(1)
26+
- Average Case: O(log N)
27+
- Worst Case: O(log N)
28+
- **Auxiliary Space**:
29+
- Normal Implementation: O(1)
30+
- Recursive Implementation: O(log N)
31+
32+
## Applications
33+
- Searching in sorted arrays
34+
- Finding first/last occurrence or closest match in a sorted array
35+
- Database indexing — Used in B-trees and similar structures for fast data lookup.
36+
- File systems & libraries — Fast search through sorted directories or symbol tables.
37+
- Machine learning tuning — Efficient hyperparameter search (e.g., learning rate, thresholds).
38+
- Network routing & IP lookup — Efficiently find routing entries in tables sorted by address ranges.
192 KB
Loading

0 commit comments

Comments
 (0)