עבור כל פעולה, קבעי מהי סיבוכיות הזמן במקרה הגרוע ביותר.
שאלה 1
public static void MethodA(int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 100; j++)
{
Console.WriteLine(i + " " + j);
}
}
}שאלה 2
public static int GetFirstElement(int[] numbers)
{
if (numbers.Length > 0)
{
return numbers[0];
}
return -1;
}שאלה 3
public static void MethodC(int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
Console.WriteLine(i + " " + j);
}
}
}שאלה 4
public int Factorial(int n)
{
if (n <= 1)
return 1;
return n * Factorial(n - 1);
}שאלה 5
public static void ConstantLoop()
{
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine("Iterating...");
}
}שאלה 6
public static void MethodB(int n)
{
int i = n;
while (i > 1)
{
i /= 2;
}
}שאלה 7
public static int CalculateFormula(int n)
{
int sum = n * (n + 1) / 2;
return sum;
}שאלה 8
public static int BinarySearch(int[] arr, int x)
{
int left = 0, right = arr.Length - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}