# Standard Forth versus Python: a case study

Paul McGuire ptmcg at austin.rr._bogus_.com
Thu Oct 12 15:49:12 CEST 2006

```<idknow at gmail.com> wrote in message
> Paul McGuire wrote:
>> <idknow at gmail.com> wrote in message
>> > [snip]
>> >
>> > no sort() is needed to calculate the median of a list.
>> >
>> > you just need one temp var.
>> >
>>
>> Ok, I'll bite.  How do you compute the median of a list using just a
>> single
>> temp var?
>>
>> -- Paul
>
> hi Paul; well when this was a stats-class assignment (back when pascal
> was popular :) i just stepped through the vector and compared it
>
> (pseudo-code)
>
> ptr p = [with values].
>
> fun median {
> var x = 0.
>  while( *p++) {
>    if( (*p) > x) x = *p.
>  }
>  return x.
> }
>
> of course, pascal is more verbose but that's median()
>

No, that's the maximum.  The median value is the value that is in the middle
of the list when the list is sorted.  Many analyses prefer median to mean
(also known as "average") because the median is less sensitive to wild
outlier points.

My original question was in response to your post, that sort() wasn't
required but only a temp variable.  I am very interested in seeing your
solution that does not require the data to be sorted.  (This is not just an
academic exercise - given a large historical data set, sorting the data is
one of the costliest parts of computing the median, and I would greatly
appreciate seeing an alternative algorithm.)

-- Paul

```