median
algorithms
data structures
arrays
computational efficiency

Keeping track of the median of an expanding array

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In the world of statistics and data analysis, the median is a crucial measure that helps summarize a dataset by identifying the middle value when the data points are arranged in order. Tracking the median efficiently becomes an important task when dealing with an expanding array, especially in real-time applications like live data feeds or streaming data. This article explores various techniques and data structures useful for maintaining the median in a dynamically growing array, providing both theoretical insights and practical examples.

The Median Problem

When a dataset is static, finding the median involves sorting the array and picking the middle element(s). However, when new data points are added continuously, recalculating the median by sorting the entire array repeatedly becomes inefficient, especially for large datasets.

Maintaining the median in an expanding array requires efficient data structures and algorithms that can dynamically adjust and quickly identify the median after each insertion.

Key Data Structures

1. Two Heaps Method

One of the most efficient ways to maintain the median in an expanding dataset is using two heaps: • Max Heap: Keeps track of the lower half of the data. • Min Heap: Keeps track of the upper half of the data.

Working Principle

  1. When a new number is added, decide which half it should belong to, and add it to the respective heap.
  2. Ensure the size property is maintained, where the max heap can be equal in size or have one more element than the min heap.
  3. If necessary, balance the heaps by moving the top element from one heap to the other.
  4. The median will either be the top of the max heap (for odd total elements) or the average of the tops of both heaps (for even total elements).

Complexity

• Insertion: O(logn)O(\log n) • Finding median: O(1)O(1)

Example

Consider the sequence: 1, 2, 3, 4, 5

• Insert 1: Max Heap = [1], Min Heap = [] • Median is 1

• Insert 2: Max Heap = [1], Min Heap = [2] • Median is (1 + 2)/2 = 1.5

• Insert 3: Max Heap = [2, 1], Min Heap = [3] • Median is 2

• Insert 4: Max Heap = [2, 1], Min Heap = [3, 4] • Median is (2 + 3)/2 = 2.5

• Insert 5: Max Heap = [3, 1, 2], Min Heap = [4, 5] • Median is 3

2. Balanced BST (Binary Search Tree)

Using a balanced binary search tree like AVL or Red-Black Tree, you can maintain a dynamically sorted dataset, which allows for efficient retrieval and insertion.

Working Principle

  1. Insertions keep the tree balanced.
  2. An in-order traversal gives a sorted list of elements.
  3. Median can be accessed by finding the n/2th⌊n/2⌋^{th} element for odd `n` or averaging the middle two elements for even `n`.

Complexity

• Insertion: O(logn)O(\log n) • Finding median: O(n)O(n) (due to traversal)

Comparison Table

MethodInsertion ComplexityFinding Median ComplexityReal-time CapabilityUse Case Scenarios
Two HeapsO(logn)O(\log n)O(1)O(1)Highly SuitableContinuous data streams
Balanced BSTO(logn)O(\log n)O(n)O(n)Moderately SuitableFixed, in-order access

Considerations & Challenges

Data Range: When the data has a large range, heaps are more advantageous due to their logarithmic insertion complexity.

Frequency of Queries: For frequent median queries, the two heaps method is superior. However, for datasets where order is critical, a balanced BST may be preferred despite its higher median retrieval cost.

Memory Use: Heaps use additional space to store data redundantly (both as nodes and pointers), whereas BSTs use space more efficiently with each node directly correlating to an element.

Conclusion

In the modern age of large-scale data, efficiently maintaining statistical measures like the median in expanding arrays enables better real-time analysis and decision-making. By leveraging robust data structures such as heaps and balanced trees, one can achieve both efficiency and scalability, tailoring the approach to the specific needs and constraints of the dataset in question.

Developers and data scientists must evaluate their use-case scenarios to choose an optimal data structure, keeping in mind the trade-offs of time complexity, memory usage, and real-time capabilities.


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.