Number of comparisons made in median of 3 function?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To find the median of three values, you need at most three comparisons. That is the standard worst-case answer. But the full story is slightly more interesting: some input orders can finish in two comparisons, while the worst case still requires three.
Why Three Is the Worst-Case Minimum
There are six possible orderings of three distinct elements. A comparison-based decision process must distinguish enough cases to identify the middle value.
After one comparison, you only know the order of one pair. After two comparisons, some branches still leave two possible medians. So in the general case, a third comparison is necessary.
That means:
- best case:
2comparisons - worst case:
3comparisons
An optimal comparison tree achieves exactly that.
A Simple Median-of-Three Implementation
For (1, 2, 3), it returns after two comparisons. For (1, 3, 2), it needs three.
Why Some Cases Need Only Two
Suppose you first compare a and b, then compare b and c.
If you learn:
- '
a < b' - '
b < c'
then the order is already a < b < c, so b is clearly the median after only two comparisons.
But if you learn:
- '
a < b' - '
c < b'
you still do not know whether the order is a < c < b or c < a < b. A third comparison between a and c is needed.
Average Number of Comparisons
If all six orderings are equally likely and you use an optimal decision tree, four cases finish in three comparisons and two cases finish in two comparisons.
So the average is:
(4 * 3 + 2 * 2) / 6 = 8 / 3
That is about 2.67 comparisons on average.
In Quicksort Context
When people talk about "median of three" in quicksort, they often mean choosing the pivot from the first, middle, and last elements. The pivot-selection step itself has the comparison cost described above, but the total sort cost also includes:
- the partition pass
- recursive calls
- swaps or moves
So saying "median of three uses three comparisons" is correct only for the pivot-selection subroutine in the worst case, not for the whole partitioning process.
Common Pitfalls
The biggest mistake is saying the median of three always uses exactly three comparisons. That is only the worst-case count.
Another mistake is confusing median selection with full sorting of three elements. Sorting all three also takes up to three comparisons, but the goal is different.
A third issue is forgetting that equal elements can change branch behavior in a concrete implementation, even though the classic analysis usually assumes distinct keys.
Summary
- Finding the median of three values takes at most three comparisons.
- Some input orders finish in only two comparisons.
- The worst-case minimum is still three comparisons.
- The average comparison count for an optimal method is
8/3. - In quicksort, this cost applies only to pivot selection, not to the whole sort.
Related reading
- Number of Increasing Subsequences of length k
- Number of largest element exchanges for quicksort
- Number of sub-sequences in a given sequence
- Number of subarrays divisible by k
- Number of substrings in range l, r that can be permuted to palindrome
- Number of ways of correctly arranging parenthesis
- Number of ways to make change for amount N
- Numpy argsort - what is it doing?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.