Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

This programming assignment is to implement the following three sorting algorithms and to collect time performance measurements on them. 1. Mergesort, 2. Heapsort, 3. Quicksort. The running time of...

1 answer below »
This programming assignment is to implement the following three sorting algorithms and to collect time
performance measurements on them.
1. Mergesort,
2. Heapsort,
3. Quicksort.
The running time of cach sorting algorithm must be measured in two ways:
o Count the number of key comparisons, COMPCOUNT. To obtain this count, it is suggeted that
you write a function COMPARE(X,Y), which will perform a comparison between X and Y and
increment COMPCOUNT. Then, wherever you need to perform a key comparison in your algorithms,
you simply make a call to this function.
® Use the actual measured CLOCK time.
You need to ca
y out the experiment in two parts.
1 A small a
ay size, n = 32, to verify co
ectness
Run cach algorithm on three sets of data:
(1) Sorted; (2) Reversely sorted; and (3) Randomly generated.
For simplicity, you may use integers. For each case, make sure the same original data is input to all three
algorithms. Print both the sorted a
ay and measured number of comparisons for cach case to show the
co
ectness of your algorithms.
2 Large a
ay sizes to determine time complexity
Run cach sorting algorithm on the following a
ay sizes:
on=210=1,024

15 32,768
on =220 — 1,048,576
For cach value of n, produce an a
ay of n randomly generated elements only once. (For simplicity, you
may use integers.) Again, make sure you provide the same randomly-generated a
ay of data to all three
sorting algorithms.
Tabulate the performace results, listing both the number of comparisons and the measured clock times.
Use the tabulated number of comparisons to determine the time complexity. For example, does the data
for quicksort verify O(n?) or O(nlogn) time, and what is the constant factor? Note that since you use a
andom sequence to sort, the running times should co
espond to the average-case time complexities.
Answered 7 days After Feb 08, 2023

Solution

Kamala answered on Feb 15 2023
33 Votes
Programming Assignment
1. A very small a
ay , n=32, to verify co
ectness
Merge Sort
import random
def COMPARE(X,Y):
global COMPCOUNT
COMPCOUNT=COMPCOUNT+1
if X return True
else:
return False
def merge_sort(lst):
global count
if len(lst)>1:
mid=len(lst)
2
left=lst[:mid]
right=lst[mid:]
merge_sort(left)
merge_sort(right)
i=0
j=0
k=0
while i
if COMPARE(left[i],right[j]):
lst[k]=left[i]
i=i+1
else:
lst[k]=right[j]
j=j+1
k=k+1
while i lst[k]=left[i]
i=i+1
k=k+1
while j lst[k]=right[j]
j=j+1
k=k+1
Heap Sort
def heapify(a
, n, i):
largest = i # largest value
l = 2 * i + 1 # left
r = 2 * i + 2 # right
# if left child exists
if l < n and COMPARE(a
[i],a
[l]):
largest = l
# if right child exits
if r < n and COMPARE(a
[largest],a
[r]):
largest =
# root
if largest != i:
a
[i],a
[largest] = a
[largest],a
[i] # swap
# root.
heapify(a
, n, largest)
def heapSort(a
):
n = len(a
)
# maxheap
for i in range(n-1, -1, -1):
heapify(a
, n, i)
# element extraction
for i in range(n-1, 0, -1):
a
[i], a
[0] = a
[0], a
[i] # swap
heapify(a
, i, 0)

def COMPARE1(X,Y):
global COMPCOUNT
COMPCOUNT=COMPCOUNT+1
if X<=Y:
return True
else:
return False
def partition(a
,low,high):
i = ( low-1 )
pivot = a
[high]
for j in range(low , high):
if COMPARE(a
[j],pivot):
i = i+1
a
[i],a
[j] = a
[j],a
[i]
a
[i+1],a
[high] = a
[high],a
[i+1]
return ( i+1 )
Quick Sort
def quickSort(a
,low,high):
if low < high:
pi = partition(a
,low,high)
quickSort(a
, low, pi-1)
quickSort(a
, pi+1, high)

#sorted a
ay
lst1=[]
for i in range(0,(2**5)):
lst1.append(i)
print("......................Sorted list.........................")
print(".........MERGE SORT.....")
lst=lst1.copy()
COMPCOUNT=0
print("The sorted input: ",lst)
merge_sort(lst)
print("The sorted numbers are: ",lst)
print("Number of Comparision",COMPCOUNT)
print(".........HEAP SORT.....")
a
=lst1.copy()
COMPCOUNT=0
print("The sorted input: ",a
)
heapSort(a
)
print("The sorted numbers are: ",a
)
print("Number of Comparision",COMPCOUNT)
print(".........QUICK SORT.....")
a
=lst1.copy()
COMPCOUNT=0
print("The sorted input: ",a
)
quickSort(a
,0,len(a
)-1)
print("The sorted numbers are: ",a
)
print("Number of Comparision",COMPCOUNT)
#reserve sorted a
ay
lst1=[]
for i in range(32,0,-1):
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here