(CYCLOMATIC/MCCABE COMPLEXITY)
Consider the following bubblesort sorting algorithm:
procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure
(a) Draw the flowchart of the above algorithm.
(b) Calculate the cyclomatic complexity of the above algorithm.
Answers:
(a) Flowchart:
(b) Calculation of cyclomatic complexity
Cyclomatic Complexity, M = E – N + 2P
Where, E = no of edges N = no of nodes P = no of connected components
Excluded the START and the END node; and corresponding edges N = 5 E = 6 P = 1
2P = 2*1= 2
Cyclomatic Complexity, M = 6 – 5 + 2 = 3