This methd has an Big Oh notation performance of O(n)
public static int GetIndexLargestCompareToMax(int[] numbers)
{
int currentMaxIndex = -1;
int length = numbers.Length;
try
{
if (length == 0)
currentMaxIndex = -1;
else if (length == 1)
currentMaxIndex = 0;
else
{
currentMaxIndex = 0;
for (int j = 1; j < length; j++)
{
if (numbers[j] > numbers[currentMaxIndex])
{
currentMaxIndex = j;
}
}
}
}
catch (Exception)
{
currentMaxIndex = -1;
}
return currentMaxIndex;
}
No comments:
Post a Comment