-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInRotatedArrayBinarySearch.cpp
More file actions
95 lines (81 loc) · 2.6 KB
/
Copy pathSearchInRotatedArrayBinarySearch.cpp
File metadata and controls
95 lines (81 loc) · 2.6 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
88
89
90
91
92
93
94
95
#include <iostream>
using namespace std;
int pivotElementOfAnArray(int array[], int size) {
// initialising value of start and end point of an array
int start = 0, end = size - 1, mid;
// binarySearch
while (start < end) {
/* mid index of array here we are not writing (s+e)/2 too handle a condition
where start and end both are 2^31-1 which will execeed the int range while adding */
mid = start + (end - start) / 2;
if (array[mid] >= array[0]) {
start = mid + 1;
} else {
end = mid;
}
}
return start;
}
int BinarySearchIterative(int array[], int key, int s, int e) {
int start = s;
int end = e;
// BinarySearch
while (start <= end) {
/* mid index of array here we are not writing (s+e)/2 too handle a condition
where start and end both are 2^31-1 which will execeed the int range while adding */
int mid = start + (end - start) / 2;
if (array[mid] == key) {
// if element found return its index and break out of while loop
return mid;
} else if (array[mid] < key) {
// pushing start to mid+1 if key is greater than mid element
start = mid + 1;
} else {
// pushing end to mid-1 if key is smaller than mid element
end = mid - 1;
}
}
return -1;
}
int searchInRotatedArray(int array, int size, int key) {
int pivot = pivotElementOfAnArray(array, size);
if (key >= array[pivot] && key <= array[size - 1]) {
int start = pivot;
int end = size - 1;
return BinarySearchRecursive(array, key, start, end);
} else {
int start = 0;
int end = pivot - 1;
return BinarySearchRecursive(array, key, start, end);
}
}
int main() {
// array of size 20
int array[20];
/* initialising array with custom size
which user will enter */
int size;
// element to search
int key;
// enter the size of array
cout << "Enter the size of array: ";
cin >> size;
// array of size entered by user
array[size];
// enter the array elements
cout << "Enter the array elements" << endl;
for (int i = 0; i < size; i++) {
cout << " Enter element at " << i << "th index: ";
cin >> array[i];
}
// enter element to search
cout << "\nEnter element to search: ";
cin >> key;
int result = searchInRotatedArray(array, size, key);
if (result == -1) {
cout << "Element not found in array";
} else {
cout << "Element found at index " << result;
}
return 0;
}