• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Smallest Number in Array

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can sort the array using a sort method from java.util.Arrays. (See the API.)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://commons.apache.org/lang/api/org/apache/commons/lang/math/NumberUtils.html#min(int[])
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I see, that makes sense. Thanks!
     
    Ranch Hand
    Posts: 1296
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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: 22783
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 ]
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic