Binary Search in Rotated Sorted Array

Binary Search in Rotated Sorted Array

23 Sep 2025
Beginner
216 Views
5 min read
Learn with an interactive course and practical hands-on labs

Free DSA Online Course with Certification

Binary Search in Rotated Sorted Array

The Binary Search Algorithm for a rotated sorted array finds a target value in an array that was originally sorted in ascending order but rotated at some pivot point. The algorithm modifies standard binary search to handle the rotation by determining which half of the array is sorted and checking if the target lies in that sorted half. It assumes no duplicates in the array and has a time complexity of O(log n), where n is the length of the array.

Build a strong foundation for high-paying tech roles with DSA expertise. Enroll in our Free DSA Course today!

Example:

  • Input: nums = [4, 5, 6, 7, 0, 1, 2], target = 0
  • Output: 4
  • Explanation: The target value 0 is found at index 4 in the rotated sorted array.

Program (Python - Binary Search in Rotated Sorted Array)

The following Python code implements the Binary Search algorithm for a rotated sorted array with O(log n) time complexity, assuming no duplicates.

def search(nums, target):
    left, right = 0, len(nums) - 1
    
    while left <= right:
        mid = (left + right) // 2
        
        if nums[mid] == target:
            return mid
        
        # Check if left half is sorted
        if nums[left] <= nums[mid]:
            # Check if target is in left half
            if nums[left] <= target < nums[mid]:
                right = mid - 1
            else:
                left = mid + 1
        # Right half is sorted
        else:
            # Check if target is in right half
            if nums[mid] < target <= nums[right]:
                left = mid + 1
            else:
                right = mid - 1
    
    return -1

# Example usage
nums = [4, 5, 6, 7, 0, 1, 2]
target = 0
print(search(nums, target))  # Output: 4

Output

For the input nums = [4, 5, 6, 7, 0, 1, 2] and target = 0, the output is:

4

Explanation: The target value 0 is found at index 4 in the rotated sorted array.

The tech industry projects 25% job growth for Java full stack developers by 2026. Lead the charge—join our Java Full Stack Course and excel!

Practice It Yourself

Enter a rotated sorted array of numbers (comma-separated, no duplicates) and a target value to find its index using the Binary Search algorithm.

Take our Datastructures skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Amit Kumar (Software Engineer And Author)

Experienced Software Engineer with a demonstrated history of working in the professional training & coaching industry. Skilled in Asp.net MVC, Angular, Language Integrated Query (LINQ), SQL, C++, and HTML. Strong engineering professional graduated from Sathyabama University.
Live Training - Book Free Demo
ASP.NET Core Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Microservices Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack Java Developer Certification Training Course
01 Nov
05:30PM - 07:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this