question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Find median of an array of uints

See original GitHub issue

🧐 Motivation

We are building a reliable price oracle for a decentralized exchange (the DutchX). We would like to take the last n prices, and output the median price. So we need an algorithm that is able to find the median of an array of, say, uints.

📝 Details

A function that takes an array and returns its median. Signature:

function findMedian(uint[] memory array) public Pure Returns (uint)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Amxxcommented, Feb 15, 2021

Digging up this old topic just to propose an implementation:

function swap(int256[] memory array, uint256 i, uint256 j) internal pure {
    (array[i], array[j]) = (array[j], array[i]);
}
function sort(int256[] memory array, uint256 begin, uint256 end) internal pure {
    if (begin < end) {
    	uint256 j = begin;
    	int256 pivot = array[j];
    	for (uint256 i = begin + 1; i < end; ++i) {
    	    if (array[i] < pivot) {
    	        swap(array, i, ++j);
    	    }
    	}
    	swap(array, begin, j);
    	sort(array, begin, j);
    	sort(array, j + 1, end);
    }
}
function median(int256[] memory array, uint256 length) internal pure returns(int256) {
	sort(array, 0, length);
	return length % 2 == 0 ? Math.average(array[length/2-1], array[length/2]) : array[length/2];
}
1reaction
hacker-DOMcommented, Dec 21, 2018

Ended up implementing this by myself 😃 To avoid moving around large arrays in evm, I used a linked list. The code is here https://github.com/gnosis/dx-price-oracle/blob/fcf2dd0fac65754ed1f99f899cb95799892f5d5a/contracts/DutchXPriceOracle.sol#L80

To make it reusable, all that has to be done is:

  • Replace the message calls by loading the next array element
  • Collapse the use of two arrays of data into one (we use two, one for numerators and one for denominators, because our data are fractions)

Just my two cents ✌️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Program for Mean and median of an unsorted array
To find median: · First, simply sort the array · Then, check if the number of elements present in the array is even...
Read more >
How to calculate the median of an array? - java - Stack Overflow
Try sorting the array first. Then after it's sorted, if the array has an even amount of elements the mean ...
Read more >
C program to calculate median of an array - Includehelp.com
Basically a median is the value present at the centre of a sorted array list. To calculate the median first we need to...
Read more >
How to get the median of an array of numbers in JavaScript
You can easily find the median if the number of elements is odd, but if the count is even, then we need to...
Read more >
Java Program To Calculate Median Array | 4 Methods
Calculate Median Array – Standard Method · if(n%2==1) · If the number of elements is odd then, the center-most element is the median....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found