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

Aoa Lab03 23102B2007

The document describes a program that analyzes the runtime of merge sort on randomly arranged and sorted arrays of increasing sizes from 5000 to 50000 elements. The program applies merge sort to each array, measures the runtime, and plots the results on a graph to analyze merge sort's performance and efficiency as input size increases.
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)
19 views4 pages

Aoa Lab03 23102B2007

The document describes a program that analyzes the runtime of merge sort on randomly arranged and sorted arrays of increasing sizes from 5000 to 50000 elements. The program applies merge sort to each array, measures the runtime, and plots the results on a graph to analyze merge sort's performance and efficiency as input size increases.
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

Department of Computer Engineering

Semester S.E. Semester IV – CMPN


Subject AOA LAB
Subject Professor Prof. Sanjeev Dwivedi
In-charge
Assisting Prof. Sanjeev Dwivedi
Teachers
Laboratory M312

Student Name Manav Sarang


Roll Number 23102B2007
Grade and Subject Teacher’s
Signature

Experiment 3
Number
Experiment Analysis of Runtime for Merge Sort
Title
Resources /
Apparatus
Hardware: Desktop/Laptop Software: VS Code
Required

Description The program generates randomly arranged and sorted arrays of increasing
sizes from 5000 to 50000 elements, applies the Merge Sort algorithm to each
array, measures the runtime of the sorting process, and plots the runtime for
both types of arrays on a graph. The graph allows visual analysis of Merge
Sort's performance in terms of runtime as the input size increases, providing
insights into its efficiency and scalability.

Program import time


import random
import matplotlib.pyplot as plt

def generate_random_array(size):
return [random.randint(1, 1000) for _ in range(size)]

def generate_sorted_array(size):
return list(range(1, size + 1))
Department of Computer Engineering

def save_array_to_file(array, filename):


with open(filename, 'w') as f:
for num in array:
f.write(f"{num}\n")

def merge_sort(arr):
if len(arr) <= 1:
return arr

mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]

left_half = merge_sort(left_half)
right_half = merge_sort(right_half)

return merge(left_half, right_half)

def merge(left, right):


merged = []
left_idx, right_idx = 0, 0

while left_idx < len(left) and right_idx < len(right):


if left[left_idx] < right[right_idx]:
merged.append(left[left_idx])
left_idx += 1
else:
merged.append(right[right_idx])
right_idx += 1

merged.extend(left[left_idx:])
merged.extend(right[right_idx:])

return merged

def measure_merge_sort_runtime(array):
start_time = time.time()
merge_sort(array)
end_time = time.time()
return end_time - start_time

# Generate and save randomly arranged arrays


for size in range(5000, 50001, 5000):
array = generate_random_array(size)
Department of Computer Engineering

save_array_to_file(array, f"random_array_{size}.txt")

# Generate and save sorted arrays


for size in range(5000, 50001, 5000):
array = generate_sorted_array(size)
save_array_to_file(array, f"sorted_array_{size}.txt")

# Measure runtime for randomly arranged arrays


random_runtimes = []
for size in range(5000, 50001, 5000):
with open(f"random_array_{size}.txt", 'r') as f:
array = [int(line.strip()) for line in f]
runtime = measure_merge_sort_runtime(array)
random_runtimes.append(runtime)

# Measure runtime for sorted arrays


sorted_runtimes = []
for size in range(5000, 50001, 5000):
with open(f"sorted_array_{size}.txt", 'r') as f:
array = [int(line.strip()) for line in f]
runtime = measure_merge_sort_runtime(array)
sorted_runtimes.append(runtime)

# Plot the graph


plt.plot(range(5000, 50001, 5000), random_runtimes, label='Random Arrays')
plt.plot(range(5000, 50001, 5000), sorted_runtimes, label='Sorted Arrays')

plt.xlabel('Input Size')
plt.ylabel('Runtime (seconds)')
plt.title('Merge Sort Runtime Analysis')
plt.legend()
plt.show()
Department of Computer Engineering

Output

Conclusion Thus, we successfully implemented Analysis on Merge Sort Algo

You might also like