Departament of ECE and Department of CRE, University of Peyge
(III). Analyze the worst-case time complexity of the following algorithm màng big 0 tota
tion. Consider comparison as the basic operation.
Algorithm 1 An algorithm to sort integers in a list.
1: procedure COUNTINGSORT(A)
2: $ $B = an array of n elements
3: $ $C = an array of k elements
4: $ $C[i] = 0
5: $ $for i = 0 to k - 1 do
6: $ $end for
7: $ $for j = 1 to n do
8: $ $C[A[j]] = C[A[j]] + 1
9: $ $end for
10: $ $for i = 1 to k - 1 do
11: $ $C[i] = C[i] + C[i - 1]
12: $ $end for
13: $ $for j = n to 1 do
14: $ $i = A[j]
15: $ $p = C[i]
16: $ $B[p] = i
17: $ $C[i] = C[i] - 1
18: $ $end for
19: $ $return B
20: end procedure
A = ($a_1, a_2, ..., a_n$), n ? 1, ?(i) ? {0, ? k}
Initialize B as an array to store sorted numbers
Initialize C array
Initialize all C[i] = 0, 0 ? i ? k
Count the number of appearance of i, 0 ? i ? k
Update C array
Place number at right position
Return a list of sorted integer
We consider comparison as the main operation.
The algorithm has four main loops.
The first loop (line ____ to ____) has a total of ____ iterations, so its complexity
is _____
The second loop (line ____ to ____) has a total of ____ iterations, so its complexity
is _____
The third loop (line ____ to ____) has a total of ____ iterations, so its complexity
is _____
The last loop (line ____ to ____) has a total of ____ iterations, so its complexity
is _____
So the complexity of the algorithm is _____
CIIC3075 / ICOM4075