# Smallest Number in Array

Drew Lane
Ranch Hand
Posts: 296
Hi, I have an array of 100 integers.

Is there a method that will tell me which int is the smallest number in the array?
[ October 26, 2008: Message edited by: Drew Lane ]

marc weber
Sheriff
Posts: 11343
Well, you can sort the array using a sort method from java.util.Arrays. (See the API.)

Ilja Preuss
author
Sheriff
Posts: 14112
http://commons.apache.org/lang/api/org/apache/commons/lang/math/NumberUtils.html#min(int[])

Campbell Ritchie
Sheriff
Posts: 50647
83
They probably want you to work out your own algorithm.

You can set a min value which is higher than the minimum, either by using numbers[0] or Integer.MAX_VALUE.
Then you iterate the array; if the value you find is smaller than the min value, use that as the new min value. You can also record the index in another variable if you wish.
When you get to the end of the array . . . voila!

Rob Spoor
Sheriff
Posts: 20707
68
Per definition the minimum over an empty list / array is +infinity, and the maximum is -infinity. For int, Integer.MAX_VALUE and Integer.MIN_VALUE are the nearest matches for those.

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Rob Prime:
Per definition the minimum over an empty list / array is +infinity, and the maximum is -infinity.

Whom's definition? I'd have thought that it is undefined...

Rob Spoor
Sheriff
Posts: 20707
68
Well that was what I learned at University. With all the theorists, a.k.a. Edsger W. Dijkstra clones. I dare say clones because they have even adopted his handwriting!

All known operators have an identity value, and this is what is returned for empty lists:
• + has 0, since x + 0 == x for all x
• * has 1, since x * 1 == x for all x
• && has true, since x && true == x for all x
• || has false, since x || false == x for all x
• min has +infinity, since min(x, +infinity) == x for all x
• max has -infinity, since max(x, -infinity) == x for all x
• This is also used when formally deducing the code for a program. For which Anne Kaldewaij's "Programming: The Derivation of Algorithms" is a great read.

Ilja Preuss
author
Sheriff
Posts: 14112
I see, that makes sense. Thanks!

Garrett Rowe
Ranch Hand
Posts: 1296
I'm still not there. I understand the logic of the identity value, but I don't get:

...and this is what is returned for empty lists

What do the other operators (+, *, &&, ||) have to do with empty lists. And why would -∞ be the minimum of an empty list? I'd have guessed undefined as Ilja did.

Further this requirement must only refer to lists (sets) of numbers where -∞ is a valid member.

Rob Spoor
Sheriff
Posts: 20707
68
If you read that book I mentioned you'll understand, but recursion is the basics:
min(a[0], a[1], a[2], ..., a[n]) == min(min(a[0], a[1], a[2], ..., a[n - 1]), a[n])

Somewhere this has to end, and Dijkstra and his followers have chosen not to use just an array of size 1 but an empty array as the basis. Basically, they have given undefined a value. And until you have a special symbol for undefined, that makes sense. If the end result of a non-empty array is that value you know that ALL values of the array have that value.

And it's +∞ that is the minimum over an empty list. -∞ would cause problems since -∞ < x for all x.
[ October 28, 2008: Message edited by: Rob Prime ]