Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2006

Tip: Looking for answers? Try searching our database.

sorting an array, but not the usual way

Thread view: 
jctown@nb.sympatico.ca - 13 Aug 2006 12:18 GMT
I have an array of 49 integers.  Each bucket contains a count of the
number of times that number (the index of the array) was picked when I
asked for 6 numbers between 1 and 49 1,000,000 times.

What I need to do is create a new array, this time with only 6 integers
where they contain the top 6 picks.

So, if my bigList contains

1)  123122
2)  233422
.
.
.
49)  231221

The final array would have say
2, 7, 29, 40, 12, 49 if those 6 numbers were picked the most.
I need an algorithm....Array.sort messes up the array so I can't tell
when numbers were picked the most.  Any ideas?

PS - This is not a homework assignment,  I am just trying to do some
elementary Java programming on my own to put into practice what I have
been learning,
Patricia Shanahan - 13 Aug 2006 12:41 GMT
> I have an array of 49 integers.  Each bucket contains a count of the
> number of times that number (the index of the array) was picked when I
[quoted text clipped - 16 lines]
> I need an algorithm....Array.sort messes up the array so I can't tell
> when numbers were picked the most.  Any ideas?

Essentially, you are doing "k largest elements", and sort is one means
to that end.

Here are a couple of approaches:

1. Find the six largest elements without sorting. Do a single scan of
the array, keeping track of the value and index for each of the 6
largest elements found so far. Store the index values for the final
version of the 6 largest in the new array.

As a variant, keep track of only the values of the six largest elements,
and do a final scan to find them and write their index values to a new
array.

2. Create a new array containing objects with both the value and index
for each entry in the original array. Sort on value, pick the last six
elements, and store their index values in the new array.

> PS - This is not a homework assignment,  I am just trying to do some
> elementary Java programming on my own to put into practice what I have
> been learning,
Dale King - 29 Aug 2006 20:44 GMT
>> I have an array of 49 integers.  Each bucket contains a count of the
>> number of times that number (the index of the array) was picked when I
[quoted text clipped - 19 lines]
> Essentially, you are doing "k largest elements", and sort is one means
> to that end.

For a thorough discussion of alternatives for selection see this
Wikipedia article:

http://en.wikipedia.org/wiki/Selection_algorithm

Signature

 Dale King

vir calimlim - 14 Aug 2006 11:36 GMT
if you don't want to write your own sort logic, try class SortedSet

vir


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.