I currently have an array of around 8 - 10 numbers that changes on a periodic basis.
So around every 5 - 10 seconds the numbers get updated.
I need to get the top 3 numbers in the array every 10 seconds.
This is all done on a mobile device.
The array is the RSSI of the currently scanned access points, so in my office its usually around 10 but out in field testing it could increase to around 50.
At the minute I iterate through the array 3 times and each time I take out the three highest numbers and place them in three previously declared variables.
My question is what should I look to do to increase speed and efficiency in this instance?
The numbers are only 10 - do nothing. It is already efficient enough.
If the size increases, you can use a max-heap to store your numbers.
Why not use the Arrays.sort method which uses as far as I am aware quick sort under the hood.
Paul
EDIT: verified it uses a tuned quick sort
You current algorithm needs 3*n comparisons. You could perform a variation of insertion sort to improve that:
This needs 2*n comparisons. (I'm not sure if it's worth the extra complexity, though.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With