-
Notifications
You must be signed in to change notification settings - Fork 0
/
searching_in_a_nearly_sorted_array.c++
64 lines (56 loc) · 1.7 KB
/
searching_in_a_nearly_sorted_array.c++
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
/*SEARCH IN A NEARLY SORTED ARRAY:
Given an array which is sorted, but after sorting some elements are moved to either of the adjacent positions, i.e., arr[i] may be present at arr[i+1] or arr[i-1]. Write an efficient function to search an element in this array. Basically the element arr[i] can only be swapped with either arr[i+1] or arr[i-1].
For example consider the array {2, 3, 10, 4, 40}, 4 is moved to next position and 10 is moved to previous position.
Example :
Input: arr[] = {10, 3, 40, 20, 50, 80, 70}, key = 40
Output: 2
Output is index of 40 in given array
*/
#include <bits/stdc++.h>
using namespace std;
int binarySearchForSearchingInANearlySortedArray(int arr[], int n, int element)
{
int start = 0;
int end = n - 1;
while (start <= end)
{
int mid = start + (end - start) / 2;
if (arr[mid] == element)
{
return mid;
}
else if (mid - 1 >= start && arr[mid - 1] == element)
{
return mid - 1;
}
else if (mid + 1 <= end && arr[mid + 1] == element)
{
return mid + 1;
}
else if (arr[mid] > element)
{
end = mid - 2;
}
else
{
start = mid + 2;
}
}
return -1;
}
int main()
{
int arr[] = {5, 10, 30, 20, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int element = 40;
int foundAtIndex = binarySearchForSearchingInANearlySortedArray(arr, n, element);
if (foundAtIndex == -1)
{
cout << "The element " << element << " is not found in the array" << endl;
}
else
{
cout << "The element " << element << " is found at index: " << foundAtIndex << endl;
}
return 0;
}