Tell which case a bound describes and which one an interviewer is asking for.
A bound promises a stated set of inputs: the best case is the cheapest input, the worst is the dearest, and an average needs its distribution stated.
Use it when the cost of code depends on the input, or an interviewer asks how fast something is. Signal words: on average, worst case, already sorted, stops at the first match.
Start: pick one input size n and one step to count, such as a comparison.
Step: find the input that makes the count smallest and the input that makes it largest, and run the code on both.
Stop: report the worst case with its input, and the best case if it helps. State the distribution before you give an average.
It does not pick the case for you. A latency promise needs the worst case, and a batch job that runs a million times may care about the average.
Cost: one extra run per case. The counts are exact for the inputs you ran.
def comparisons_to_find(items, target):
# [1] Start with no comparisons made.
count = 0
for x in items:
# [2] Compare the next id with the target, and count it.
count += 1
# [3] Stop at the first match: the rest of the list is never read.
if x == target:
break
# [4] Return the number of comparisons.
return countpublic int comparisonsToFind(int[] items, int target) {
// [1] Start with no comparisons made.
int count = 0;
for (int x : items) {
// [2] Compare the next id with the target, and count it.
count++;
// [3] Stop at the first match: the rest of the list is never read.
if (x == target) {
break;
}
}
// [4] Return the number of comparisons.
return count;
}int comparisonsToFind(vector<int>& items, int target) {
// [1] Start with no comparisons made.
int count = 0;
for (int x : items) {
// [2] Compare the next id with the target, and count it.
count++;
// [3] Stop at the first match: the rest of the list is never read.
if (x == target) {
break;
}
}
// [4] Return the number of comparisons.
return count;
}function comparisonsToFind(items, target) {
// [1] Start with no comparisons made.
let count = 0;
for (const x of items) {
// [2] Compare the next id with the target, and count it.
count++;
// [3] Stop at the first match: the rest of the list is never read.
if (x === target) {
break;
}
}
// [4] Return the number of comparisons.
return count;
}def insertion_comparisons(values):
# [1] Work on a copy, and start with no comparisons made.
a = list(values)
comparisons = 0
# [2] Take each item from the second on, and move it left into place.
for i in range(1, len(a)):
j = i
while j > 0:
# [3] One comparison: is the item before the new one larger?
comparisons += 1
if a[j - 1] > a[j]:
a[j - 1], a[j] = a[j], a[j - 1]
j -= 1
else:
# [4] Early exit: the item is in place, so stop moving it.
break
# [5] Return the number of comparisons.
return comparisonspublic long insertionComparisons(int[] values) {
// [1] Work on a copy, and start with no comparisons made.
int[] a = values.clone();
long comparisons = 0;
// [2] Take each item from the second on, and move it left into place.
for (int i = 1; i < a.length; i++) {
int j = i;
while (j > 0) {
// [3] One comparison: is the item before the new one larger?
comparisons++;
if (a[j - 1] > a[j]) {
int t = a[j - 1];
a[j - 1] = a[j];
a[j] = t;
j--;
} else {
// [4] Early exit: the item is in place, so stop moving it.
break;
}
}
}
// [5] Return the number of comparisons.
return comparisons;
}long long insertionComparisons(vector<int>& values) {
// [1] Work on a copy, and start with no comparisons made.
vector<int> a = values;
long long comparisons = 0;
// [2] Take each item from the second on, and move it left into place.
for (size_t i = 1; i < a.size(); i++) {
size_t j = i;
while (j > 0) {
// [3] One comparison: is the item before the new one larger?
comparisons++;
if (a[j - 1] > a[j]) {
swap(a[j - 1], a[j]);
j--;
} else {
// [4] Early exit: the item is in place, so stop moving it.
break;
}
}
}
// [5] Return the number of comparisons.
return comparisons;
}function insertionComparisons(values) {
// [1] Work on a copy, and start with no comparisons made.
const a = values.slice();
let comparisons = 0;
// [2] Take each item from the second on, and move it left into place.
for (let i = 1; i < a.length; i++) {
let j = i;
while (j > 0) {
// [3] One comparison: is the item before the new one larger?
comparisons++;
if (a[j - 1] > a[j]) {
const t = a[j - 1];
a[j - 1] = a[j];
a[j] = t;
j--;
} else {
// [4] Early exit: the item is in place, so stop moving it.
break;
}
}
}
// [5] Return the number of comparisons.
return comparisons;
}This version says insertion sort on 1,000 items makes about 499,500 comparisons, because that is what the textbook worst case gives.
This version says a linear lookup costs O(1), because it found the first id with one comparison.
This version says the average lookup in 1,000 ids is 500.5 comparisons and stops there.
This version says a duplicate check with an early exit is fast, because it stops when it finds a pair.
Summing all items of a list reads every item, whatever the order. Its best, average and worst cases are all n steps, so there is nothing to separate.
systemdesignmastery.dev/dsa/cheatsheets/best-average-worst