-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-search.cpp
More file actions
58 lines (52 loc) · 1.63 KB
/
binary-search.cpp
File metadata and controls
58 lines (52 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @file BinarySearch.cpp
* @brief Template implementation of the Binary Search algorithm.
* * Note: This algorithm requires the input array to be sorted in ascending order.
*/
#include <iostream>
using namespace std;
/**
* @brief Performs a Binary Search on a sorted, fixed-size array.
* * This implementation utilizes template argument deduction to handle various
* data types and retrieve the array size 'n' automatically at compile time.
* * @tparam T The data type of the array elements (must support comparison operators).
* @tparam n The size of the array, deduced by the compiler via reference.
* @param arr A reference to the sorted array to be searched.
* @param key The target value to locate.
* @return int The index of the key if found; otherwise -1.
*/
template <typename T, std::size_t n>
int binarySearch(T (&arr)[n], T key)
{
int low = 0;
int high = n - 1;
int mid;
while (low <= high)
{
// Calculate the middle index
mid = (low + high) / 2;
// Check if the key is present at mid
if (arr[mid] == key)
{
return mid;
}
// If key is greater, ignore the left half
else if (arr[mid] < key)
{
low = mid + 1;
}
// If key is smaller, ignore the right half
else
{
high = mid - 1;
}
}
// Target element was not found in the array
return -1;
}
/**
* Technical Implementation Details:
* 1. Passing by reference 'T (&arr)[n]' ensures O(1) space for parameter passing.
* 2. Time Complexity: O(log n).
* 3. Space Complexity: O(1) for iterative approach.
*/