Skip to content

Commit e44a808

Browse files
authored
Add binary search
1 parent 6378043 commit e44a808

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/binary_search.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <iostream>
2+
#include <algorithm> // For std::sort
3+
4+
void printArray(const int *array, int size);
5+
int binarySearch(const int *array, int size, int value);
6+
7+
int main()
8+
{
9+
// Initialize an array with 10 integer values
10+
int array[10] = {1, 45, 65, 22, 76, 55, 88, 81, 99, 96};
11+
int valueToFind;
12+
13+
// Sort the array before performing binary search
14+
std::sort(array, array + 10);
15+
16+
// Print the sorted contents of the array
17+
printArray(array, 10);
18+
19+
// Ask the user to enter a number they want to find
20+
std::cout << "Enter the number you want to find: ";
21+
std::cin >> valueToFind;
22+
23+
// Use binary search to search for the value in the sorted array
24+
int index = binarySearch(array, 10, valueToFind);
25+
26+
// Check if the value was found and display the result
27+
if (index != -1)
28+
{
29+
std::cout << "The value was found at position: " << index << std::endl;
30+
}
31+
else
32+
{
33+
std::cout << "The value was not found in the array.\n";
34+
}
35+
36+
return 0;
37+
}
38+
39+
/**
40+
* @brief Prints the elements of an integer array (vector).
41+
*
42+
* This function takes an array and its size as parameters, and prints all the
43+
* elements of the array, separating them with a dash (" - ").
44+
*
45+
* @param array The integer array to be printed.
46+
* @param size The size of the array.
47+
*/
48+
49+
void printArray(const int *array, int size)
50+
{
51+
std::cout << "Array: ";
52+
for (int i = 0; i < size; i++)
53+
{
54+
std::cout << array[i] << " - "; // Print each element followed by " - "
55+
}
56+
std::cout << std::endl;
57+
}
58+
59+
/**
60+
* @brief Performs a binary search on a sorted array.
61+
*
62+
* This function implements the binary search algorithm, which efficiently finds
63+
* an element in a sorted array by repeatedly dividing the search interval in half.
64+
*
65+
* @param array The sorted array to search.
66+
* @param size The size of the array.
67+
* @param value The value to search for in the array.
68+
* @return The index of the element if found, or -1 if the element is not found.
69+
*/
70+
int binarySearch(const int *array, int size, int value)
71+
{
72+
int left{0};
73+
int right{size - 1};
74+
75+
while (left <= right)
76+
{
77+
int mid = left + (right - left) / 2; // Calculate middle index
78+
79+
// Check if the value is at the middle
80+
if (array[mid] == value)
81+
{
82+
return mid; // Value found, return its index
83+
}
84+
85+
// If the value is greater, ignore the left half
86+
if (array[mid] < value)
87+
{
88+
left = mid + 1;
89+
}
90+
// If the value is smaller, ignore the right half
91+
else
92+
{
93+
right = mid - 1;
94+
}
95+
}
96+
97+
return -1; // Value not found
98+
}

0 commit comments

Comments
 (0)