RE: highest value in a an array

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Wibberlet <Wibberlet@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Basically you will need to sort the array and then choose the last element
(assuming you have sorted it in lowest to highest order).

Well, that's one way to do it. Its complexity is O(n log n) however,
whereas just finding the maximal value only needs to be O(n).

You just go through the array, remembering the current highest value:

int max = int.MinValue;

foreach (int value in integerArray)
{
if (value > max)
{
max = value;
}
}

LINQ will make all of this rather easier.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Play Wales Bin
    ... dragged upwards by a couple of manager types who are in their 40s; ... int main ... You wanted needlessly verbose comments everywhere? ... complexity of the code. ...
    (uk.games.video.misc)
  • Re: CSpinControl/CEdit relationship
    ... Actually that complexity is what Im banking on, ... calculations that need to be redone based on the value of the int in that ... The int is part of a calculation that I want to recalc every ... > for what you want to do (get the edit control value and then update ...
    (microsoft.public.vc.mfc)
  • Re: OOP and C++ and C (was Re: Dennis Ritchie -- An Appreciation)
    ... void f(int*, size_t); ... container removes at least two bits of complexity: ... Tagging an array with a size member is a genuine advantage of a ...
    (comp.lang.c)
  • Re: OOP and C++ and C (was Re: Dennis Ritchie -- An Appreciation)
    ... void f(int*, size_t); ... container removes at least two bits of complexity: ... In all but the most basic situations, the programmer lost track of what the compiler is doing when compilers grew optimisers. ...
    (comp.lang.c)