Question
FOLLOW INSTRUCTIONS BELOW. Complete the heapsort function in ALGORITHMS.PY in the picture provided below. All of the code needed to test and run the algorithms.py
FOLLOW INSTRUCTIONS BELOW. Complete the heapsort function in ALGORITHMS.PY in the picture provided below. All of the code needed to test and run the algorithms.py file has been provided. I cannot insert the code as text for this question as Chegg will not allow a question that long.
"""
File: profiler.py
Project 10.10
Updated to test heap sort.
"""
import time
import random
class Profiler(object):
def test(self, function, lyst = None, size = 10,
unique = True, comp = True, exch = True,
trace = False):
self.comp = comp
self.exch = exch
self.trace = trace
if lyst != None:
self.lyst = lyst
elif unique:
self.lyst = list(range(1, size + 1))
random.shuffle(self.lyst)
else:
self.lyst = list()
for count in range(size):
self.lyst.append(random.randint(1, size))
self.exchCount = 0
self.cmpCount = 0
self.startClock()
function(self.lyst, self)
self.stopClock()
print(self)
def exchange(self):
"""Counts exchanges if on."""
if self.exch:
self.exchCount += 1
if self.trace:
print(self.lyst)
def comparison(self):
"""Counts comparisons if on."""
if self.comp:
self.cmpCount += 1
def startClock(self):
"""Record the starting time."""
self.start = time.time()
def stopClock(self):
"""Stops the clock and computes the elapsed time
in seconds, to the nearest millisecond."""
self.elapsedTime = round(time.time() - self.start, 3)
def __str__(self):
"""Returns the results as a string."""
result = "Problem size: "
result += str(len(self.lyst)) + " "
result += "Elapsed time: "
result += str(self.elapsedTime) + " "
if self.comp:
result += "Comparisons: "
result += str(self.cmpCount) + " "
if self.exch:
result += "Exchanges: "
result += str(self.exchCount) + " "
return result
In the algorithms.py file, complete the following 1. Implement and test a heapSort function that is based on the heap class developed in this chapter. 2. Profile this function using the technology developed in Chapter 3, "Searching, Sorting, and Complexity Analysis," to verify its run-time complexity. To test your program run the main() method in the test.py file. Your program's output should look like the following: Problem size: 100 Elapsed time: 0.002 Comparisons: 710 Exchanges: 522 Problem size: 1000 Elapsed time: 0.043 Comparisons: 10537 Exchanges: 8683 Problem size: 10000 Elapsed time: 0.315 Comparisons: 139599 Exchanges: 121188 Problem size: 100000 Elapsed time: 3.502 Comparisons: 1728694 Exchanges: 1543687 File: algorithms.py Project 10.10 Updates to profile heap sort. The heap now needs a reference to the profiler so it can record comparisons and exchanges. Algorithms configured for profiling. from profiler import Profiler from arrayheap import ArrayHeap def heapSort(lyst, profiler): #Your code here File: arrayheap.py Defines the class ArrayHeap from abstractcollection import AbstractCollection class ArrayHeap (AbstractCollection): def __init__(self, sourceCollection = None, profiler = None): self.heap = list) self.profiler = profiler AbstractCollection. __init__(self, sourceCollection) def peek (self): if self.isEmpty(): raise AttributeError("Heap is empty") return self.heap[@] - 1 def add(self, item): self.size += 1 self.heap.append(item) curPos - len(self.heap) while curPos > 0: if self.profiler: self.profiler.comparison parent (curpos - 1) // 2 parentItem = self.heap [parent] if parentItem lastIndex: break if rightChild > lastIndex: maxchild leftChild; else: if self.profiler: self.profiler.exchange) left Item self.heap[leftChild] rightItem = self.heap[rightChild] if left Item 0: if self.profiler: self.profiler.comparison parent (curpos - 1) // 2 parentItem = self.heap [parent] if parentItem lastIndex: break if rightChild > lastIndex: maxchild leftChild; else: if self.profiler: self.profiler.exchange) left Item self.heap[leftChild] rightItem = self.heap[rightChild] if left ItemStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started