sorted array
rotated array
binary search
algorithm
search technique

Searching in a sorted and rotated array

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In computer science, searching is a vital operation, and efficiency can be critical, especially when dealing with large datasets. One of the complex variants of the search problem is searching in a sorted and rotated array. This type of array presents unique challenges due to its altered structure, and understanding how to efficiently find elements within it is a compelling topic.

What is a Sorted and Rotated Array?

A sorted and rotated array is an array that has originally been sorted in ascending order and then rotated or shifted around a pivot unknown to the observer. Consider the array `[0, 1, 2, 4, 5, 6, 7]`. After rotating it at pivot index 3, it becomes `[4, 5, 6, 7, 0, 1, 2]`.

The Problem

The main problem is to find a given element in a sorted and rotated array efficiently, ideally in `O(log n)` time. This efficiency suggests the use of a modified binary search algorithm.

Understanding Rotated Arrays

When rotating an array, the order is disrupted at the pivot point, forming two sorted sub-arrays. If we apply binary search to an entire rotated array, we must take into account two potential conditions:

  1. A portion of the array is still sorted.
  2. The pivot is within the two halves, causing the break in the order.

Key Steps

  1. Identify the Midpoint
    • Calculate the middle index of the array.
  2. Determine which part is sorted
    • Compare the values at the starting and midpoint indices.
    • If the start point is less than or equal to the midpoint, the left half is sorted.
    • If the start point is greater than the midpoint, the right half is sorted.
  3. Search in the Sorted Half
    • Check if the target is in the sorted half.
    • If it is, apply binary search in that half.
    • Otherwise, repeat the process on the unsorted half.

Pseudocode Example

Here's a pseudocode example demonstrating the binary search on a rotated array:

  • Single Element: The algorithm will naturally handle a single element array without alteration.
  • No Rotation: If the array has not been rotated, the algorithm functions as a simple binary search.
  • All Elements Same: If all elements are the same, the algorithm should return any index where the target is found, or indicate absence efficiently.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.