Professional Documents
Culture Documents
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.
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 :
1st pass:
Comparisons: (1, 2)
No swaps needed, array already sorted.
The final sorted array: [1, 2, 3, 5, 8]
function fibonacci(n)
if n is 0
return 0
else if n is 1
return 1
else
return fibonacci(n - 1) + fibonacci(n - 2)
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