Program to find element in sorted array of integers that is circulated k number of times in java
Problem Statement:- Write program to find a element in a circulated sorted array in java.
Solution: The given problem can be divided in two parts .First part contains the rotation of sorted array k number of times and second part consists of finding the element in the circulated sorted array .
The problem of array rotation we has already been discussed in our previous post rotate array given number of times .
To solve the second part we need to search element in that already circulated array , for that we can use any search algorithm .The most easiest solution to use linear search in array but its the efficient solution as we need to loop n number of times.
Since our array is already sorted so another solution will be to use binary search algorithm , but our array is circulated so we also need to modify our binary search algorithm .
If you are not aware of binary search then you can learn it in previous post binary search in java .
Here we will solve this problem in three approaches .
Note:- In these approaches we will use sorted array after 2 rotations .So in this i will use array rotation program given in post.
1.Using Linear Search :- In this solution we will loop the circulated sorted array and then we will find the element by comparing with array element in each index .
- rotate sorted array 2 times .
- loop array by the size of array
- compare each array element with search element.
- if element found return true else false.
private static boolean searchElementUsingLoop(int[] ar,int searchElement) {
for (int j : ar) {
if (j == searchElement) {
return true;
}
}
return false;
}
- if index is equal to length of array then return false as whole have checked whole array [terminating condition]
- if array element at given index and search element are equal return true [terminating condition]
- call the same method again with given array , search element and increased index .
private static boolean searchElementUsingRecursion(int[] ar,int searchElement,int index) {
if(index==ar.length)return false;
if(ar[index]==searchElement)return true;
return searchElementUsingRecursion(ar,searchElement,index+1);
}
- initialize three variables start ,end, mid as start =0,end = array length -1 , mid = (start + end)/2
- start looping array until start <=end
- compare search element with array[mid] , if equal return true
- if search element is less than middle element and greater than start element then go to left part
- if search element is greater than middle element and less than end element then go to right part
private static boolean searchElementUsingModifiedBinarySearch(int[] rsar,int searchElem) {
int start=0; int end= rsar.length-1;
int mid;
do {
mid = (start+end)/2;
if (searchElem == rsar[mid]) return true;
else if (rsar[mid] >= rsar[end]) { // right part from middle
if (searchElem < rsar[mid] && searchElem>=rsar[start]) { // go to left part of array
end = mid - 1;
} else {
start = mid+1;
}
}else { // left part from middle
if (searchElem > rsar[mid]) { // go to right part of array
start = mid + 1;
} else {
end = mid - 1;
}
}
} while (start <= end);
return false;
}