You are on page 1of 2

Task 1: Pseudocode and Flowchart for Sorting Algorithm Write pseudocode and create

a flowchart for a bubble sort algorithm. Provide a brief explanation of how the
algorithm works and a simple array of integers to demonstrate a dry run of your
algorithm.

Pseudocode for Bubble sort Algorithm:

function bubbleSort(arr)
n = length of arr
for i from 0 to n - 1
for j from 0 to n - i - 1
if arr[j] > arr[j + 1]
swap arr[j] and arr[j + 1]
return arr

Explanation:

Bubble sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order. The pass
through the list is repeated until the list is sorted.
In each pass, the largest unsorted element bubbles up to its correct position at
the end of the array.
The algorithm has a time complexity of O(n^2) in the worst-case scenario, where 'n'
is the number of elements in the array.

Example :

Let's consider an array: [5, 3, 8, 1, 2]

1st pass:

Comparisons: (5, 3), (5, 8), (8, 1), (8, 2)


Swaps: (5, 3), (8, 1), (8, 2)
Array: [3, 5, 1, 2, 8]
2nd pass:

Comparisons: (3, 5), (5, 1), (5, 2)


Swaps: (5, 1), (5, 2)
Array: [3, 1, 2, 5, 8]
3rd pass:

Comparisons: (3, 1), (3, 2)


Swaps: (3, 1), (3, 2)
Array: [1, 2, 3, 5, 8]
4th pass:

Comparisons: (1, 2)
No swaps needed, array already sorted.
The final sorted array: [1, 2, 3, 5, 8]

Task 2: Recursive Function and Efficiency Analysis Write a recursive function


pseudocode and calculate the nth Fibonacci number and use Big O notation to analyze
its efficiency. Compare this with an iterative approach and discuss the pros and
cons in terms of space and time complexity

Recursive Fibonacci Function Pseudocode:

function fibonacci(n)
if n is 0
return 0
else if n is 1
return 1
else
return fibonacci(n - 1) + fibonacci(n - 2)

Efficiency Analysis using Big O notation:

The time complexity of the recursive Fibonacci function is exponential,


specifically O(2^n). This is because for each call to the function, two more
recursive calls are made, resulting in a binary tree-like recursion structure.
Additionally, the space complexity of the recursive Fibonacci function is also
exponential, as each call to the function consumes memory for its own stack frame,
leading to a space complexity of O(n) in the worst case, where 'n' is the depth of
the recursion tree.

Iterative Fibonacci Function Pseudocode:

function iterativeFibonacci(n)
if n is 0
return 0
else if n is 1
return 1
else
fib1 = 0
fib2 = 1
for i from 2 to n
temp = fib1 + fib2
fib1 = fib2
fib2 = temp
return fib2

Efficiency Analysis using Big O notation:

The time complexity of the iterative Fibonacci function is linear, specifically


O(n). This is because it iterates through the sequence once, calculating each
Fibonacci number in constant time.
The space complexity of the iterative Fibonacci function is O(1), as it only
requires a constant amount of memory for storing variables regardless of the input
size.
Comparison:

The recursive approach to calculating Fibonacci numbers is elegant and easy to


understand, but it suffers from poor efficiency due to its exponential time and
space complexity.
On the other hand, the iterative approach is more efficient in terms of both time
and space complexity, making it preferable for large values of 'n'.
However, the recursive approach may be more intuitive for smaller values of 'n' and
can be easier to implement for some programmers.
Overall, the iterative approach is more practical for real-world applications ,
while the recursive approach may be suitable for educational purposes or situations
where simplicity and clarity are prioritized over efficiency.

You might also like