----- Original Message ----- From: "Gregg Allen" <[EMAIL PROTECTED]>
.
.

This is what I was using for medians:

use Algorithm::MedianSelect::XS qw(median);


Aaah ... the following script demonstrates the Inline::C equivalent of that function. It should work just as quickly and correctly as the function in that module:

--------------------
use warnings;
use Inline C => <<'EOC';

void median(SV * x, ...) {
       Inline_Stack_Vars;
       long buffer, numbers[items];
       int i, is_sorted, median;

       if (Inline_Stack_Items <= 1) {
    croak("Require more than one argument");
}
       for (i = 0; i < items; i++) {
    numbers[i] = SvIV(Inline_Stack_Item(i));
}
do {
    is_sorted = 1;
    for (i = 0; i < (Inline_Stack_Items-1); i++) {
        if (numbers[i-1] < numbers[i] && numbers[i] < numbers[i+1])
     continue;
        if (numbers[i] > numbers[i+1]) {
            buffer = numbers[i];
     numbers[i] = numbers[i+1];
     numbers[i+1] = buffer;
     is_sorted = 0;
        }
    }
} while (!is_sorted);
if (Inline_Stack_Items % 2 == 0) median = Inline_Stack_Items / 2;
else median = (Inline_Stack_Items - 1) / 2;
       Inline_Stack_Reset;
       Inline_Stack_Push(sv_2mortal(newSViv(numbers[median])));
       Inline_Stack_Done;
       Inline_Stack_Return(1);
}


EOC

@x = (1106, 102, 103, 105, 104, 80, 1107);

$median = median(@x);

print $median, "\n";
-------------------------

Be warned, however, that it's Fridy evenin' over here .... I've had a few to drink ... and my judgement may be impaired.

(My level of intelligence may also be impaired ... but that's *always* the case ... irrespective of my level of sobriety :-)

You might gain a small (but measurable) performance in improvement if you assign Inline_Stack_Items to a variable. (Haven't checked that .. and even if it *is* the case, it's probably only measurable for very large arrays.)

Cheers,
Rob

Reply via email to