Maximum value in an array
Posted
#1
(In Topic #869)
Regular

At the moment I'm copying the array to a temporary array, sorting it then using array.max
Posted
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..
Posted
Regular

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]
Posted
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!
If min is always zero then why do you say you want to find min value?"What is the best way to determine the max or min value in array?"
Ahh no sorry i see what you mean now.
Try this…
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.
Posted
Regular

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
Posted
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…
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.
Posted
Regular

Anyway thanks again.
1 guest and 0 members have just viewed this.


