Article View: comp.lang.java
Article #427Finding 2 biggest numbers in a row
From: Bart Vandewoestyne
Date: Tue, 14 Nov 2000 00:00
Date: Tue, 14 Nov 2000 00:00
49 lines
2719 bytes
2719 bytes
I am trying to write a program that implements an algorithm to find the two
largest integers out of a row of numbers.
My algorithm should work this way:
The number of elements in the row is a power of 2.
I separate the row in two halves, and then i apply the same algorithm recursivly
to the two new rows. If I have the two biggest numbers of these new rows, then
i can find the two biggest of the total with maximum 2 comparisons.
The alorithm I implemented so far is located at:
http://users.skynet.be/vandewoestyne/downloads/Searcher.java
and
http://users.skynet.be/vandewoestyne/downloads/BigThings.java
But it seems to have a mistake in it, because this was an example of an
incorrect output I got ('links' = left, 'rechts' = right, 'Grootste waarde =
Biggest value', 'Tweede grootste waarde='second largest value'):
Heaven:~/projecten/ontwerpen02$ java Searcher
78 67 51 15 34 28 31 53 35 50 68 1 33 92 95 13
links : 78 67 51 15 34 28 31 53
rechts: 35 50 68 1 33 92 95 13
links : 78 67 51 15
rechts: 34 28 31 53
links : 78 67
rechts: 51 15
links : 34 28
rechts: 31 53
links : 35 50 68 1
rechts: 33 92 95 13
links : 35 50
rechts: 68 1
links : 33 92
rechts: 95 13
Grootste waarde: 95
Tweede grootste waarde: 78
--
Ing. Bart Vandewoestyne Bart.Vandewoestyne@skynet.be
Hugo Verrieststraat 48 GSM: +32 (0)478 397 697
B-8550 Zwevegem http://hello.to/MC303
----------------------------------------------------------------------
"Any fool can know, the point is to understand." - Albert Einstein
Technical Details
Message-ID:
<3A11B99A.2BB2E1E7@skynet.be>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fcea4,470c011dba4db9c3,start X-Google-Attributes: gidfcea4,public X-Google-Thread: 113ae9,470c011dba4db9c3,start X-Google-Attributes: gid113ae9,public X-Google-Thread: f5d71,470c011dba4db9c3,start X-Google-Attributes: gidf5d71,public X-Google-Thread: 10a146,470c011dba4db9c3,start X-Google-Attributes: gid10a146,public From: Bart Vandewoestyne <Bart.Vandewoestyne@skynet.be> Subject: Finding 2 biggest numbers in a row Date: 2000/11/14 Message-ID: <3A11B99A.2BB2E1E7@skynet.be> X-Deja-AN: 693657255 Cache-Post-Path: marvin!unknown@kn-10-0-53-046.kotnet.kuleuven.ac.be Content-Transfer-Encoding: 7bit X-Accept-Language: nl-BE, nl, en, de Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@belnet.be X-Trace: naxos.belnet.be 974239991 25465 134.58.127.3 (14 Nov 2000 22:13:11 GMT) Organization: MyHome X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/) Mime-Version: 1.0 NNTP-Posting-Date: Tue, 14 Nov 2000 22:13:11 +0000 (UTC) Newsgroups: comp.lang.java,comp.lang.java.help,comp.lang.java.programmer,be.comp.programming