0

Possible Duplicate:
Finding the median value of an array?
How to calculate mean, median, mode and range from a set of numbers
Combine QuickSort and Median selection algorithm

How can I find the median values of a randomly generated array?

For Example: It would give me an array like 88,23,93,65,22,43 . The code I'm using finds the middle number but it's not sorted.

Here is the code I'm using so far:

double Median()
{
    int Middle = TheArrayAssingment.length / 2;
       if (TheArrayAssingment.length%2 == 1)
        {
           return TheArrayAssingment[Middle];
        }
    else {
        return (TheArrayAssingment[Middle-1] + TheArrayAssingment[Middle]) / 2.0;
    }
}
Community
  • 1
  • 1
user1510747
  • 9
  • 1
  • 3
  • 1
    Dont forget to post your code. – Reimeus Jul 21 '12 at 22:50
  • why not just sort the array first then? – Jon Taylor Jul 21 '12 at 22:50
  • [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – tiwo Jul 21 '12 at 22:51
  • This question has been asked [before](http://stackoverflow.com/questions/4191687/how-to-calculate-mean-median-mode-and-range-from-a-set-of-numbers) – Óscar López Jul 21 '12 at 22:54
  • Duplicate [Finding the median value of an array?](http://stackoverflow.com/q/3691940/1048330) – tenorsax Jul 21 '12 at 22:55
  • If you want fastest asymptotic behaviour, [median of medians](http://stackoverflow.com/q/1790360/1468366) would be the way to go. But that is pretty advanced stuff. By the way, if this is [homework](http://meta.stackexchange.com/a/10812/188688), please tag it accordingly. – MvG Jul 22 '12 at 21:11

1 Answers1

1

Your code looks good but it assumes the array is sorted. Just sort it:

Arrays.sort(TheArrayAssignment);
tiwo
  • 3,238
  • 1
  • 20
  • 33