Get min/max values in data collections with binary heap?

I have list data of int, float, long.

How to get minimum and maximum number (smallest or largest) among them?

I searched a little, But I couldn’t find exactly whole implementation of get min and max.

Where can I see it?

Thanks.

Use this snippet:

``````List<int> myList = new List<int>();

int min_value = Int32.MaxValue;
int max_value = Int32.MinValue;

foreach(int item in myList)
{
if(item > max_value)
max_value = item;

if(item < min_value)
min_value = item;
}
Debug.Log("Min: " + min_value.ToString() + "; Max: " + max_value.ToString());
``````

Thx for simple code, but I heard binary heap way is much faster. Don’t need to care this until getting mix/max become core or list’s data list become many? (over hundreds?)

and this result just max and min of Int32.

so some revision needed like below?

int min_value = myList[0];
int max_value = myList[0];

This is linear complexity O(n) algorithm, so it should work enought fast over hundreds and thousands. There is not reason to use binary heap, I think.

P.S.: This is good way:

``````int min_value = myList[0];
int max_value = myList[0];
``````