Java Program for Search an element in a sorted and rotated array


Suppose you have a sorted array with no duplicate values and from a certain index this array is rotated. You have to search for a particular element in it. If it is available in the array then return the index otherwise return -1.

In this article, we will solve the given problem using two approaches Linear search and Binary search.

Approach 1: Using Linear Search

This approach will search for the given element in sequential manner.

Algorithm

  • Step 1 − First, declare and initialize an array named ‘araylist’ and an integer variable named ‘searchElem’ that we are going to search in the array. We need two more integer variables ‘isFound’ and ‘location’.

  • Step 2 − Now, create a for loop that will run till the length of array. In this loop, take an if block that checks whether the ‘searchElem’ is present in the array or not. If it is available store its index in variable ‘location’ and increment the variable ‘isFound’ to 1.

  • Step 3 − Moving ahead, we create an if else block to check whether the variable ‘isFound’ is incremented to 1 or not. If it is equal to 1 it means element is found and we will return the index. If it is not then statement in the else block will get executed.

Example 1

public class Linear {
   public static void main(String[] args) {
      int araylist[] = {87, 89, 93, 0, 2, 5, 19};
      int searchElem = 2;
      int isFound = 0;
      int location = 0;
      for(int i = 0; i < araylist.length; i++) {
         if(searchElem == araylist[i]) {
            isFound = 1;
            location = i;
         } 
      }
      if(isFound == 1) {
         System.out.print("The given element is available at index: " + location);
      } else {
         System.out.print("Element is not available");
      }
   }
}

Output

The given element is available at index: 4

Approach 2: Using Binary Search

This approach will search for the given element by divide and conquer rule.

Algorithm

  • Step 1 − First, declare and initialize an array named ‘araylist’ and an integer variable named ‘searchElem’. We are going to search ‘searchElem’ in the array.

  • Step 2 − We create a parameterized method of return type int named ‘bSearch’ along with two parameters ‘araylist[]’ and ‘searchElem’ of type int.

  • Step 3 − In the method ‘bSearch’ we will declare and initialize variable ‘l’ to store the first index of array and ‘h’ to store last index of array.

  • Step 4 − Moving ahead, we create a while loop that will run from first to last index. In this loop, we declare a integer variable ‘mid’ to store the middle index of the array. Then, we create an if block that will check whether ‘searchElem’ is found at the mid index or not. If it is found then mid index will be returned.

  • Step 5 − Now, we create second if block to check whether the left array is sorted or not. If so, then again in the next if block we check the ‘searchElem’ lies between first and mid index or not. If it lies between them then ‘mid -1’ becomes the last index otherwise ‘mid + 1’ becomes the first index.

  • Step 6 − If the left array is not sorted that means right array is sorted. So, in the else block, we create another if block to check the ‘searchElem’ lies between mid and last index or not. If it lies between them then ‘mid + 1’ becomes the first index otherwise ‘mid -1’ becomes the last index.

Example 2

public class Binary {
   public int bSearch(int araylist[], int searchElem) {
      int l = 0;
      int h = araylist.length - 1;
      while(l <= h) {
         int mid = (l + h) / 2;
         if(araylist[mid] == searchElem) {
            return mid;
         }
         if(araylist[l] <= araylist[mid]) {
            if(searchElem >= araylist[l] && searchElem < araylist[mid]) {
               h = mid - 1;
            } else {
               l = mid + 1;
            } 
         } else {
            if(searchElem > araylist[mid] && searchElem <= araylist[h]) {
               l = mid + 1;
            } else {
               h = mid - 1;
            }
         }
      }
      return -1;
   }
   public static void main(String[] args) {
      int araylist[] = {87, 89, 93, 0, 2, 5, 19};
      int searchElem = 2;
      Binary obj = new Binary(); 
      // object creation 
      System.out.println("The given element is available at index: " + obj.bSearch(araylist, searchElem)); 
      // calling method with arguments
   }
}

Output

The given element is available at index: 4

Conclusion

In this article, we have discussed two approaches to search for an element in a sorted and rotated array. Binary search approach is more optimized than the linear search. It uses divide and conquer algorithm.

Updated on: 05-May-2023

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements