What is an Algorithm?
An algorithm is a step-by-step procedure or a finite set of instructions used to solve a specific problem or
perform a task. Algorithms are essential in computer science, as they define how data is processed,
transformed, and analyzed.
Example: Steps to Find the Minimum in a Rotated Array
1. Check if the array is rotated.
2. Use binary search logic to locate the minimum element.
3. Return the minimum value.
What is a Sorted and Rotated Array?
A sorted and rotated array is a sorted array that has been rotated around a pivot. For example:
Original sorted array: [1, 2, 3, 4, 5]
After rotation: [3, 4, 5, 1, 2]
The rotation maintains order within subarrays but shifts the smallest value out of its original position.
Python Implementation
Below is the Python code to find the minimum in a sorted and rotated array using binary search logic:
python
Copy code
def find_min_in_rotated_array(arr):
left, right = 0, len(arr) - 1
while left < right:
mid = left + (right - left) // 2
# If mid element is greater than the rightmost element
if arr[mid] > arr[right]:
left = mid + 1
elif arr[mid] < arr[right]:
right = mid
else: # arr[mid] == arr[right], handle duplicates
right -= 1
return arr[left]
# Example usage
arr = [4, 5, 6, 7, 0, 1, 2]
print("Minimum element:", find_min_in_rotated_array(arr))
Diagram: Binary Search in Rotated Array
sql
Copy code
Initial array: [4, 5, 6, 7, 0, 1, 2]
Target: Find minimum
1. Set pointers: left=0, right=6
2. Calculate mid: mid=(0+6)//2 = 3 (value=7)
3. Compare arr[mid] (7) > arr[right] (2) → Move left=mid+1
New range: [0, 1, 2]
4. Repeat binary search until left==right
5. Minimum found at arr[left]
Start → Input Array → Initialize Minimum as arr[0]
For Each Element in Array
If Element < Minimum
Update Minimum
End Loop
Output Minimum
Stop
Visual Explanation of Sorted and Rotated Array
1. Original Sorted Array:
o The array [1, 2, 3, 4, 5] is sorted in ascending order.
o Values increase consistently from left to right.
2. Rotated Array:
o The array [3, 4, 5, 1, 2] is derived by rotating the original array around a pivot (in this
case, 2).
o The rotation preserves internal order within subarrays, but the smallest value (1) moves
to a different position.
Next, I’ll explain the Python code line by line and create a diagram for the binary search logic.
Line-by-Line Code Explanation
python
Copy code
def find_min_in_rotated_array(arr):
Function Definition: Defines a function find_min_in_rotated_array that takes a single parameter
arr (the input array).
python
Copy code
left, right = 0, len(arr) - 1
Initialize Pointers: Two pointers are initialized:
o left: Points to the start of the array (index 0).
o right: Points to the end of the array (len(arr) - 1).
python
Copy code
while left < right:
Binary Search Loop: Repeats until left equals right. This ensures the search range is reduced to
one element, which is the minimum.
python
Copy code
mid = left + (right - left) // 2
Midpoint Calculation: Calculates the middle index between left and right.
python
Copy code
if arr[mid] > arr[right]:
left = mid + 1
Check if Pivot is on the Right:
o If arr[mid] > arr[right], the minimum lies in the right half of the array.
o Move the left pointer to mid + 1.
python
Copy code
elif arr[mid] < arr[right]:
right = mid
Check if Pivot is on the Left:
o If arr[mid] < arr[right], the minimum lies in the left half (including mid).
o Move the right pointer to mid.
python
Copy code
else: # arr[mid] == arr[right], handle duplicates
right -= 1
Handle Duplicates:
o If arr[mid] == arr[right], reduce the search range by decrementing right.
python
Copy code
return arr[left]
Return Minimum: When the loop exits, left points to the smallest element.
Binary Search Logic Diagram
I’ll now create a diagram illustrating the binary search process in this algorithm.
Diagram Explanation: Binary Search in a Rotated Array
1. Phase 1: Initial Range
o left = 0, right = 6, mid = 3 (value = 7).
o Since arr[mid] > arr[right], the minimum lies in the right half. Update left = mid + 1.
2. Phase 2: Right Half
o left = 4, right = 6, mid = 5 (value = 1).
o Now, arr[mid] < arr[right], so the minimum lies in the left half (including mid). Update
right = mid.
3. Phase 3: Found Minimum
o left = 4, right = 4, mid = 4 (value = 0).
o The pointers converge, and arr[left] (0) is the minimum.