You are on page 1of 7

ANALYSIS OF

ALGORITHM
NAME : MUHAMMAD TOQEER
REG NO : BSCS-2020-34

PROBLEM : 01
Empirical Analysis of Algorithms Write a program that
shows you Empirical Analysis of Growth of Two
Algorithms belonging to diverse classes (linear,
logarithmic, quadratic, etc.). Then Process should be
executed which should be an algorithm to solve any
problem such as searching, sorting or any other task on
data structures, Matrix Operations or any other problem
you like to solve. Then for increasing values of n, show
the increase in time (in any unit). NOTE: Size of the data
should be randomly generated. Input values can be
randomly generated. (for some diverse algorithms, this
condition may not apply). No two students can have same
algorithms and same code and same execution and same
Graph (obviously). PLUS: Output of One Algorithm (out
of two) should be Dry run of the algorithm. PLUS: Share
Acknowledgements and References.

ALGORITHMS
01: BUBBLE SORT
ITS TIME COMPLEXITY IS O(n) “ BEST CASE “

02: SELECTION SORT


ITS TIME COMPLEXITY IS ‘’O(n)^2’’ IN MOST OF
THE CASES

BUBBLE SORT :
* Start with an unsorted array of elements.
* Compare each element with its adjacent element.
* If the adjacent element is smaller, swap them.
* Repeat this process for each pair of adjacent
elements, moving from the beginning to the end of
the array.
* After one pass, the largest element will be in its
correct position at the end of the array.
* Repeat steps 2-5 for the remaining unsorted part of
the array until the entire array is sorted.
SELECTION SORT :

Selection Sort is another comparison-based sorting


algorithm. It divides the input array into two parts,
the sorted part and the unsorted part. In each
iteration, it finds the minimum element from the
unsorted part and places it at the beginning of the
sorted part. This process is repeated until the entire
array is sorted.
1. Start with an unsorted array of elements.
2. Find the minimum element in the unsorted
part of the array.
3. Swap the minimum element with the first
element of the unsorted part, effectively
expanding the sorted part.
4. Repeat steps 2-3 for the remaining unsorted
part of the array until the entire array is sorted.

RANDOMLY GENERATED INPUTS :


01)
02)

—-----------------------------

OUTPUTS:
01)

02):
—-----------------------------------------------------------------

CODE OF THE PROGRAM:


import matplotlib.pyplot as plt
import timeit
import random

def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]

def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]

# Generate random data for testing


data_sizes = [100, 200, 300, 400, 500]
bubble_sort_times = []
selection_sort_times = []

for size in data_sizes:


arr = random.sample(range(size), size)

bubble_sort_time = timeit.timeit(lambda: bubble_sort(arr.copy()), number=1)


selection_sort_time = timeit.timeit(lambda: selection_sort(arr.copy()), number=1)

bubble_sort_times.append(bubble_sort_time)
selection_sort_times.append(selection_sort_time)

# Plotting the data


plt.plot(data_sizes, bubble_sort_times, label='Bubble Sort')
plt.plot(data_sizes, selection_sort_times, label='Selection Sort')
plt.xlabel('Data Size')
plt.ylabel('Execution Time (seconds)')
plt.title('Comparison of Bubble Sort and Selection Sort')
plt.legend()
plt.show()

# Displaying the data in lists


print("Data Sizes:", data_sizes)
print("Bubble Sort Times:", bubble_sort_times)
print("Selection Sort Times:", selection_sort_times)

—-------------------------------------------------------------------

DRY RUN OF BUBBLE SORT ALGORITHM


Consider the following unsorted array: [5, 2, 8, 3, 1].
Step 1:
● Starting from the first element, compare it with the next element.
● If the next element is smaller, swap their positions.
● Repeat this process until the end of the array is reached.
● After this step, the largest element will be in its correct position at the end of the array.
● Array after Step 1: [2, 5, 3, 1, 8]
Step 2:
● Repeat Step 1 for the remaining unsorted elements (excluding the last element).
● Array after Step 2: [2, 3, 1, 5, 8]
Step 3:
● Repeat Step 1 again for the remaining unsorted elements.
● Array after Step 3: [2, 1, 3, 5, 8]
Step 4:
● Repeat Step 1 once more.
● Array after Step 4: [1, 2, 3, 5, 8]
The algorithm continues to iterate through the array, performing these steps until the entire
array is sorted.
In summary, the dry run of Bubble Sort on the given array [5, 2, 8, 3, 1] results in the sorted
array [1, 2, 3, 5, 8].

—-------------------------------------------------
Acknowledgements 🙂
TO REVISED THE ALGORITHMS ,

REFERENCE 👍
www.geeksforgeeks.org

You might also like