Python3 Program for Search an element in a sorted and rotated array
Last Updated :
06 Sep, 2024
An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time.

Example:
Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3};
key = 3
Output : Found at index 8
Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3};
key = 30
Output : Not found
Input : arr[] = {30, 40, 50, 10, 20}
key = 10
Output : Found at index 3
All solutions provided here assume that all elements in the array are distinct.
Basic Solution:
Approach:
- The idea is to find the pivot point, divide the array in two sub-arrays and perform binary search.
- The main idea for finding pivot is – for a sorted (in increasing order) and pivoted array, pivot element is the only element for which next element to it is smaller than it.
- Using the above statement and binary search pivot can be found.
- After the pivot is found out divide the array in two sub-arrays.
- Now the individual sub – arrays are sorted so the element can be searched using Binary Search.
Implementation:
Input arr[] = {3, 4, 5, 1, 2}
Element to Search = 1
1) Find out pivot point and divide the array in two
sub-arrays. (pivot = 2) /*Index of 5*/
2) Now call binary search for one of the two sub-arrays.
(a) If element is greater than 0th element then
search in left array
(b) Else Search in right array
(1 will go in else as 1 < 0th element(3))
3) If element is found in selected sub-array then return index
Else return -1.
Below is the implementation of the above approach:
Python3
# Python Program to search an element
# in a sorted and pivoted array
# Searches an element key in a pivoted
# sorted array arrp[] of size n
def pivotedBinarySearch(arr, n, key):
pivot = findPivot(arr, 0, n-1);
# If we didn't find a pivot,
# then array is not rotated at all
if pivot == -1:
return binarySearch(arr, 0, n-1, key);
# If we found a pivot, then first
# compare with pivot and then
# search in two subarrays around pivot
if arr[pivot] == key:
return pivot
if arr[0] <= key:
return binarySearch(arr, 0, pivot-1, key);
return binarySearch(arr, pivot + 1, n-1, key);
# Function to get pivot. For array
# 3, 4, 5, 6, 1, 2 it returns 3
# (index of 6)
def findPivot(arr, low, high):
# base cases
if high < low:
return -1
if high == low:
return low
# low + (high - low)/2;
mid = int((low + high)/2)
if mid < high and arr[mid] > arr[mid + 1]:
return mid
if mid > low and arr[mid] < arr[mid - 1]:
return (mid-1)
if arr[low] >= arr[mid]:
return findPivot(arr, low, mid-1)
return findPivot(arr, mid + 1, high)
# Standard Binary Search function*/
def binarySearch(arr, low, high, key):
if high < low:
return -1
# low + (high - low)/2;
mid = int((low + high)/2)
if key == arr[mid]:
return mid
if key > arr[mid]:
return binarySearch(arr, (mid + 1), high,
key);
return binarySearch(arr, low, (mid -1), key);
# Driver program to check above functions */
# Let us search 3 in below array
arr1 = [5, 6, 7, 8, 9, 10, 1, 2, 3]
n = len(arr1)
key = 3
print("Index of the element is : ",
pivotedBinarySearch(arr1, n, key))
# This is contributed by Smitha Dinesh Semwal
Output:
Index of the element is : 8
Complexity Analysis:
- Time Complexity: O(log n).
Binary Search requires log n comparisons to find the element. So time complexity is O(log n). - Space Complexity:O(1), No extra space is required.
Thanks to Ajay Mishra for initial solution.
Improved Solution:
Approach: Instead of two or more pass of binary search the result can be found in one pass of binary search. The binary search needs to be modified to perform the search. The idea is to create a recursive function that takes l and r as range in input and the key.
1) Find middle point mid = (l + h)/2
2) If key is present at middle point, return mid.
3) Else If arr[l..mid] is sorted
a) If key to be searched lies in range from arr[l]
to arr[mid], recur for arr[l..mid].
b) Else recur for arr[mid+1..h]
4) Else (arr[mid+1..h] must be sorted)
a) If key to be searched lies in range from arr[mid+1]
to arr[h], recur for arr[mid+1..h].
b) Else recur for arr[l..mid]
Below is the implementation of above idea:
Python3
# Search an element in sorted and rotated array using
# single pass of Binary Search
# Returns index of key in arr[l..h] if key is present,
# otherwise returns -1
def search (arr, l, h, key):
if l > h:
return -1
mid = (l + h) // 2
if arr[mid] == key:
return mid
# If arr[l...mid] is sorted
if arr[l] <= arr[mid]:
# As this subarray is sorted, we can quickly
# check if key lies in half or other half
if key >= arr[l] and key <= arr[mid]:
return search(arr, l, mid-1, key)
return search(arr, mid + 1, h, key)
# If arr[l..mid] is not sorted, then arr[mid... r]
# must be sorted
if key >= arr[mid] and key <= arr[h]:
return search(a, mid + 1, h, key)
return search(arr, l, mid-1, key)
# Driver program
arr = [4, 5, 6, 7, 8, 9, 1, 2, 3]
key = 6
i = search(arr, 0, len(arr)-1, key)
if i != -1:
print ("Index: % d"% i)
else:
print ("Key not found")
# This code is contributed by Shreyanshi Arun
Output:
Index: 2
Complexity Analysis:
- Time Complexity: O(log n).
Binary Search requires log n comparisons to find the element. So time complexity is O(log n). - Space Complexity: O(1).
As no extra space is required.
Approach 3: Using Stacks:
The stack-based approach can be used to search for an element in a sorted and rotated array. The basic idea is to traverse the array and push the elements onto a stack until the element being searched is found. Once the element is found, the index of the element is returned.
To implement this approach, we can start by creating an empty stack and pushing the first element
of the array onto the stack. Then, we can traverse the remaining elements of the array and compare
them with the element being searched. If the element being searched is found, we return the index
of the element. Otherwise, we push the element onto the stack.
If the current element is less than the top element of the stack, it means that we have
reached the end of the sorted part of the array. So, we pop the elements from the stack until
we find an element that is smaller than the current element or the stack becomes empty.
If the stack becomes empty, it means that we have traversed the entire array and the element
is not present.
If the current element is greater than the top element of the stack, it means that we are still
in the sorted part of the array. So, we simply push the element onto the stack and continue
traversing the array.
Here is the code of above approach:
Python
def search(arr, n, key):
# Create a stack to hold the elements
s = []
# Push all the elements of the array onto the stack
for i in range(n):
s.append(arr[i])
# Find the pivot element
pivot = 0
while s:
top = s.pop()
if not s or top < s[-1]:
pivot = n - pivot - 1
break
pivot += 1
# Binary search for the key in the appropriate subarray
l, h = 0, n - 1
if key >= arr[pivot] and key <= arr[h]:
l = pivot
else:
h = pivot - 1
while l <= h:
mid = (l + h) // 2
if arr[mid] == key:
return mid
elif arr[mid] < key:
l = mid + 1
else:
h = mid - 1
# Key not found
return -1
# Driver program
arr = [4, 5, 6, 7, 8, 9, 1, 2, 3]
n = len(arr)
key = 6
i = search(arr, n, key)
if i != -1:
print("Index:", i)
else:
print("Key not found")
Output:
Index: 2
Complexity Analysis:
Time Complexity: O(N), where N is the size of the array
Space Complexity: O(N), where N is the size of the array
Thanks to Gaurav Ahirwar for suggesting above solution.
How to handle duplicates?
It doesn’t look possible to search in O(Logn) time in all cases when duplicates are allowed. For example consider searching 0 in {2, 2, 2, 2, 2, 2, 2, 2, 0, 2} and {2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}.
It doesn’t look possible to decide whether to recur for the left half or right half by doing a constant number of comparisons at the middle.
Similar Articles:
Please write comments if you find any bug in the above codes/algorithms, or find other ways to solve the same problem.
Please refer complete article on Search an element in a sorted and rotated array for more details!
Similar Reads
Python3 Program for Check if an array is sorted and rotated
Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 }Output : YESThe above arra
3 min read
Python Program for Last duplicate element in a sorted array
We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu
2 min read
Python3 Program to Find Mth element after K Right Rotations of an Array
[GFGTABS] Python3 # Python3 program to implement # the above approach # Function to return Mth element of # array after k right rotations def getFirstElement(a, N, K, M): # The array comes to original state # after N rotations K %= N # If K is greater or equal to M if (K >= M): # Mth element afte
1 min read
Python Program to Sort an array in wave form
Given an unsorted array of integers, sort the array into a wave like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
3 min read
Python program to Search an Element in a Circular Linked List
A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input: CList = 6->5->4->3->2
3 min read
Python3 Program to Print all possible rotations of a given Array
Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
3 min read
Python3 Program to Find the Mth element of the Array after K left rotations
Given non-negative integers K, M, and an array arr[] with N elements find the Mth element of the array after K left rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1Output: 5Explanation: The array after first left rotation a1[ ] = {4, 5, 23, 3}The array after second left rotation a2[ ]
2 min read
Python Program for Find the element that appears once
Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once. Expected time complexity is O(n) and O(1) extra space. Examples : Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3} Output: 2 In the given array all element appear thre
2 min read
Python3 Program to Split the array and add the first part to the end | Set 2
Given an array and split it from a specified position, and move the first part of array add to the end. Examples: Input : arr[] = {12, 10, 5, 6, 52, 36} k = 2Output : arr[] = {5, 6, 52, 36, 12, 10}Explanation : Split from index 2 and first part {12, 10} add to the end .Input : arr[] = {3, 1, 2} k =
2 min read
Python3 Program to Count rotations in sorted and rotated linked list
Given a linked list of n nodes which is first sorted, then rotated by k elements. Find the value of k. The idea is to traverse singly linked list to check condition whether current node value is greater than value of next node. If the given condition is true, then break the loop. Otherwise increase
3 min read