-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-sorted-rotated.cpp
48 lines (48 loc) · 2.31 KB
/
search-sorted-rotated.cpp
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
//Description : Searching element in a sorted-rotated array using binary search
#include<bits/stdc++.h>
using namespace std;
int search_array(vector <int> arr, int item) {
int low = 0;
int high = arr.size() - 1;
while (low <= high) {
int mid = low + (high-low)/2; //Calculating middle index
if (arr[mid] == item) { //If the element at middle index is equal to the item then we return the middle index
return mid;
}
else if (arr[mid] > arr[low]) { //Here we check whether left half of the array is sorted or not .
if ((arr[low] <= item) && (item<arr[mid])) { // If left half is sorted then we check whether item is greater than the lower index
high = mid - 1; //If item is greater than the lower index then we ignore the right half of the array
}
else { //If item is less than the lower index then we ignore the left half and start our search
low = mid + 1; //in the right half of the array
}
}
else { //This execute if right half is sorted
if ((arr[high] >= item) && (item>arr[mid])) { //We check whether our item is smaller or equal to the upper index
low = mid + 1; //If yes, then we start our search within the range (mid+1,high)
}
else {
high = mid - 1; //If item is greater than the element at higher index then we start
} //our search in the range(low,mid-1)
}
}
return -1;
}
int main() {
int t ;
int n;
int item;
vector <int> arr;
cin >> t; //Size of the array
while (t--) {
cin >> n;
arr.push_back(n);
}
cin >> item;
int found = search_array(arr, item);
cout << found << endl;
return 0;
//Time Complexity : O(log t)
//Ex: t=5, arr=[10,20,30,40,50,8,9], item=30
//found = 2
}