Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#16
July 9th, 2003, 03:22 PM
 dog135
Doggie

Join Date: Jul 2003
Location: Seattle, WA
Posts: 751
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 11
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.

#17
July 9th, 2003, 10:22 PM
 me_no_xpert
Junior Member

Join Date: Jun 2003
Location: India
Posts: 19
Time spent in forums: < 1 sec
Reputation Power: 0
dog135,

can u explain the algo more ?

#18
July 10th, 2003, 11:47 AM
 dog135
Doggie

Join Date: Jul 2003
Location: Seattle, WA
Posts: 751
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 11
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.

 Viewing: Dev Shed Forums > Programming Languages - More > Software Design > mth largest element in array