What if you could find anything fast even when the order seems broken?
Why Search in Rotated Sorted Array in DSA Rust?
Imagine you have a long list of books sorted by title on a shelf. Suddenly, someone rotates part of the shelf, so the order is broken but still partially sorted. Now, you want to find a specific book quickly.
Looking for the book by checking each title one by one is slow and tiring. The usual quick search methods fail because the order is not fully sorted anymore, making it confusing and error-prone.
This concept helps you find the book fast by cleverly deciding which part of the rotated shelf to search next, using a smart method that works even when the order is broken.
for i in 0..array.len() { if array[i] == target { return i; } } return -1;
fn search_rotated(array: &[i32], target: i32) -> i32 {
let mut left = 0;
let mut right = array.len() as i32 - 1;
while left <= right {
let mid = (left + right) / 2;
if array[mid as usize] == target {
return mid;
}
if array[left as usize] <= array[mid as usize] {
if array[left as usize] <= target && target < array[mid as usize] {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if array[mid as usize] < target && target <= array[right as usize] {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
-1
}You can quickly find any item in a list that has been rotated, saving time and effort even when the order looks confusing.
Finding a specific time in a daily schedule that was shifted due to daylight saving time, without checking every single appointment.
Manual search is slow and inefficient for rotated lists.
Smart binary search adapts to the rotation and finds targets quickly.
This method works well for partially sorted, rotated data.
Practice
Solution
Step 1: Understand the array structure
A rotated sorted array is originally sorted but then rotated at some pivot, splitting it into two sorted parts.Step 2: Impact on search
This split means a normal binary search can't be applied directly because the order is disrupted at the pivot.Final Answer:
The array is split into two sorted parts, making direct binary search tricky -> Option DQuick Check:
Rotated array = two sorted parts [OK]
- Assuming the array is fully sorted without rotation
- Ignoring the pivot point in the array
- Thinking duplicates cause the main challenge
- Confusing descending order with rotation
Solution
Step 1: Analyze parameter types
Using a slice &[i32] is efficient and idiomatic for read-only array input in Rust.Step 2: Analyze return type
Returning Option<usize> allows indicating if the target was found (Some(index)) or not (None).Final Answer:
fn search(nums: &[i32], target: i32) -> Option<usize> -> Option AQuick Check:
Slice input + Option return = fn search(nums: &[i32], target: i32) -> Option<usize> [OK]
- Using Vec<i32> unnecessarily copies data
- Returning usize without Option loses 'not found' info
- Returning bool loses index information
- Returning i32 index can be ambiguous
fn search(nums: &[i32], target: i32) -> Option<usize> {
let mut left = 0;
let mut right = nums.len() - 1;
while left <= right {
let mid = (left + right) / 2;
if nums[mid] == target {
return Some(mid);
}
if nums[left] <= nums[mid] {
if nums[left] <= target && target < nums[mid] {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if nums[mid] < target && target <= nums[right] {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
None
}
let arr = [4,5,6,7,0,1,2];
println!("{:?}", search(&arr, 3));Solution
Step 1: Understand the array and target
The array is [4,5,6,7,0,1,2], target is 3 which is not present in the array.Step 2: Trace the search logic
The function performs binary search on rotated array but since 3 is missing, the loop ends returning None.Final Answer:
None -> Option AQuick Check:
3 not in array = None [OK]
- Assuming target 3 is present and returning wrong index
- Confusing mid calculation causing infinite loop
- Ignoring rotation logic in conditions
- Returning Some with wrong index
fn search(nums: &[i32], target: i32) -> Option<usize> {
let mut left = 0;
let mut right = nums.len() - 1;
while left < right {
let mid = (left + right) / 2;
if nums[mid] == target {
return Some(mid);
}
if nums[left] <= nums[mid] {
if nums[left] <= target && target < nums[mid] {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if nums[mid] < target && target <= nums[right] {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
None
}Solution
Step 1: Analyze loop condition
The loop uses while left < right, which misses checking when left == right, possibly skipping the last element.Step 2: Correct loop condition
Changing to while left <= right ensures all elements are checked, including when left equals right.Final Answer:
The loop condition should be while left <= right, not while left < right -> Option CQuick Check:
Loop must cover all indices [OK]
- Using < instead of <= causing missed checks
- Trying floating division for mid index
- Changing return type unnecessarily
- Making array mutable without need
Solution
Step 1: Understand duplicates impact
Duplicates can make it unclear which side is sorted because nums[left], nums[mid], nums[right] may be equal.Step 2: Modify algorithm to skip duplicates
When nums[left] == nums[mid] == nums[right], increment left and decrement right to skip duplicates and continue search.Step 3: Avoid sorting or linear search
Sorting changes array order; linear search is inefficient. Proper duplicate handling keeps binary search efficient.Final Answer:
Add extra checks to skip duplicates when nums[left] == nums[mid] == nums[right] -> Option BQuick Check:
Skip duplicates to maintain binary search [OK]
- Sorting array losing rotation info
- Using linear search losing efficiency
- Ignoring duplicates causing wrong results
- Assuming duplicates don't affect logic
Ready to prove it?
Pass the test and this pattern moves unknown
