✅ Can we find min and max values of any given array directly? (use of any built-in methods?)
Hi guys, I wanted to find min and max value of an array of numbers. I can do it using a loop, comparing each index. I have to manually set up everything and the algorithm is not as much efficient I think. So I was wondering if there was any in-built method or any better algorithm in terms of efficiency please
13 Replies
There's
Enumerable.Max()
from LINQ, but under the hood it does the same thing
You can't know what the min/max are without checking every element
There are SIMD accelerated solutions that can check multiple pairs of entries simultaneously, but that's not something you need to worry about at this pointoki noted, thanks !
sure thing
>algorithm is not much efficient
Surely it’s O(n)? How did you do it @Faker?
Cattywampus
REPL Result: Success
Console Output
Compile: 548.824ms | Execution: 64.503ms | React with ❌ to remove this embed.
like that ☝️
it might not be faster due to
GetElement()
s, should be easily be tackled with shuffles then use myVec.ToScalar()
which is free compared to GetElement
tbfh, 100% sure Linq already be doing this under the hoodsurprisingly no
at least not the file i checked
oh wait, yes it does, i just can't read code properly
yeah, min/max is like very easy to vectorized
euh I iterate through entire array, take the maximum as being the first element, then check for each subsequent element, time complexity of O(n) yeah
what is the meaning of vectorized please ;c... I once saw the syntax of C++ where the term "vector" was used
SIMD-accelerated types in .NET - .NET
This article describes SIMD-enable types in .NET and demonstrates how to use hardware SIMD operations in C# and .NET.
it's a somewhat advanced topic
not the same vectors in C++, those are equivalent to C#'s lists
https://www.meziantou.net/finding-maximum-value-in-an-array-using-vectorization.htm here's an example of finding max using it
oh ok, I will just scheme through, just to see what it is all about
Thanks !
sure thing