Find the smallest of a group of numbers

I would like to find the smallest number in the group of numbers - to keep it simple.

I would like to know the best way to do this.

The numbers would be in an array, set as variables or in a database - whatever makes the most sense.

Thanks



Answer this question

Find the smallest of a group of numbers

  • manick312938

    You'll need a bubblesort (there are other ways, which may be quicker) if you need an algorithm.

    Alternatively, take advantageof built in sort functions, for example the array.sort command: here's a recent link to a thread which involves sorting an array of structures, but it'll work the same way:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1176363&SiteID=1

    Be aware that this is just one way of doing it, there may be a better way: I think the link above demonstrates the most versatile way of sorting.



  • John Papa

    Thanks for your help!


  • Will Merydith

    A sort is not necessary.

    Dim curSmallest As Double = Double.MaxValue
    ' look at each number
    ' if the number i am looking at right now is smaller than smallest
    ' curSmallest is number i am looking at right now
    ' curSmallest is the smallest

  • NoEgo

    Yeah, your second inclination is correct. Let me clean up my algorithm a bit:

    Dim curSmallest As Double = Double.MaxValue
    ' look at each number
    ' if the number i am looking at right now is less than curSmallest
    ' curSmallest is number i am looking at right now
    ' curSmallest is the answer

  • Aaron Oneal

    BlueMikey wrote:
    A sort is not necessary.

    Dim curSmallest As Double = Double.MaxValue
    ' look at each number
    ' if the number i am looking at right now is smaller than smallest
    ' curSmallest is number i am looking at right now
    ' curSmallest is the smallest


    Thanks, but I am a little lost at this line...
    ' if the number i am looking at right now is smaller than smallest


    I am not sure what to put for smallest.

    if curNum < smallest then
    curNum = curSmallest

    My guess would be that smallest above should be curSmallest, but I want to make sure.

    if curNum < curSmallest then
    curNum = curSmallest


  • robhendershot

    Doh!



  • K. Rose

    Lots of response, here's a bit of code from QuickBasic that does the job.
    'demo code to find minimum values stored in an array
    DIM a(1 TO 10) AS SINGLE 'a( ) contains the values
    DIM min, j AS SINGLE
    min = 1000000! 'set min to be larger than expected value
    a(5) = -5 'test value =-5
    FOR j = 1 TO 10
    IF a(j) < min THEN min = a(j)
    NEXT j
    PRINT min


  • Find the smallest of a group of numbers