Calculate median in c
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To calculate a median in C#, you usually sort the data first and then take either the middle value or the average of the two middle values. The details matter, though: you need to decide what to do with empty input, whether to mutate the original collection, and whether integer input should still produce a fractional median.
The Definition of Median
For a sorted sequence:
- if the count is odd, the median is the middle value
- if the count is even, the median is the average of the two middle values
Examples:
- '
[1, 3, 5]has median3' - '
[1, 3, 5, 7]has median4'
That definition is simple, but it assumes the data is ordered. Sorting is therefore the usual first step.
A Simple C# Implementation
Here is a reusable method that accepts any numeric sequence convertible to double and returns a double median.
This version is clear, safe, and appropriate for ordinary application code.
Why Returning double Is Usually Best
Even if the input values are integers, an even-length median may not be an integer. For example, [1, 2, 100, 101] has a median of 51.
That is why returning double is usually the least surprising API. If you keep everything as int, you risk losing the fractional part through integer division.
For example, this is wrong for even counts:
Using 2.0 or a floating-point return type avoids that trap.
Avoid Mutating the Original Data by Accident
If the caller passes a List or array and you sort it in place, you may change the caller's data unexpectedly. That may be fine in some code, but it should be a conscious choice.
The OrderBy(...).ToArray() pattern copies the data into a sorted array, leaving the original sequence unchanged.
If mutation is acceptable and performance is critical, an in-place sort can reduce extra allocations:
Just be clear about which behavior your method promises.
What About Very Large Datasets
Sorting is O(n log n), which is perfectly reasonable for many use cases. But if you need the median of a huge dataset and do not need a full sort, there are selection algorithms that can find the median in linear time on average.
That said, the fully sorted approach is still the most practical default in ordinary business or analytics code because it is simple and reliable.
For streaming data, the problem changes again. In that case, a common solution is maintaining two heaps so the median can be updated incrementally.
A Generic Convenience Overload
If your code often deals with integers, a small overload can keep call sites cleaner.
That lets you call the same conceptual API while still getting correct fractional results.
Common Pitfalls
The biggest mistake is forgetting to sort the data before taking the middle element. Median depends on order, not insertion sequence.
Another mistake is using integer division for even-length collections. That silently truncates correct results.
People also forget to handle the empty-sequence case. Returning 0 silently is usually worse than throwing a clear error.
Finally, decide whether your method is allowed to mutate the caller's collection. Hidden in-place sorting can cause bugs far away from the median calculation itself.
Summary
- Median requires sorted data.
- Odd counts use the middle value; even counts use the average of the two middle values.
- Returning
doubleis usually the safest API, even for integer inputs. - Be explicit about how empty input is handled.
- Avoid accidental in-place sorting unless the caller expects mutation.

