forked from SR-Sunny-Raj/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchnearlysortedarray.c
53 lines (39 loc) · 967 Bytes
/
searchnearlysortedarray.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
#include <stdio.h>
int searchElement(int nums[], int n, int target)
{
int low = 0, high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (nums[mid] == target) {
return mid;
}
else if (mid - 1 >= low && nums[mid - 1] == target) {
return mid - 1;
}
else if (mid + 1 <= high && nums[mid + 1] == target) {
return mid + 1;
}
else if (target > nums[mid]) {
low = mid + 2;
}
else {
high = mid - 2;
}
}
return -1;
}
int main(void)
{
int nums[] = { 2, 1, 3, 5, 4, 7, 6, 8, 9 };
int target = 5;
int n = sizeof(nums) / sizeof(nums[0]);
int index = searchElement(nums, n, target);
if (index != -1) {
printf("Element %d found at index %d", target, index);
}
else {
printf("Element not found in the array");
}
return 0;
}