Explain why comparison sorts need about n log n comparisons.
After k comparisons a method has at most 2^k endings, and sorting n items needs n! of them, so 2^k must reach n!.
Use it when you must put items in order, or answer a question about their order, and the only tool is comparing two items. Signal words: the fewest comparisons any method needs, n factorial orders, two answers for each comparison.
Start: count the answers the method must tell apart. For a full sort that is n!.
Step: each comparison at most doubles the endings, so k comparisons give at most 2^k endings. Find the smallest k with 2^k at least the number of answers.
Stop: the lower bound is that k. A method that uses about k comparisons is nearly the best possible.
It does not limit a method that never compares items, such as counting sort. It is a worst-case bound, so one lucky input can need far fewer comparisons.
Cost: the floor is log2(n!) rounded up, which is about n log2 n − 1.44 n. It grows like n log n, O(n log n).
def comparison_floor(n):
# [1] Count the orders: n! of them, which a 64-bit integer holds up to n = 20.
orders = 1
for i in range(2, n + 1):
orders *= i
# [2] With no comparison there is one ending.
endings = 1
k = 0
# [3] Each comparison at most doubles the endings: stop when every order can have its own.
while endings < orders:
endings *= 2
k += 1
# [4] No method can use fewer than k comparisons in the worst case.
return kpublic int comparisonFloor(int n) {
// [1] Count the orders: n! of them, which a 64-bit integer holds up to n = 20.
long orders = 1;
for (int i = 2; i <= n; i++) {
orders *= i;
}
// [2] With no comparison there is one ending.
long endings = 1;
int k = 0;
// [3] Each comparison at most doubles the endings: stop when every order can have its own.
while (endings < orders) {
endings *= 2;
k++;
}
// [4] No method can use fewer than k comparisons in the worst case.
return k;
}int comparisonFloor(int n) {
// [1] Count the orders: n! of them, which a 64-bit integer holds up to n = 20.
long long orders = 1;
for (int i = 2; i <= n; i++) {
orders *= i;
}
// [2] With no comparison there is one ending.
long long endings = 1;
int k = 0;
// [3] Each comparison at most doubles the endings: stop when every order can have its own.
while (endings < orders) {
endings *= 2;
k++;
}
// [4] No method can use fewer than k comparisons in the worst case.
return k;
}function comparisonFloor(n) {
// [1] Count the orders: n! of them, which a 64-bit integer holds up to n = 20.
let orders = 1n;
for (let i = 2; i <= n; i++) {
orders *= BigInt(i);
}
// [2] With no comparison there is one ending.
let endings = 1n;
let k = 0;
// [3] Each comparison at most doubles the endings: stop when every order can have its own.
while (endings < orders) {
endings *= 2n;
k++;
}
// [4] No method can use fewer than k comparisons in the worst case.
return k;
}def binary_insertion_count(values):
# [1] Work on a copy; the front part of it is always in order.
ranked = list(values)
count = 0
# [2] Take each item in turn and search the ranked front part for its place.
for i in range(1, len(ranked)):
x = ranked[i]
lo = 0
hi = i
# [3] Compare with the middle of the window: one comparison, and half the window is ruled out.
while lo < hi:
mid = lo + (hi - lo) // 2
count += 1
if ranked[mid] < x:
lo = mid + 1
else:
hi = mid
# [4] Shift the larger items right and put x in its place.
j = i
while j > lo:
ranked[j] = ranked[j - 1]
j -= 1
ranked[lo] = x
# [5] Return the comparisons used.
return countpublic int binaryInsertionCount(int[] values) {
// [1] Work on a copy; the front part of it is always in order.
int[] ranked = values.clone();
int count = 0;
// [2] Take each item in turn and search the ranked front part for its place.
for (int i = 1; i < ranked.length; i++) {
int x = ranked[i];
int lo = 0;
int hi = i;
// [3] Compare with the middle of the window: one comparison, and half the window is ruled out.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
count++;
if (ranked[mid] < x) {
lo = mid + 1;
} else {
hi = mid;
}
}
// [4] Shift the larger items right and put x in its place.
int j = i;
while (j > lo) {
ranked[j] = ranked[j - 1];
j--;
}
ranked[lo] = x;
}
// [5] Return the comparisons used.
return count;
}int binaryInsertionCount(vector<int>& values) {
// [1] Work on a copy; the front part of it is always in order.
vector<int> ranked = values;
int count = 0;
// [2] Take each item in turn and search the ranked front part for its place.
for (int i = 1; i < (int)ranked.size(); i++) {
int x = ranked[i];
int lo = 0;
int hi = i;
// [3] Compare with the middle of the window: one comparison, and half the window is ruled out.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
count++;
if (ranked[mid] < x) {
lo = mid + 1;
} else {
hi = mid;
}
}
// [4] Shift the larger items right and put x in its place.
int j = i;
while (j > lo) {
ranked[j] = ranked[j - 1];
j--;
}
ranked[lo] = x;
}
// [5] Return the comparisons used.
return count;
}function binaryInsertionCount(values) {
// [1] Work on a copy; the front part of it is always in order.
const ranked = [...values];
let count = 0;
// [2] Take each item in turn and search the ranked front part for its place.
for (let i = 1; i < ranked.length; i++) {
const x = ranked[i];
let lo = 0;
let hi = i;
// [3] Compare with the middle of the window: one comparison, and half the window is ruled out.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
count++;
if (ranked[mid] < x) {
lo = mid + 1;
} else {
hi = mid;
}
}
// [4] Shift the larger items right and put x in its place.
let j = i;
while (j > lo) {
ranked[j] = ranked[j - 1];
j--;
}
ranked[lo] = x;
}
// [5] Return the comparisons used.
return count;
}This version takes log2(n!) and drops the fraction. For three photos, log2(6) is 2.585, so it says 2 comparisons are enough.
This version says "the floor for 1,000 photos is 8,530, so ranking any 1,000 photos takes at least 8,530 comparisons".
This version computes the natural logarithm of n!, not the base-2 logarithm. Each comparison has two answers, so the base is 2.
This version holds n! in a 32-bit integer, which Java and C++ use by default for int. The product passes 2,147,483,647 at n = 13.
A rewards screen in a payments app sorts 1,000,000 user ages, each a whole number from 0 to 120. The comparison floor for a million items is about 18,488,885 comparisons.
systemdesignmastery.dev/dsa/cheatsheets/comparison-model