-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.c++
More file actions
34 lines (29 loc) · 827 Bytes
/
Question2.c++
File metadata and controls
34 lines (29 loc) · 827 Bytes
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
// Binary Question : Is it possible to perform a binary search on an unsorted array in C++ ?
// proof with example
#include <iostream>
bool linearSearch(int arr[], int size, int target)
{
for (int i = 0; i < size; ++i)
{
if (arr[i] == target)
{
return true; // Found the target element
}
}
return false; // Target element not found
}
int main()
{
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 9;
if (linearSearch(arr, size, target))
{
std::cout << "The target element " << target << " was found in the array." << std::endl;
}
else
{
std::cout << "The target element " << target << " was not found in the array." << std::endl;
}
return 0;
}