Finding the median of an unsorted array
You can use the Median of Medians algorithm to find median of an unsorted array in linear time.
How to calculate the median of an array?
The Arrays class in Java has a static sort function, which you can invoke with Arrays.sort(numArray)
.
Arrays.sort(numArray);
double median;
if (numArray.length % 2 == 0)
median = ((double)numArray[numArray.length/2] + (double)numArray[numArray.length/2 - 1])/2;
else
median = (double) numArray[numArray.length/2];
Find the median in my array while also sorting the numbers from smallest to largest
Median is the middle element of the sorted array if the array size is odd, else it is the average of the middle two elements. The following code snippet finds the median of the array.
public static void main(String[] args) {
int[]num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
int median;
int len = num.length;
Arrays.sort(num); // sorts the array
//check if the length is odd
if(len%2 != 0)
median = num[len/2];
else // length is even
median = (num[(len - 1) / 2] + num[len / 2])/2;
System.out.println(median);
}
Find the median of an array
I'm going to just jump in with a solution here...
def median(ary)
mid = ary.length / 2
sorted = ary.sort
ary.length.odd? ? sorted[mid] : 0.5 * (sorted[mid] + sorted[mid - 1])
end
Edit - I've incorporated .odd?
as per BroiSatse's suggestion.
Calculating median - javascript
Change your median method to this:
function median(values){
if(values.length ===0) throw new Error("No inputs");
values.sort(function(a,b){
return a-b;
});
var half = Math.floor(values.length / 2);
if (values.length % 2)
return values[half];
return (values[half - 1] + values[half]) / 2.0;
}
fiddle
Get median of array
To get the median
you can use the following:
let median = arr.sorted(by: <)[arr.count / 2]
In your case it will return 5
.
As @Nirav pointed out [1,2,3,4,5,6,7,8]
will return 5
but should return 4.5
.
Use this instead:
func calculateMedian(array: [Int]) -> Float {
let sorted = array.sorted()
if sorted.count % 2 == 0 {
return Float((sorted[(sorted.count / 2)] + sorted[(sorted.count / 2) - 1])) / 2
} else {
return Float(sorted[(sorted.count - 1) / 2])
}
}
Usage:
let array = [1,2,3,4,5,6,7,8]
let m2 = calculateMedian(array: array) // 4.5
Median of an array in Javascript?
var ar1 = [];
while (true) { var enterValues = prompt("enter your values"); if (enterValues != "") { ar1.push(parseFloat(enterValues)); } else { break; }}
function calcMedian() { var half = Math.floor(ar1.length / 2); ar1.sort(function(a, b) { return a - b;}); if (ar1.length % 2) { return ar1[half]; } else { return (ar1[half-1] + ar1[half]) / 2.0; }}
console.log(ar1);console.log(ar1.length);console.log(calcMedian());console.log(ar1);
Related Topics
How to Return a Group of Sequential Numbers That Might Exist in an Array
How to Require Activerecord in Irb
Rails Cors Issue with Ajax API Endpoint Request
Rspec Hook After Report Gets Created
Complete Web Apps in Ruby Without Using Any Frameworks - How Difficult? Pitfalls
Are Spies an Appropriate Approach to See If Resque Methods Are Being Fired
Rails Get Index of "Each" Loop
What Is the Fully Qualified Name of a Model in Ruby on Rails
How to Translate Arabic/Persian Numbers to English Using Ruby
Heroku-18: Git Push Fails. Showing Different Versions of Ruby on Push
Mongomapper Many to Many Issue with Array
Ruby: "Unexpected Keyword_End"... But All Openers and Closers Match