Article View: comp.lang.java
Article #431Re: Finding 2 biggest numbers in a row
From: Marc Chappuis
Date: Wed, 15 Nov 2000 00:00
Date: Wed, 15 Nov 2000 00:00
76 lines
3322 bytes
3322 bytes
Bart Vandewoestyne a οΏ½crit :
>
> Andy Zaidman wrote:
> >
> > If you definitly wanted to work recursively...
>
> Yes, I do.
>
> > Try the same algo, but return
> > only the biggest value and then reapply the algo to the same list, but with
> > the biggest value removed...
>
> Hmm... that is not exactly what I want to do...
> The point in my algo is that if I have 4 values, say p1>p2 (found from left
> side) and q1>q2 (found from right side), then i can find the two biggest numbers
> from (left+right) side by:
>
> if (p1>q1) then {
> p1 is biggest in (left+right);
> if (p2>q1) then p2 is second_biggest else q1 is second_biggest
> }
> else {
> q1 is biggest;
> if (q2>p1) then q2 is second_biggest else p1 is second_biggest
> }
>
> This will always result in a maximum of 2 comparisons.
>
> > Otherwise, just plainly run through the entire
> > list and keep the 2 biggest values in 2 variables...
>
Hi,
> That is not what I want to do. I want to implement the recursive algorithm.
Ok, look at a linear algorithm, and if realy you still want to implement a
recursive one, you can still use this example and call it with variable
search limits.
final static int MIN = -9999999;
public static void main(String[] args)
{
int first = MIN;
int second = MIN;
int[] list = {78, 67, 51, 15, 34, 28, 31, 53, 35, 50, 68, 1, 33, 92, 95, 13};
int size = list.length;
for(int i=0;i<size;i++)
{
final int n = list[i];
if(n>first) {
second = first;
first = n;
}
else if(n>second)
{
second = n;
}
}
System.out.println(first);
System.out.println(second);
}
regards,
Marc
_____________________________________________________________________
L U D O M E D I A http://www.ludomedia.ch
------------------------- Phone: ++ 41 21 634.80.88
W E B - S O L U T I O N S mailto:marnic@ludomedia.ch
_____________________________________________________________________
Technical Details
Message-ID:
<3A124685.78849562@ludomedia.ch>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3A11B99A.2BB2E1E7@skynet.be> <8usf9t$fod$1@news1.skynet.be> <3A11CCFF.A6526AE9@skynet.be>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f5d71,470c011dba4db9c3 X-Google-Attributes: gidf5d71,public X-Google-Thread: 10a146,470c011dba4db9c3 X-Google-Attributes: gid10a146,public X-Google-Thread: fcea4,470c011dba4db9c3 X-Google-Attributes: gidfcea4,public X-Google-Thread: 113ae9,470c011dba4db9c3 X-Google-Attributes: gid113ae9,public From: Marc Chappuis <marnic@ludomedia.ch> Subject: Re: Finding 2 biggest numbers in a row Date: 2000/11/15 Message-ID: <3A124685.78849562@ludomedia.ch> X-Deja-AN: 693812375 Content-Transfer-Encoding: 8bit References: <3A11B99A.2BB2E1E7@skynet.be> <8usf9t$fod$1@news1.skynet.be> <3A11CCFF.A6526AE9@skynet.be> To: Bart Vandewoestyne <Bart.Vandewoestyne@skynet.be> X-Accept-Language: fr Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: abuse@urbanet.ch X-Trace: news1.urbanet.ch 974276260 502 195.202.218.162 (15 Nov 2000 08:17:40 GMT) Organization: Ludomedia Mime-Version: 1.0 Reply-To: marnic@ludomedia.ch NNTP-Posting-Date: 15 Nov 2000 08:17:40 GMT Newsgroups: comp.lang.java,comp.lang.java.help,comp.lang.java.programmer,be.comp.programming