### Thread: mth largest element in array

Page 2 of 2 First 12
1. #### Another method

I had a similar problem as this just a few months back where I had to create a list of the top 10 most visited sites on our network.

The source array I was searching was too big to sort, so what I did, was I created a small 10 element array, each with "0" hits assigned to it, then sorted the larger array into the smaller one, pushing values off the end as needed.

If that doesn't make sense, I can write some pseudo code.
2. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Location
India
Posts
19
Rep Power
0
dog135,

can u explain the algo more ?
3. Sure. The original was in Mumps, so I can't provide a snippet. But here's some pseudo code:

Code:
```inputs: list[],maxlist
output: arr[10] // biggest item: arr[0], smallest: arr[9]

set each item in a to 0;

for(i=0 to maxlist){ // loop through list
for(j=0 to 9){ // loop through arr[]
x=list[i]
y=arr[j]
if(x>=y){ // sort x into arr[]
for(k=j to 8) arr[k+1]=arr[k] // shift values down to make room for x, drop smallest value
arr[j]=x // add x into arr[]
last // end loop, get next item in list[]
}
}
}```
As you can see, you only make one loop through the main list so it'll run faster then sorting the whole list.
Page 2 of 2 First 12