Getting median out of frequency table counting sort
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
In statistics and data analysis, finding the median is a common requirement, especially when dealing with frequency tables. A frequency table displays the frequencies of different outcomes in a sample. When dealing with large datasets, calculating the median using methods like sorting can become computationally expensive. An efficient way to find the median from frequency data is by using counting sort principles. This article will delve into how to achieve this, supported by technical explanations and examples.
Understanding Frequency Tables
A frequency table typically consists of two columns: the distinct values (or intervals) of the dataset and their respective frequencies. Consider a simple dataset representing the scores of students in a test between 1 and 5:
Score | Frequency |
| 1 | 4 |
| 2 | 2 |
| 3 | 5 |
| 4 | 3 |
| 5 | 1 |
The task is to find the median of this dataset efficiently.
Median in Statistics
The median is the middle value of a dataset when it is ordered. For a dataset with an odd number of elements, the median is the center element, whereas for an even number of elements, it is the average of the two center elements.
Procedure to Find the Median Using Counting Sort
Counting sort is a sorting technique based on keys between specific ranges. It counts the occurrence of each key and uses this count to position the keys in a sorted order. Here is a step-by-step process to compute the median from a frequency table:
- Calculate the Total Number of Observations:
Sum all the frequencies to get the total number of observations ().Example: - Determine the Median Position(s):
• If is odd, the median is at position . • If is even, the median positions are and .Example: Since (odd), the median is at position . - Create a Cumulative Frequency Table:
Calculate the cumulative frequency for each value.
| `Score` | Frequency | Cumulative Frequency | |||
| 1 | 4 | 4 | |||
| 2 | 2 | 6 | |||
| 3 | 5 | 11 | |||
| 4 | 3 | 14 | |||
| 5 | 1 | 15 | 4. Locate the Median Using Cumulative Frequency:\ Identify the smallest value for which the cumulative frequency meets or exceeds the median position. Example: • The 8th position falls within the cumulative frequency for score 3. Thus, the median score is 3. ## Special Considerations • Ties in Frequencies:\ If there are ties at median positions in an even-sized dataset, the median may not be a specific data value but rather the average of the two central tendencies. • Continuous Data:\ For grouped frequency distributions, interpolation might be necessary to estimate the median when data is continuous. ## Benefits of Using Counting Sort for Median • Efficiency: Counting sort allows median calculation without fully sorting the dataset, improving time complexity from (typical sorting) to . • Simplicity: The method is straightforward and easy to implement with simple arithmetic. ## Conclusion Counting sort principles simplify finding the median from frequency tables by efficiently using cumulative frequencies. This technique is especially valuable for large datasets or when the data is naturally segmented into discrete categories. Understanding the mechanics behind frequency tables and counting sort not only enhances computational efficiency but also deepens one's insight into data handling and statistics. To summarize, the process is broken down in the table below: | Step | Description |
| --- | --- | --- | --- | --- | --- |
| Calculate Total Observations | Sum all frequencies to determine the size of the dataset (). | ||||
| Determine Median Position(s) | Use to determine relevant median positions - whether odd or even. | ||||
| Create Cumulative Frequency | Compute cumulative frequencies to track position coverage. | ||||
| Locate the Median Value | Use cumulative frequencies to find the value meeting the desired positions. |
With the outlined approach, one can efficiently determine the median of a dataset structured as a frequency table, leveraging counting sort as an effective tool.
Related reading
- Getting No loop matching the specified signature and casting error
- getting the index of a row in a pandas apply function
- Ghost line in Tensorboard scalar plot
- GLM Warning message 'newdata' had 16623 rows but variables found have 22488 rows
- Getting parent of a vertex in a perfect binary tree
- Getting the closest string match
- Google Colaboratory local runtime using local GPU
- gradient descent using python and numpy

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.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.