CSC 345 Section 4.8
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". a = b + c; d = a + e; Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(1)
Determine \ThetaΘ for the following code fragment in the average case. Assume that all variables are of type "int". sum = 0; for (i = 1; i ≤ n; i *= 2) for (j = 1; j ≤ n; j++) sum++; Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n log n)
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". sum = 0; for (i = 1; i ≤ n; i++) for (j = 1; j ≤ n; j *= 2) sum++; Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n log n)
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". sum = 0; for (i = 0; i < 3; i++) for (j = 0; j < n; j++) sum++; Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n)
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". sum = 0; if (EVEN(n)) for (i = 0; i < n; i++) sum++; else sum = sum + n; Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n)
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". Assume that array A contains nn values, "random" takes constant time, and "sort" takes n log n time. sum = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) A[j] = random(n); sort(A); } Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n^2 log n)
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". for (i = 0; i < n - 1; i++) for (j = i + 1; j < n; j++) { tmp = AA[i][j]; AA[i][j] = AA[j][i]; AA[j][i] = tmp; } Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n^2)
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". sum = 0; for (i = 0; i < n * n; i++) sum++; Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n^2)
Determine Θ for the following code fragment in the average case. Assume that all variables are of type "int". Assume array A contains a random permutation of the values from 0 to n−1. sum = 0; for (i = 0; i < n; i++) { for (j = 0; A[j] != i; j++) sum++; } Θ(n^3) Θ(n^2) Θ(log n) Θ(2^n) Θ(1) Θ(n^2 log n) Θ(n) Θ(n log n)
Θ(n^2)