-
Notifications
You must be signed in to change notification settings - Fork 40
/
search_in_rotated_sorted_array.c
69 lines (61 loc) · 1.92 KB
/
search_in_rotated_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
65
66
67
68
69
/*
* Date: 2018-06-12
*
* Description:
* Search an element from rotated sorted array like [5, 1, 2, 3, 4].
*
* Approach:
* Either left or right half of the array will surely be sorted so we can check
* if element lies in sorted part or not otherwise search other half of array.
*
* Complexity: O(log(n))
*/
#include "stdio.h"
#include "stdlib.h"
/*
* Description:
* Finds index of element in rotated sorted array, if element not found, returns
* -1.
*
* Args:
* A: Base address of array.
* low: low index.
* high: high index.
* key: Key element to be searched.
*/
int search_in_rotated_sorted_array(int A[], int low, int high, int key) {
int mid = low + (high - low)/2;
if (low > high) return -1; // Element not found.
if (key == A[mid]) return mid; // Element found.
// Check if A[low to mid] is sorted.
if (A[low] <= A[mid]) {
// Check if key lies between A[low to mid].
if (key >= A[low] && key <= A[mid])
return search_in_rotated_sorted_array(A, low, mid - 1, key);
return search_in_rotated_sorted_array(A, mid + 1, high, key);
}
// If A[low to mid] is not sorted then A[mid to high] will surely be sorted.
if (key >= A[mid] && key <= A[high]) // Check if key between A[mid to high].
return search_in_rotated_sorted_array(A, mid + 1, high, key);
return search_in_rotated_sorted_array(A, low, mid - 1, key);
}
int main() {
int i = 0, n = 0, key = 0, idx = -1;
int *A = NULL;
printf("Enter number of elements in rotated sorted array: ");
scanf("%d", &n);
A = (int *)malloc(n * (sizeof (int)));
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i);
scanf("%d", &A[i]);
}
printf("Enter element to be searched: ");
scanf("%d", &key);
// Calling function to find index of key.
idx = search_in_rotated_sorted_array(A, 0, n - 1, key);
if (idx == -1)
printf("%d not found\n", key);
else
printf("%d is found at index: %d\n", key, idx);
return 0;
}