| | |
find top n elements
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 54
Reputation:
Solved Threads: 3
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
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
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
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
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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.
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.
•
•
Join Date: Nov 2007
Posts: 54
Reputation:
Solved Threads: 3
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?
![]() |
Similar Threads
- META Keywords-how many? (Search Engine Optimization)
- CSS Centering Image Navigation Bar (HTML and CSS)
- help with - Read csv into vector and display elements of vector. (C++)
- BUG in Internet Explorer. Please help find a solution! (HTML and CSS)
- arrays (C)
- using ifstream, counting elements of each line in a dat file (C++)
- phpBB 2.2 is Shut Down ....... Welcome Olympus! (Existing Scripts)
- Browser Battle -- Your TOP Pick! (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: SetWindowText doesn't seem to work
- Next Thread: what is lvalue
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






