How do you calculate the median?

Short answer. Sort the values from smallest to largest and take the middle one. For 3, 9, 4, 7, 12 the sorted list is 3, 4, 7, 9, 12, so the median is 7. With an even count there are two middle values, so average them: 2, 5, 8, 11 gives (5 + 8) ÷ 2 = 6.5.

The median needs no formula, only a sort and a count. That is also why people get it wrong: it feels too easy to need a method, so they skip the sort, or count the wrong thing, or forget what to do when there is no single middle.

The method

Three steps, in this order. Sort. Count. Take the middle.

Seven response times in milliseconds, as they came in: 14, 22, 9, 31, 18, 27, 12.

step 1  sort, smallest to largest
        9, 12, 14, 18, 22, 27, 31

step 2  count
        seven values, so n = 7

step 3  take the middle
        position (7 + 1) ÷ 2 = 4
        the 4th value is 18

        9, 12, 14, 18, 22, 27, 31
        three below, three above

The median is 18. The mean of the same seven numbers is 133 ÷ 7 = 19, which is close, because nothing in this list is wildly out of line with the rest. Keep that pair in mind for later.

Even count: two middles

Add an eighth reading of 45 and the list no longer has a single middle entry:

sorted  9, 12, 14, 18, 22, 27, 31, 45
count   n = 8
middle  position (8 + 1) ÷ 2 = 4.5, between the 4th and 5th
        4th = 18,  5th = 22

median  (18 + 22) ÷ 2  =  40 ÷ 2  =  20

The rule for an even count is to average the two values that straddle the middle. The result need not be a member of the list, and here it is not. If the two middle values happen to be equal, the averaging is trivial: 3, 7, 7, 9 has a median of 7.

For the record, the mean of the eight readings is 178 ÷ 8 = 22.25. One slow reading pulled the mean up from 19 to 22.25, and the median from 18 to 20. That is the first hint of the thing the median is actually for.

The position formula

For long lists, counting to the middle by hand is where mistakes creep in. The median sits at position (n + 1) ÷ 2 in the sorted list. A whole number means take that value. A half means average the values either side of it.

Count n(n + 1) ÷ 2Median is
74the 4th value
84.5average of the 4th and 5th
2513the 13th value
10050.5average of the 50th and 51st
1,000500.5average of the 500th and 501st

Note that for 100 values the median is not the 50th value. It is halfway between the 50th and the 51st, so that exactly 50 values sit on each side.

Three ways to get it wrong

Not sorting. The median is the middle of the sorted list, not the middle of the list as written. Take 3, 9, 4, 7, 12:

as typed   3, 9, 4, 7, 12       middle entry is 4     wrong
sorted     3, 4, 7, 9, 12       median is 7

Counting distinct values. Duplicates are separate entries and each one takes up a position. For 5, 5, 5, 6, 9 the count is five, the 3rd value is 5, and the median is 5. Collapsing the list to 5, 6, 9 and answering 6 is wrong.

Dropping zeros. A zero is a value like any other. For 0, 0, 3, 8, 10 the median is 3. Throw the zeros away and you get 8, which describes a different set of data.

Negative numbers sort below zero, not by size. For −4, −1, 0, 3 the middle pair is −1 and 0, so the median is (−1 + 0) ÷ 2 = −0.5.

What the median is for

The mean and the median both get called the average, and the average post covers the mean in full. The short version of the difference: the mean is dragged by every value, and the median is not. Six houses sell on one street:

240,000   255,000   262,000   270,000   290,000   1,900,000

mean     3,217,000 ÷ 6             =  536,166.67
median   (262,000 + 270,000) ÷ 2   =  266,000

Five of the six houses cost less than the mean. Nobody on this street paid anything like 536,000. The one large sale lifts the mean by more than a quarter of a million and leaves the median exactly where the typical house is.

Now suppose that big house had sold for 5,000,000 instead:

mean     6,317,000 ÷ 6  =  1,052,833.33
median   still (262,000 + 270,000) ÷ 2  =  266,000

The median did not move at all. It only cares that the big value is above the middle, not how far above. This is why house prices, incomes, response times and view counts are reported as medians: the data has a long tail on one side, and the mean reports the tail rather than the typical case.

When the data is symmetric the two agree. For 2, 4, 6, 8, 10 the mean is 30 ÷ 5 = 6 and the median is 6. A gap between them is a sign the set leans one way, and the direction of the gap tells you which way.

Median from a frequency table

When values arrive as counts rather than a list, do not expand the list. Find the middle position, then walk the running total until you reach it. Fourteen pairs of shoes sold in one afternoon:

size   sold   running total
 7      3      3           positions 1 to 3
 8      5      8           positions 4 to 8
 9      4     12           positions 9 to 12
10      2     14           positions 13 to 14

n = 14, median at position (14 + 1) ÷ 2 = 7.5
the 7th and 8th values are both size 8

median  8

The mean size is (7 × 3 + 8 × 5 + 9 × 4 + 10 × 2) ÷ 14 = 117 ÷ 14 = 8.357142..., which is a weighted average and also not a shoe size anyone can buy. The median, 8, is a size the shop can restock.

Small cases

The median of two numbers is their mean: for 10 and 14 it is (10 + 14) ÷ 2 = 12. The median of one number is that number. The median of an empty list does not exist, which spreadsheets handle by returning an error and some calculators handle by returning zero, which is worse.

The halving that is not exact

An even-count median ends with a division by 2, and that is one more place for a calculator to lose the plot. Most software stores decimals in binary floating point, where 0.1 and 0.2 have no exact form. Average them and the error shows up in the answer. These are real results from JavaScript and Python:

(0.1 + 0.2) ÷ 2   =  0.15000000000000002    should be 0.15
(1.1 + 2.2) ÷ 2   =  1.6500000000000001     should be 1.65
(2.3 + 2.4) ÷ 2   =  2.3499999999999996     should be 2.35

The median of 0.1 and 0.2 is 0.15. Not 0.15 and a few quadrillionths, and not something a program will refuse to call equal to 0.15. It is the same fault that makes 0.1 + 0.2 miss 0.3. Prism does the arithmetic on exact decimals, so (0.1 + 0.2) ÷ 2 comes back as 0.15, and 0.1 + 0.2 is exactly 0.3, with nothing rounded on the way through.

Prism Calculator icon

Sort on paper, halve in Prism

Type the two middle values, divide by 2, and read 0.15, not 0.15000000000000002. Every entry stays in a searchable history, so you can scroll back and check the list you sorted against the list you typed. History, percentages and fractions are free forever, no ads, no subscription. See the app.