943,896 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1339
  • C++ RSS
Dec 26th, 2008
0

find top n elements

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
manzoor is offline Offline
54 posts
since Nov 2007
Dec 26th, 2008
0

Re: find top n elements

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 26th, 2008
0

Re: find top n elements

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.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 27th, 2008
0

Re: find top n elements

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?
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
manzoor is offline Offline
54 posts
since Nov 2007
Dec 27th, 2008
1

Re: find top n elements

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: SetWindowText doesn't seem to work
Next Thread in C++ Forum Timeline: Help On Creating a Uni Homework





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC