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

1. Member 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 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.  Reply With Quote

2. Member 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))

Last edited by prometheuzz; 11-09-2008 at 12:24 PM.  Reply With Quote

3. ## What you have tried so far? Can you post that here to see.  Reply With Quote

4. Senior Member Join Date
Feb 2009
Posts
312
Rep Power
11

## 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
}
}
}
}```  Reply With Quote

5. Senior Member Join Date
Jan 2009
Posts
671
Rep Power
12

## What I would do, is search the list for the largest number, remove it, and then do that (k-1) more times.  Reply With Quote

6. ##   Reply With Quote