🎯 Median Calculator

📐 工作原理與說明
Median (page-code conventions): (1) individual samples are separated by commas, whitespace or semicolons; after sorting, odd n takes position (n+1)/2 and even n averages the two middle values. (2) Frequency mode enters one "value, frequency" pair per line; equal values are merged, then the half position is found by cumulative frequency. If total frequency is even and the half position falls exactly between two distinct values, their average is used, matching the expanded-sample convention. (3) Weighted mode enters one "value, weight" pair per line with decimal weights allowed; the half position uses cumulative weight, averaging adjacent values when it falls exactly on a boundary. All-zero frequencies/weights, negative weights, lines without exactly two columns or non-numeric entries return a line-specific error. There is no fixed grading; the output shows the cumulative table, half position and convention note.
The median is the middle value after sorting: for odd n take position (n+1)/2, for even n average the two middle values. Frequency/weighted modes locate the half position by cumulative frequency or cumulative weight, averaging adjacent values when the half position falls exactly on a boundary. Unlike the mean, it is not pulled by extreme values.
Results keep at most 3 decimals; nonzero values below 0.001 keep 6 significant digits, and absolute values below 1e-6 use scientific notation. For even samples each middle value is halved before summing to avoid overflow.

Sort a list of numbers and return the median, resistant to outliers, for skewed data such as scores, salaries and house prices.

將資料排序後取中間值(偶數個取中間兩個的平均)

📚 深度解析:中位數計算器

💡 常見使用場景

  • 排序取中間,抗極端值。
  • 收入、房價等偏態分佈資料中看典型水平。
計算
[1,3,5,7,9] 中位數=5;[1,2,3,4] 中位數=(2+3)/2=2.5。
📖 定義與演算法
中位數是升序排列後位於中間的數:n 為奇數時取第 (n+1)/2 個(下標 (n−1)/2),n 為偶數時取第 n/2 與第 n/2+1 個的算術平均。頁面先把輸入按逗號/空格/換行/分號切分並過濾非數字,再排序、取位、給出完整步驟。顯示按 1e-10 四捨五入,絕對值小於 1e-12 的浮點殘差按 0 顯示。
🧮 算例(按本頁程式碼口徑實跑)
3, 1, 4, 1, 5, 9, 2, 6, 5(n=9):升序 1, 1, 2, 3, 4, 5, 5, 6, 9,中位數 = 第 5 個 = 4。 1, 2, 3, 4(n=4):取第 2、3 個,(2 + 3) / 2 = 5 / 2 = 2.5。 5, 3, 9, 1(n=4):升序 1, 3, 5, 9,(3 + 5) / 2 = 8 / 2 = 4——雖然原始順序混亂,排序後結果正確。 空輸入或全是非數字:頁面提示「請輸入資料」,不輸出 0。
🧭 為什麼用中位數而不是均值
對 1, 2, 3, 4, 100 這組資料,均值 = 110/5 = 22,中位數 = 3。一個極端值把均值拉高 7 倍多,中位數只挪到中間位置。收入、房價、響應時間這類長尾分佈因此更適合看中位數;但中位數不參與求和,不能拿來分攤總量。
⚠ 常見誤用
不排序直接取中間:輸入順序不代表大小順序,必須先升序。 偶數個忘記平均:n=4 時中位數是第 2、3 個的均值,不是任意一個。 把中位數當均值:中位數不滿足「各項與中心的偏差和為 0」,也不能用於預算平攤。

❓ 常見問題(FAQ)

偶數個怎麼辦?
取中間兩數平均。
中位數與均值差很大說明什麼?
說明分佈偏斜或存在離群值。差值方向告訴你長尾在哪一側:均值明顯大於中位數通常是右偏(少數極大值),反之是左偏。
重複值會影響結果嗎?
不會。位置只由 n 決定,重複值只是讓相鄰位置上的數相同;例如 1, 1, 2, 3, 4 的中位數仍是第 3 個 = 2。

Formula Reference

Item Formula / Note Source / Standard
MedianMean of the two middle values for even n; robust to skewStandard statistical definition
⚠️ 使用說明與注意事項