-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathternarySearch.cpp
More file actions
63 lines (50 loc) · 1.2 KB
/
ternarySearch.cpp
File metadata and controls
63 lines (50 loc) · 1.2 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
#include <iostream>
#include <algorithm>
using namespace std;
int ternarySearch(int arr[], int size) {
int key;
cout << "\nEnter the key : ";
cin >> key;
int right, left, mid1, mid2;
left = 0;
right = (size - 1);
while (left <= right) {
mid1 = left + (right - left) / 3;
mid2 = right - (right - left) / 3;
if (arr[mid1] == key) {
return mid1;
}
else if (arr[mid2] == key) {
return mid2;
}
else if (key < arr[mid1]) {
right = mid1 - 1;
}
else if (key > arr[mid2]) {
left = mid2 + 1;
}
else {
left = mid1 + 1;
right = mid2 - 1;
}
}
return -1; // Key not found
}
int main() {
int size;
cout << "Enter the size of the array : ";
cin >> size;
int arr[size];
for (int i = 0; i < size; i++) {
cout << "Enter the element no #" << (i + 1) << " : ";
cin >> arr[i];
}
int temp;
sort(arr, arr + size);
cout << "Sorted Array : ";
for (int i : arr) {
cout << '[' << i << ']';
}
cout << "At index position : " << ternarySearch(arr, size);
return 0;
}