wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Smallest Number in Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Smallest Number in Array" Watch "Smallest Number in Array" New topic
Author

Smallest Number in Array

Drew Lane
Ranch Hand

Joined: May 13, 2001
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

Joined: Aug 31, 2004
Posts: 11343

Well, you can sort the array using a sort method from java.util.Arrays. (See the API.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://commons.apache.org/lang/api/org/apache/commons/lang/math/NumberUtils.html#min(int[])


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

    Joined: Jul 11, 2001
    Posts: 14112
    I see, that makes sense. Thanks!
    Garrett Rowe
    Ranch Hand

    Joined: Jan 17, 2006
    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.


    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19651
        
      18

    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Smallest Number in Array
     
    Similar Threads
    Variable Madness
    -VE or +VE
    The smallest stack
    Array i/o and sorting
    Comparison between array elements