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

Aoa Lab02 23102B2007

This Python program compares the runtime performance of the Quick Sort algorithm for sorting randomly arranged and sorted arrays of varying sizes ranging from 5000 to 50000 elements. It applies Quick Sort to each array, measures the runtime, and plots the runtime on a graph to provide insights into Quick Sort's efficiency and scalability as the 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 views3 pages

Aoa Lab02 23102B2007

This Python program compares the runtime performance of the Quick Sort algorithm for sorting randomly arranged and sorted arrays of varying sizes ranging from 5000 to 50000 elements. It applies Quick Sort to each array, measures the runtime, and plots the runtime on a graph to provide insights into Quick Sort's efficiency and scalability as the 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/ 3

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 2
Number
Experiment Analysis of Runtime for Quick Sort
Title
Resources /
Apparatus
Hardware: Desktop/Laptop Software: VS Code
Required

Description This Python program compares the runtime performance of the Quick Sort
algorithm for sorting randomly arranged and sorted arrays of varying sizes. It
generates arrays ranging from 5000 to 50000 elements, applies Quick Sort to
each array, measures the runtime of the sorting process, and plots the runtime
on a graph. The graph provides visual insights into Quick Sort's efficiency and
scalability as the input size increases, helping to assess its performance
characteristics.

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):
Department of Computer Engineering

return list(range(1, size + 1))

def save_array_to_file(array, filename):


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

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

pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]

return quick_sort(left) + middle + quick_sort(right)

def measure_quick_sort_runtime(array):
start_time = time.time()
quick_sort(array)
end_time = time.time()
return end_time - start_time

for size in range(5000, 50001, 5000):


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

for size in range(5000, 50001, 5000):


array = generate_sorted_array(size)
save_array_to_file(array, f"sorted_array_{size}.txt")

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_quick_sort_runtime(array)
random_runtimes.append(runtime)

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_quick_sort_runtime(array)
sorted_runtimes.append(runtime)
Department of Computer Engineering

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('Quick Sort Runtime Analysis')
plt.legend()
plt.show()

Output

Conclusion Thus, we successfully implemented Analysis on Quick Sort Algo

You might also like