find top n elements

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 54
Reputation: manzoor is an unknown quantity at this point 
Solved Threads: 3
manzoor manzoor is offline Offline
Junior Poster in Training

find top n elements

 
0
  #1
Dec 26th, 2008
How to find the top n elements without sorting the array?

Is this possible???

Well I don't want to sort the array because the order is mandatory? After I have found the top n elements I want to modify them?

Is there any certain algorithM?? Help ???

this is no homework , hints are appreciated


Ty
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: find top n elements

 
0
  #2
Dec 26th, 2008
1) You could sort a copy of the array to determine the values and then search the original array for the desired values.

2) You could place the first n values of the array in a list using and insertion sort so the head of the list is the smallest and the tail the largest. Then compare the first element of the list, to the n + 1 (current) element in the original array. If the current element of the array is bigger than the smallest element of the list then delete the smallest element of the list and insert the current element of the array into the list in the appropriate spot of the list so it remains sorted. When array has been reviewed in it's entirety, then search through the array for the values in the list and do what you will.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: find top n elements

 
0
  #3
Dec 26th, 2008
Here are another 2 options:

1) Make a copy of the array, and do only a partial selection sort. Use the variation of selection sort that puts the largest element at the end. Since you only want the top n elements, only run the outer loop of selection sort n times, then your top n elements should all be at the end of your copied array.

2) Instead of making a copy of the array, search for the largest number in the array, then store the index of that number in a list or vector of some sort. Search again for the largest number in the array, but ignoring any indexes that are in your list of indexes, repeat n times.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 54
Reputation: manzoor is an unknown quantity at this point 
Solved Threads: 3
manzoor manzoor is offline Offline
Junior Poster in Training

Re: find top n elements

 
0
  #4
Dec 27th, 2008
how to ignore the elements that I have found??? is there any specific algorithm for this or would i have to implement this my self?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: find top n elements

 
1
  #5
Dec 27th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC