0% found this document useful (0 votes)
5 views4 pages

Unit 4 Part C

The document outlines a program that implements the Bubble Sort algorithm to sort an array of numbers, including a Python code example. It provides a flowchart and flow graph to represent the program logic and calculates the Cyclomatic Complexity, which is determined to be 5. The document emphasizes the simplicity of Bubble Sort for educational purposes and the importance of understanding code complexity for software quality.

Uploaded by

vreena11xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Unit 4 Part C

The document outlines a program that implements the Bubble Sort algorithm to sort an array of numbers, including a Python code example. It provides a flowchart and flow graph to represent the program logic and calculates the Cyclomatic Complexity, which is determined to be 5. The document emphasizes the simplicity of Bubble Sort for educational purposes and the importance of understanding code complexity for software quality.

Uploaded by

vreena11xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Problem Statement

Write a program to sort n numbers using a sorting algorithm. Also, represent the program
logic with:

• A Flowchart

• A Flow Graph

• Calculate and explain Cyclomatic Complexity

2. Sorting Logic (Bubble Sort)

➤ Algorithm:

• Compare each pair of adjacent elements.

• Swap if they are in the wrong order.

• Repeat this process n-1 times.

➤ Why Bubble Sort?

• Simple to implement

• Easy to represent in diagrams

• Best for educational purposes

1. Sorting Program (Bubble Sort in Python)

python

CopyEdit

def bubble_sort(arr):

n = len(arr)

for i in range(n):

# Last i elements are already in place

for j in range(0, n - i - 1):

if arr[j] > arr[j + 1]:

# Swap if elements are in the wrong order

arr[j], arr[j + 1] = arr[j + 1], arr[j]

return arr
# Example usage

numbers = [64, 25, 12, 22, 11]

sorted_numbers = bubble_sort(numbers)

print("Sorted array is:", sorted_numbers)

4. Flowchart Explanation

• Start: Begin the program

• Input array: Take user input for array

• Outer Loop (i): Loop over array elements

• Inner Loop (j): Loop over unsorted elements

• Condition: Check if current element is greater than next

• Swap: Swap the elements if needed

• End Loops: Continue until fully sorted

• Output: Print the sorted array

• End: Terminate the program


5. Flow Graph (Control Flow Graph)

➤ Nodes: Represent decisions or actions

➤ Edges: Arrows that show flow between steps

Structure:

Node 1: Start

Node 2: Input

Node 3: Outer loop

Node 4: Inner loop

Node 5: If condition

Node 6: Swap

Node 7: End inner loop

Node 8: Output

Node 9: End

6. Cyclomatic Complexity

➤ Definition:

Cyclomatic Complexity measures the number of linearly independent paths through a


program.

➤ Formula:

V(G) = E - N + 2P

Where:

E = Number of Edges

N = Number of Nodes

P = Number of connected components (usually 1)

➤ In our case:

E = 10

N=7

P=1
V(G) = 10 - 7 + 2*1 = 5

So, Cyclomatic Complexity = 5

7. Use of Cyclomatic Complexity

Helps in identifying complexity of code

Indicates number of test cases needed for full path coverage

Supports code quality analysis

8. Conclusion

• A simple bubble sort algorithm is used


• The logic is clearly shown with a flowchart and flow graph
• Cyclomatic complexity is calculated to be 5
• This helps ensure software quality through better understanding of the control
structure

You might also like