Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to determine the non-zero minimum

Having an array of for instance 4 integers how can one determine it's non-zero minimum - in the fastest way ?

like image 919
Patryk Avatar asked Jan 15 '12 03:01

Patryk


People also ask

How do you find the smallest nonzero number in an array?

The procedure is the same as finding the smallest from an array. On top of that, add a condition to check that the current search is not zero. Note: The change from smallest > priceArr[x] to smallest < priceArr[x] . If your array size is at least 1, you can also set smallest as the first array element.


1 Answers

Unless you keep the minimum value as elements are added to the array, or you keep the array in a sorted order - I see no other solution but to iterate every member to determine the minimum value.

There is no 'fast' way of testing each member.

Generally I suggest do not optimize something unless it actually proves to be slow. The old rule of your program spends 90% of its time in 10% of the code generally holds true. So does the rules that programmers are 99.99% likely to optimize code not in that 10%.

Profile your code - profile your code - profile your code

like image 110
Adrian Cornish Avatar answered Sep 20 '22 02:09

Adrian Cornish