I am reading Cracking the Coding Interview (the new one). The program seems to be running correctly. When I check it out though, it seems N^2 / 2 is the answer. I don't think I am right. Could someone tell me what the Big-O is and why?
class Program
{
static void Main(string[] args)
{
int userNumber = Convert.ToInt32(Console.ReadLine());
int[] makeAnArray = new int[userNumber];
for (var x = 0; x < userNumber; x++)
{
makeAnArray[x] = x;
}
DisplayIterations(makeAnArray);
}
static void DisplayIterations(int[] testA)
{
int totalIterations = 0;
for (var i = 0; i < testA.Length; i++)
{
totalIterations++;
Console.WriteLine("i is " + i );
for (var j = i + 1; j < testA.Length; j++)
{
totalIterations++;
Console.WriteLine("j is " + j);
}
}
Console.WriteLine("The amount of iterations: " + totalIterations);
}
}
Basically the function takes in an array, runs a for
loop for the array length and a for loop length-1
. I put in 10 I get 55 back.