-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathAllSearchingAlgorithm.cpp
More file actions
87 lines (67 loc) · 2.11 KB
/
AllSearchingAlgorithm.cpp
File metadata and controls
87 lines (67 loc) · 2.11 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
int linearSearch(const std::vector<int>& arr, int target) {
for (int i = 0; i < arr.size(); ++i) {
if (arr[i] == target)
return i;
}
return -1;
}
int binarySearch(const std::vector<int>& arr, int target) {
int left = 0;
int right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int interpolationSearch(const std::vector<int>& arr, int target) {
int left = 0;
int right = arr.size() - 1;
while (left <= right && target >= arr[left] && target <= arr[right]) {
if (left == right) {
if (arr[left] == target)
return left;
return -1;
}
int pos = left + ((target - arr[left]) * (right - left)) / (arr[right] - arr[left]);
if (arr[pos] == target)
return pos;
if (arr[pos] < target)
left = pos + 1;
else
right = pos - 1;
}
return -1;
}
void printSearchResult(int result, const std::string& algorithm) {
if (result != -1)
std::cout << "Element found at index " << result << " using " << algorithm << " search.\n";
else
std::cout << "Element not found using " << algorithm << " search.\n";
}
int main() {
std::vector<int> arr = {10, 20, 30, 40, 50, 60, 70};
std::cout << "Array: ";
for (int value : arr)
std::cout << value << " ";
std::cout << "\n";
int target = 50;
// Linear Search
int linearResult = linearSearch(arr, target);
printSearchResult(linearResult, "Linear");
// Binary Search (Array must be sorted)
std::sort(arr.begin(), arr.end());
int binaryResult = binarySearch(arr, target);
printSearchResult(binaryResult, "Binary");
// Interpolation Search (Array must be sorted)
int interpolationResult = interpolationSearch(arr, target);
printSearchResult(interpolationResult, "Interpolation");
return 0;
}