Binary search when the array is sorted only in parts.
Every position before lo is in the first run, and every position from hi on is in the second, so lo ends where the second run starts.
Use it when a sorted list was rotated, so it has two sorted runs and one break. You need its smallest value or a search for a value. Signal words: sorted but the start moved, a ring buffer that wrapped, rotated by an unknown amount.
Start: lo = 0 and hi = n. The condition is values[mid] > values[n - 1]: true means that mid is in the first run.
Step: while lo < hi, set mid = lo + (hi - lo) / 2. If the condition is true, set lo = mid + 1. Otherwise set hi = mid.
Stop: when lo == hi. The answer is lo, the position of the smallest value. To search, pick the run with the last value, and use the lower bound inside it.
It does not work on a list with more than one break, or on a list with repeats at both ends.
Cost: at most floor(log2 n) + 1 checks for the break, O(log n), and O(1) extra space. The scan costs O(n).
lo, hi and mid, so the extra space is O(1).def rotation_start(values):
n = len(values)
# [1] The window [lo, hi): positions before lo hold ids above the last id, positions from hi on hold ids at or below it, and position n counts as at or below.
lo = 0
hi = n
# [2] While the window holds a position, look at the id in its middle.
while lo < hi:
mid = lo + (hi - lo) // 2
if values[mid] > values[n - 1]:
# [3] An id above the last id is in the first run, so it and every id before it is ruled out: lo moves past it.
lo = mid + 1
else:
# [4] An id at or below the last id is in the second run, so it may be the first of it: hi moves to it.
hi = mid
# [5] The window is empty: lo is where the second run starts, which holds the smallest id. It is 0 when the list is not rotated.
return lopublic int rotationStart(int[] values) {
int n = values.length;
// [1] The window [lo, hi): positions before lo hold ids above the last id, positions from hi on hold ids at or below it, and position n counts as at or below.
int lo = 0;
int hi = n;
// [2] While the window holds a position, look at the id in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] > values[n - 1]) {
// [3] An id above the last id is in the first run, so it and every id before it is ruled out: lo moves past it.
lo = mid + 1;
} else {
// [4] An id at or below the last id is in the second run, so it may be the first of it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is where the second run starts, which holds the smallest id. It is 0 when the list is not rotated.
return lo;
}int rotationStart(vector<int>& values) {
int n = (int)values.size();
// [1] The window [lo, hi): positions before lo hold ids above the last id, positions from hi on hold ids at or below it, and position n counts as at or below.
int lo = 0;
int hi = n;
// [2] While the window holds a position, look at the id in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] > values[n - 1]) {
// [3] An id above the last id is in the first run, so it and every id before it is ruled out: lo moves past it.
lo = mid + 1;
} else {
// [4] An id at or below the last id is in the second run, so it may be the first of it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is where the second run starts, which holds the smallest id. It is 0 when the list is not rotated.
return lo;
}function rotationStart(values) {
const n = values.length;
// [1] The window [lo, hi): positions before lo hold ids above the last id, positions from hi on hold ids at or below it, and position n counts as at or below.
let lo = 0;
let hi = n;
// [2] While the window holds a position, look at the id in its middle.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (values[mid] > values[n - 1]) {
// [3] An id above the last id is in the first run, so it and every id before it is ruled out: lo moves past it.
lo = mid + 1;
} else {
// [4] An id at or below the last id is in the second run, so it may be the first of it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty: lo is where the second run starts, which holds the smallest id. It is 0 when the list is not rotated.
return lo;
}def search_rotated(values, x):
n = len(values)
# [1] An empty list holds nothing.
if n == 0:
return -1
# [2] Find where the second run starts, with the first-true search from the break template.
lo = 0
hi = n
while lo < hi:
mid = lo + (hi - lo) // 2
if values[mid] > values[n - 1]:
lo = mid + 1
else:
hi = mid
start = lo
# [3] An id above the last id can only be in the first run, positions 0 to start - 1. Any other id can only be in the second run.
if x > values[n - 1]:
lo = 0
hi = start
else:
lo = start
hi = n
end = hi
# [4] Inside that run, find the first id that is not below x, as in the lower bound.
while lo < hi:
mid = lo + (hi - lo) // 2
if values[mid] < x:
lo = mid + 1
else:
hi = mid
# [5] The id is present only if the search stopped inside the run, on an id equal to x.
if lo < end and values[lo] == x:
return lo
return -1public int searchRotated(int[] values, int x) {
int n = values.length;
// [1] An empty list holds nothing.
if (n == 0) {
return -1;
}
// [2] Find where the second run starts, with the first-true search from the break template.
int lo = 0;
int hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] > values[n - 1]) {
lo = mid + 1;
} else {
hi = mid;
}
}
int start = lo;
// [3] An id above the last id can only be in the first run, positions 0 to start - 1. Any other id can only be in the second run.
if (x > values[n - 1]) {
lo = 0;
hi = start;
} else {
lo = start;
hi = n;
}
int end = hi;
// [4] Inside that run, find the first id that is not below x, as in the lower bound.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] < x) {
lo = mid + 1;
} else {
hi = mid;
}
}
// [5] The id is present only if the search stopped inside the run, on an id equal to x.
if (lo < end && values[lo] == x) {
return lo;
}
return -1;
}int searchRotated(vector<int>& values, int x) {
int n = (int)values.size();
// [1] An empty list holds nothing.
if (n == 0) {
return -1;
}
// [2] Find where the second run starts, with the first-true search from the break template.
int lo = 0;
int hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] > values[n - 1]) {
lo = mid + 1;
} else {
hi = mid;
}
}
int start = lo;
// [3] An id above the last id can only be in the first run, positions 0 to start - 1. Any other id can only be in the second run.
if (x > values[n - 1]) {
lo = 0;
hi = start;
} else {
lo = start;
hi = n;
}
int end = hi;
// [4] Inside that run, find the first id that is not below x, as in the lower bound.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] < x) {
lo = mid + 1;
} else {
hi = mid;
}
}
// [5] The id is present only if the search stopped inside the run, on an id equal to x.
if (lo < end && values[lo] == x) {
return lo;
}
return -1;
}function searchRotated(values, x) {
const n = values.length;
// [1] An empty list holds nothing.
if (n === 0) {
return -1;
}
// [2] Find where the second run starts, with the first-true search from the break template.
let lo = 0;
let hi = n;
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (values[mid] > values[n - 1]) {
lo = mid + 1;
} else {
hi = mid;
}
}
const start = lo;
// [3] An id above the last id can only be in the first run, positions 0 to start - 1. Any other id can only be in the second run.
if (x > values[n - 1]) {
lo = 0;
hi = start;
} else {
lo = start;
hi = n;
}
const end = hi;
// [4] Inside that run, find the first id that is not below x, as in the lower bound.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (values[mid] < x) {
lo = mid + 1;
} else {
hi = mid;
}
}
// [5] The id is present only if the search stopped inside the run, on an id equal to x.
if (lo < end && values[lo] === x) {
return lo;
}
return -1;
}def find_peak(values):
# [1] The window [lo, hi): the list climbs into lo, and hi is higher than the position after it, or is the last position, which is never compared.
lo = 0
hi = len(values) - 1
# [2] While the window holds a position, look at its middle and the position after it. The middle is below hi, so the position after it exists.
while lo < hi:
mid = lo + (hi - lo) // 2
if values[mid] < values[mid + 1]:
# [3] Still climbing: a peak lies after mid, so lo moves past it.
lo = mid + 1
else:
# [4] Falling: mid may be a peak, so keep it: hi moves to it.
hi = mid
# [5] The window is empty, so lo == hi, and that position is a peak.
return lopublic int findPeak(int[] values) {
// [1] The window [lo, hi): the list climbs into lo, and hi is higher than the position after it, or is the last position, which is never compared.
int lo = 0;
int hi = values.length - 1;
// [2] While the window holds a position, look at its middle and the position after it. The middle is below hi, so the position after it exists.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] < values[mid + 1]) {
// [3] Still climbing: a peak lies after mid, so lo moves past it.
lo = mid + 1;
} else {
// [4] Falling: mid may be a peak, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty, so lo == hi, and that position is a peak.
return lo;
}int findPeak(vector<int>& values) {
// [1] The window [lo, hi): the list climbs into lo, and hi is higher than the position after it, or is the last position, which is never compared.
int lo = 0;
int hi = (int)values.size() - 1;
// [2] While the window holds a position, look at its middle and the position after it. The middle is below hi, so the position after it exists.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (values[mid] < values[mid + 1]) {
// [3] Still climbing: a peak lies after mid, so lo moves past it.
lo = mid + 1;
} else {
// [4] Falling: mid may be a peak, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty, so lo == hi, and that position is a peak.
return lo;
}function findPeak(values) {
// [1] The window [lo, hi): the list climbs into lo, and hi is higher than the position after it, or is the last position, which is never compared.
let lo = 0;
let hi = values.length - 1;
// [2] While the window holds a position, look at its middle and the position after it. The middle is below hi, so the position after it exists.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (values[mid] < values[mid + 1]) {
// [3] Still climbing: a peak lies after mid, so lo moves past it.
lo = mid + 1;
} else {
// [4] Falling: mid may be a peak, so keep it: hi moves to it.
hi = mid;
}
}
// [5] The window is empty, so lo == hi, and that position is a peak.
return lo;
}This version runs the lower bound from the last lesson on the whole ring. It assumes that the list is sorted.
This version tests values[mid] >= values[0] to decide that mid is in the first run. It works on a rotated list, but on a list that was never rotated every id is at least the first id.
This version uses Template 1 on a list with repeats. The test values[mid] > values[n - 1] says that no id is in the first run when the first run holds ids equal to the last id.
This version runs the peak search on a list that has equal neighbours. When the middle equals the next id, the test values[mid] < values[mid + 1] is false, so the search moves hi to mid.
Your team lead hands you a list that was merged from several sorted pieces: [5, 6, 1, 2, 7, 8, 3, 4]. It has three sorted runs and two breaks. You search for the id 2, which is at position 3.
systemdesignmastery.dev/dsa/cheatsheets/rotated-arrays-and-peaks