Searching an element in sorted and rotated array GFG

 package com.company;


public class SearchingInSortedAndRotatedArray {
// search in a sorted and roted array
public static int search(int[] arr, int x) {
int n = arr.length;
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] > arr[low]) {
if (arr[low] <= x && arr[mid] > x) {
high = mid - 1;
}
else low = mid + 1;
}
else {
if (arr[high] >= x && arr[mid] < x) {
low = mid + 1;
}
else high = mid - 1;
}
}
return -1;
}

public static void main(String[] args) {
int[] array = {10, 20, 40, 60, 5, 8};
System.out.println(search(array, 6));
}
}

Comments