Maximum value in an array

Post

Posted
Rating:
#1 (In Topic #869)
Regular
bill-lancaster is in the usergroup ‘Regular’
 What is the best way to determine the max or min value in array?
At the moment I'm copying the array to a temporary array, sorting it then using array.max
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

bill-lancaster said

What is the best way to determine the max or min value in array?
At the moment I'm copying the array to a temporary array, sorting it then using array.max

I should think just itterate through it..

Code (gambas)

  1. Dim MinVal, MaxVal As Integer
  2.  
  3. For Each CurVal As Integer in MyArrayOfNumbers
  4.   MaxVal=Max(CurVal, MaxVal)
  5.   MinVal=Min(CurVal, MinVal)
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
bill-lancaster is in the usergroup ‘Regular’
Thanks Bruce,
That's a nice idea but MinVal is always zero!
Also, copying the array of values into a temporary array then sorting it strangely sorts the original array.

Code

Dim MyArrayOfNumbers As Integer[] = [99, 91, 93, 92, 95, 90, 95, 100, 90, 90, 99]
Dim fTemp As Integer[]
fTemp = MyArrayOfNumbers
fTemp.Sort
Print fTemp[0];; MyArrayOfNumbers[0]
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

bill-lancaster said

Thanks Bruce,
That's a nice idea but MinVal is always zero!
Also, copying the array of values into a temporary array then sorting it strangely sorts the original array.

Code

Dim MyArrayOfNumbers As Integer[] = [99, 91, 93, 92, 95, 90, 95, 100, 90, 90, 99]
Dim fTemp As Integer[]
fTemp = MyArrayOfNumbers
fTemp.Sort
Print fTemp[0];; MyArrayOfNumbers[0]

You said you wanted to display max or min values!
"What is the best way to determine the max or min value in array?"
If min is always zero then why do you say you want to find min value?

Ahh no sorry i see what you mean now.
Try this…

Code (gambas)

  1.  Dim MinVal As Integer = MyArrayOfNumbers[0]
  2.  Dim MaxVal As Integer
  3.  
  4. For Each CurVal As Integer in MyArrayOfNumbers
  5.   MaxVal=Max(CurVal, MaxVal)
  6.   MinVal=Min(CurVal, MinVal)
  7.  
  that will give MinVal an initial value that's not zero to begin with.

iterating through the array will be a much faster operation than making a copy then using sort on it.

PS. Sorting original array is not so strange as you are not copying the array you are making fTemp a pointer to the array.
fTemp = MyArrayOfNumbers.Copy()  will make a copy.
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
bill-lancaster is in the usergroup ‘Regular’
Of course!  I spotted my mistake earlier and found that this works:-

Code

fTemp = MyArrayOfNumbers.Copy(0, MyArrayOfNumbers.Max)
fTemp.Sort()
Print fTemp[0];; fTemp[fTemp.Max]
 
But your solution using MinVal = MyArrayOfNumbers[0] works fine and as you say, iteration is quicker than .Copy.
Thanks for your help
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

bill-lancaster said

Of course!  I spotted my mistake earlier and found that this works:-

Code

fTemp = MyArrayOfNumbers.Copy(0, MyArrayOfNumbers.Max)
fTemp.Sort()
Print fTemp[0];; fTemp[fTemp.Max]
 
But your solution using MinVal = MyArrayOfNumbers[0] works fine and as you say, iteration is quicker than .Copy.
Thanks for your help

WOW, i used the profiler and found out your way is actually faster, by quite a lot! LOL :)

I made this command…

Code (gambas)

  1. Public Sub MinMaxValue((Array) As Variant[]) As Variant[]
  2.  
  3.   Dim a As Variant[] = Array.Sort()
  4.   Return [a.First, a.Last]
  5.  
  6.  

it returns [MinValue, MaxValue]

So can be used like
Dim MinMax As Integer[] = MinMaxValue(MyArrayOfNumbers)
Print "Min="; MinMax[0];; "Max="; MinMax[1]

or
Print "Max="; MinMaxValue(MyArrayOfNumbers)[1]

and is speedy.
you need less help than you think lol :)
Sorry for my misunderstanding.
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
bill-lancaster is in the usergroup ‘Regular’
 No problem Bruce, it was a useful discussion I'm just surprised its not a buit-in function e.g. MyArray.MaxValue.
Anyway thanks again.
Online now: No Back to the top
1 guest and 0 members have just viewed this.