From: John Bokma on
Tad McClellan <tadmc(a)seesig.invalid> writes:

> John Bokma <john(a)castleamber.com> wrote:
>> Tad McClellan <tadmc(a)seesig.invalid> writes:
>>
>>> IJALAB <balaji.draj(a)gmail.com> wrote:
>>>
>>>> captured these 4
>>>> elements in an array. how do i find the max of these values, which is
>>>> in an array using perl?
>>>
>>> my($max) = sort {$b <=> $a} @array;
>
>> A linear search /might/ be faster than sorting if @array is large.
>
> He said n=4, which is why I quoted where he said that n=4

Yes, yes, I didn't miss this, hence the "/might/" and "large" ;-)

(Also, it was a bit of a plug for List::Util)

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: Xho Jingleheimerschmidt on
J�rgen Exner wrote:
> "sopan.shewale(a)gmail.com" <sopan.shewale(a)gmail.com> wrote:
>> Once you have array, how about?
>> my $max = (sort { $b <=> $a } @array)[0];
>
> If you insist on being inefficient,

We are discussing Perl, aren't we?

> then that is certainly a good
> solution.
> Sort is O(n*log n), while computing the max value can easily be done in
> O(n).

Xho
From: John Bokma on
Xho Jingleheimerschmidt <xhoster(a)gmail.com> writes:

> Jürgen Exner wrote:
>> "sopan.shewale(a)gmail.com" <sopan.shewale(a)gmail.com> wrote:
>>> Once you have array, how about?
>>> my $max = (sort { $b <=> $a } @array)[0];
>>
>> If you insist on being inefficient,
>
> We are discussing Perl, aren't we?

Yup, and my experience is that good Perl runs fast enough. I suspect
Python to be a bit slower, but note that that's just a suspicion. On top
of that I don't care.

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: J�rgen Exner on
Xho Jingleheimerschmidt <xhoster(a)gmail.com> wrote:
>J�rgen Exner wrote:
>> "sopan.shewale(a)gmail.com" <sopan.shewale(a)gmail.com> wrote:
>>> Once you have array, how about?
>>> my $max = (sort { $b <=> $a } @array)[0];
>>
>> If you insist on being inefficient,
>
>We are discussing Perl, aren't we?

Well, yeah, so? Are you implying that people should use inefficent
algorithms when programming in Perl?

Of course the cost for designing, writing, and maintaining the code is
an important factor, too, and it often justifies the use of a less
efficient algorithm.
However in this case where there are trivial, short and well-known
algorithms for computing the largest element there is really no excuse
for using a poor algorithm(*).

*: Well, except that Tad noticed the "only 4 element list" and thus
efficieny is REALLY totally irrelevant.

jue
From: Uri Guttman on
>>>>> "JE" == J�rgen Exner <jurgenex(a)hotmail.com> writes:

JE> Xho Jingleheimerschmidt <xhoster(a)gmail.com> wrote:
>> J�rgen Exner wrote:
>>> "sopan.shewale(a)gmail.com" <sopan.shewale(a)gmail.com> wrote:
>>>> Once you have array, how about?
>>>> my $max = (sort { $b <=> $a } @array)[0];
>>>
>>> If you insist on being inefficient,
>>
>> We are discussing Perl, aren't we?

JE> Well, yeah, so? Are you implying that people should use inefficent
JE> algorithms when programming in Perl?

JE> Of course the cost for designing, writing, and maintaining the
JE> code is an important factor, too, and it often justifies the use
JE> of a less efficient algorithm. However in this case where there
JE> are trivial, short and well-known algorithms for computing the
JE> largest element there is really no excuse for using a poor
JE> algorithm(*).

JE> *: Well, except that Tad noticed the "only 4 element list" and thus
JE> efficieny is REALLY totally irrelevant.

so you think applying Sort::Maker on this list is not worth the effort?
:)

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------