forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInRotatedSortedArray.java
More file actions
33 lines (27 loc) · 906 Bytes
/
Copy pathSearchInRotatedSortedArray.java
File metadata and controls
33 lines (27 loc) · 906 Bytes
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
package com.geeksforgeeks.array;
public class SearchInRotatedSortedArray {
public static void main(String[] args) {
search(new int[]{3, 4, 5, 1, 2, 6}, 0, 5, 6);
}
public static void search(int[] arr, int low, int high, int data) {
if (low > high)
return;
int mid = (low + high) / 2;
if (arr[mid] == data) {
System.out.println("Data Found at position " + mid);
return;
} else if (arr[low] <= arr[mid]) {
if (arr[low] <= data && arr[mid] >= data) {
search(arr, low, mid, data);
} else {
search(arr, mid + 1, high, data);
}
} else {
if (arr[mid] <= data && arr[high] >= data) {
search(arr, mid + 1, high, data);
} else {
search(arr, low, mid, data);
}
}
}
}