# PYTHON: Runtime Analysis & BST

Hi assignment should be written in python only. Assignment details are below.

Loading...

Hi assignment should be written in python only. Assignment details are below.

Assignment 2

Note: Submitting wrong files or in the wrong format or corrupted files or missing files will

not give you permission to re-submit. It is your responsibility to submit all the correct files

on time.

1. Compare: Write 2 page essay, in your own words, comparing and contrasting insertion,

selection, quicksort, mergesort algorithms that we went over in class (max 12-point font).

Diagrams or tables are not included in the length and your comparison must be in the form of

essay. Submit essay as PDF or Word document to be checked by TurnItIn (NOT inside of a

zip/rar file). Note that work which is not written in student’s own words will not earn credit.

Changing words to their synonyms (or running through translation program) is not sufficient

and often changes the meaning of the context or makes it read as garbage because technical

terms are changed incorrectly. Essay that makes no sense and cannot be understood will not

earn credit. The submitted work must be graduate level quality and will be graded as such.

2. Analysis:

a) Write Python code to generate input data for sorting algorithms as follows

Number of elements: 2000, 4000, 6000, 8000, 10000, 12000

Types of data: sorted, reverse sorted, random

b) Capture runtime (how long it took to sort):

Run your input data sets that you generated (18 inputs) through each of the

sorting algorithms provided for this assignment

Be careful you do not pass the sorted input to the next sort for reverse and

random!!!

Put all your results in a table

c) In your program graph the runtime results using line graph as follows:

3 dataset type results for the same algorithm (2 graphs)

same dataset type for the different algorithms (3 graphs)

you should have at minimum five graphs

d) Write an analysis report which includes:

runtime results in a table

time complexity analysis of each sorting algorithm (code) for best and worst

time complexity

analysis of the data relative to growth rate (are the results linear, quadratic,

etc.)

each generated graph and discussion of each graph to include the differences

in performance as shown in the graph and differences in rate of growth

summary comparing the algorithms and their results relative to the different

data sets based on the individual graph discussion and the worst versus best

case scenarios

final conclusion of the analysis to include which algorithm seems more

efficient relative to different data set types and why and whether the runtime

results support the time complexity of the algorithms

e) Submit code in one zip/rar file; and analysis report as PDF or Word document to be

checked by TurnItIn (NOT in zip/rar);

3. BST: 1. Using the bst code given for assignment 2 add the following operations:

a) Implement get_by_type bst operation which take as parameter type value and returns a Python list with all the art objects that match that type. The operation

must be self-contained - you cannot add class attributes

b) Implement insertM operation which takes as a parameter a Python list with art objects and inserts the objects into the bst

2. Add test cases to bst-test to demonstrate that the new operations work correctly 3. Write report analyzing the time and space complexity of each of the new operations

for best and worst complexity. Make sure you explain in writing your analysis and

give all the calculations as well as the final rate of growth. A guess of the final rate of

growth time or space complexity (without calculations and explanation) will not earn

any credit

4. Submit all code in a single zip/rar file and time/space complexity analysis in separate pdf/word file to be checked by TurnItIn (not in zip/rar)

Grading Rubric

Points Criteria

20 Compare: The compare and contrast is written in student’s own words in essay format

with introduction/body/conclusion format, represents graduate level depth and

breath, has at least 50% original thought, uses appropriate examples to support

discussion, and does not plagiarize any of the materials

40 Analysis: Correctly implements datasets, experiment, and plotting code

Analysis is submitted as PDF or Word file (not in zip/rar so can be run by

TurnitIn)

Correctly analyzes time complexity of each algorithm

Correctly analyzes the runtime performance of the sort methods for each

graph and discusses how they compare to each other and to best-worst in the

written report

Each graph is analyzes individually and then summarized relative to algorithm

and each data type category

Provides final conclusions in the written report to include whether runtime

results support the time complexities

40 BST: Correctly implements new operations adding to the given bst code

Correctly implements new test cases adding to the given test code

Correctly analyzes in writing the time and space complexity of the external

operations giving details and the final rate of growth value

from a2bst import * # create art instances art1 = art(1234, 'summer', 568.90, 'painting') art2 = art(1235, 'fall', 569.90, 'drawing') art3 = art(1236, 'girl', 89.90, 'painting') # create binary search tree and insert art objects bst = bst() bst.insert(art1) bst.insert(art2) bst.insert(art3) # traverse tree in order and print art values bst.traverse() # find and print art object based on key value print() print(bst.finditem(1234))

class art: def __init__(self, serial, name, price, type): self.serial = serial self.name = name self.price = price self.type = type @property def key(self): return self.serial def __str__(self): return 'serial={} name={} price={} type={}'.format(self.serial, self.name, self.price, self.type) class node: def __init__(self, nitem): self.item = nitem self.left = None self.right = None @property def key(self): return self.item.key class bst: def __init__(self): self.root = None def finditem(self, key): n = self.findR(self.root, key) if n is None: raise ValueError('Item cannot be found with the given key!') return n.item def findR(self, root, key): if root is None: return root if root.key == key: return root if key > root.key: return self.findR(root.right, key) if key < root.key: return self.findR(root.left, key) def insert(self, data): if self.findR(self.root, data.key) is not None: raise ValueError('Object exists : Cannot insert duplicate object into tree') else: self.root = self.insertR(self.root, data) def insertR(self, root, key): n = node(key) if root is None: root = n if n.key < root.key: root.left = self.insertR(root.left, key) if n.key > root.key: root.right = self.insertR(root.right, key) return root def traverse(self): self.traverse_in_orderR(self.root) def traverse_in_orderR(self, root): if root is not None: self.traverse_in_orderR(root.left) print(root.item) self.traverse_in_orderR(root.right)

def sort1a(nlist): size = len(nlist) for i in range(size): for j in range(size-1): if nlist[j] > nlist[j+1]: nlist[j], nlist[j+1] = nlist[j+1], nlist[j] return nlist def sort1b(nlist): size = len(nlist) change = False for i in range(size): if not change and i > 500: break for j in range(size-i-1): if nlist[j] > nlist[j+1]: nlist[j], nlist[j+1] = nlist[j+1], nlist[j] change = True return nlist