Results 1 to 6 of 6
 11092008, 08:06 AM #1Member
 Join Date
 Nov 2008
 Posts
 1
 Rep Power
 0
finding the largest k numbers in an array without sorting the whole list?
Hi, I need help with my assignment.
I need to write a program to find the largest k numbers in the array.
eg. array: 12, 9, 36, 3, 0, 33
k = 3
output = 12, 36, 33
Here are the constraints:
1. I can't sort the whole list and then pick out the largest k numbers
but can sort part of the data
2. output of k largest items doesn't need to be sorted.
3. the running time should run better than O(nlogn) , n is the size of the array.
Here is what I am going to do.
1. first, pick the first k numbers in the data and store them in another array of size k, called HEAP
2. sort the HEAP of size k, which takes klogk time when using the quicksort.
3. loop over the rest nk numbers in the original array, for each number, compare it to the smallest number in the HEAP array, if smaller than it, discard it, if larger than it, update the smallest number in the HEAP array with this number.
4. If after the updating, the order of the numbers in the HEAP array has changed, then resort them, resorting takes logk time when using heap sort. (I am not sure about the running time here...)
5. then worst case running time should be klogk + (n  k) * klogk = nlogk <= nlogn
but using this implementation, the k largest numbers will be sorted at the end. My question is that is there any better way to do it without sorting anything? or the largest k numbers are not sorted at the end?
Sorry for the long description. Thanks in advance.
 11092008, 12:15 PM #2Member
 Join Date
 Nov 2008
 Posts
 4
 Rep Power
 0
"Part of the data" is a rather vague term. A part of the data is the entire array except the first element. So sort that "part" which will get you [12, 0, 3, 9, 33, 36]. Then get the largest k numbers ([9, 33, 36]) and check if the first number (12 in this case) is larger than the smallest of your k numbers (9 in this case). If so, remove the smallest of the k numbers and add the first value from your original array. You now have [12, 33, 36].
O(n*log(n))
When crossposting, please provide a link to your other post(s). That way people can first check if you've been answered elsewhere before answering you.
http: //forums.sun.com/thread.jspa?threadID=5346235Last edited by prometheuzz; 11092008 at 12:24 PM.
 11102008, 11:06 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
What you have tried so far? Can you post that here to see.
 02062009, 10:15 PM #4Senior Member
 Join Date
 Feb 2009
 Posts
 312
 Rep Power
 8
pseudocode:
numberArray[]//The array of numbers to grab the largest k numbers
sortedArray[]//Create an array of size k
Java Code:for (int i = 0; i < numberArray.length(); i ++){//For all the numbers in your given array int temp = numberArray[i];//Get the first number for (int j = 0; j < sortedArray.length(); j ++){//For all the spots you can place the number if (!sortedArray[j].equals(null)){//If there currently is a number there int currentValue = sortedArray[j]; //Get the current number set if (currentValue < temp){//If the number in the given array is greater then the current number there sortedArray[j] = temp;//Set that spot to the number in the given array temp = currentValue; //Set the number to check the additional spots to the number that was there } } else { //There is no number there currently sortedArray[j] = temp;//Set the number to the empty spot break;//Break out of the for loop to make sure the other empty spots are not filled with the number } } } }
 02072009, 08:36 AM #5Senior Member
 Join Date
 Jan 2009
 Posts
 671
 Rep Power
 9
What I would do, is search the list for the largest number, remove it, and then do that (k1) more times.
 02072009, 08:48 AM #6
ALERT!
old post. and cross poster: Java Programming  finding the largest k numbers in an array without sorting the whole list?
this problem has been solved in a cross post already.USE CODE TAGS> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads

Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11082010, 09:25 PM 
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11032010, 07:49 AM 
Converting array to list and sorting it
By Java Tip in forum java.langReplies: 0Last Post: 04162008, 10:36 PM 
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01122008, 11:30 PM 
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11292007, 01:49 PM
Bookmarks