# finding the largest k numbers in an array without sorting the whole list?

• 11-09-2008, 08:06 AM
jmmjm
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 n-k 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.
• 11-09-2008, 12:15 PM
prometheuzz
"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))

• 11-10-2008, 11:06 AM
Eranga
What you have tried so far? Can you post that here to see.
• 02-06-2009, 10:15 PM
StormyWaters
pseudocode:

numberArray[]//The array of numbers to grab the largest k numbers

sortedArray[]//Create an array of size k

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             }         }     } }```
• 02-07-2009, 08:36 AM