Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Task 2
- * What is the expected running time of the
- * following C# code? Explain why.
- * Assume the input matrix has size of n * m.
- */
- namespace Task2
- {
- using System;
- public class Task2
- {
- /// <summary>
- /// Original Code from homework.
- /// </summary>
- public static long CalcCountOriginal(int[,] matrix)
- {
- long count = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- if (matrix[row, 0] % 2 == 0)
- for (int col = 0; col < matrix.GetLength(1); col++)
- if (matrix[row, col] > 0)
- count++;
- return count;
- }
- /// <summary>
- /// Test code.
- /// </summary>
- private static void Main()
- {
- int[,] matrix = new int[,]
- {
- { 1, 2, 4, -5, 3 },
- { 122, 33, -56, 22, 3 },
- { 1, 2, 4, -5, 3 },
- { 122, 33, -56, 22, 3 },
- };
- Console.WriteLine(CalcCountOriginal(matrix));
- }
- /*
- * Runing time of this code is: O(n * m)
- * Because for every row with first element wich is even
- * we check all columns of row --- odd <-> even is fifty <-> fifty ;)
- * and therefore formula is n/2 * m ---> O(n * m).
- * No improving is available - this is optimal code!
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement