C#
median calculation
programming tutorial
data analysis
.NET

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 median 3'
  • '[1, 3, 5, 7] has median 4'

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.

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5public static class Statistics
6{
7    public static double Median(IEnumerable<double> values)
8    {
9        double[] sorted = values.OrderBy(x => x).ToArray();
10
11        if (sorted.Length == 0)
12        {
13            throw new InvalidOperationException("Cannot compute median of an empty sequence.");
14        }
15
16        int middle = sorted.Length / 2;
17
18        if (sorted.Length % 2 == 1)
19        {
20            return sorted[middle];
21        }
22
23        return (sorted[middle - 1] + sorted[middle]) / 2.0;
24    }
25}
26
27Console.WriteLine(Statistics.Median(new[] { 7.0, 1.0, 3.0 }));      // 3
28Console.WriteLine(Statistics.Median(new[] { 1.0, 3.0, 5.0, 7.0 })); // 4

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:

csharp
int wrong = (3 + 4) / 2; // 3, not 3.5

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:

csharp
1using System;
2
3int[] data = { 9, 2, 5, 1 };
4Array.Sort(data);

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.

csharp
1using System.Collections.Generic;
2using System.Linq;
3
4public static double Median(IEnumerable<int> values)
5{
6    return Statistics.Median(values.Select(x => (double)x));
7}

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 double is 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.

Course illustration
Course illustration

All Rights Reserved.