Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! This must be completed in Python. All .py files have been provided. File: profiler.py Project 10.10 Updated to test heap sort. import

Hello! This must be completed in Python. All .py files have been provided.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

"""

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: test.py Project 10.10 Runs a profile of a heap sort with increasing list sizes. from profiler import Profiler from algorithms import heapSort p - Profiler for size in (100, 1000, 10000, 100000): p.test(heapSort, size = size) 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 Item

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions