Question: My code keeps failing, error messages: Description : This is the same test as in the PDF Examples Input : DYN _ ARR Size /
My code keeps failing, error messages:
Description : This is the same test as in the PDF Examples Input : DYNARR SizeCap: Expected : DYNARR SizeCap: : Return: None Student : DYNARR SizeCap: : Return: None DynamicArray has different contents Test Failed: False is not true
Description : This is a test with random values Input : DYNARR SizeCap: Expected : DYNARR SizeCap: : Return: None Student : DYNARR SizeCap: : Return: None DynamicArray has different contents Test Failed: False is not true
This is the problem:
heapsortda: DynamicArray None:
Write a function that receives a DynamicArray and sorts its content in nonascending order,
using the Heapsort algorithm. You must sort the array in place, without creating any data
structures. This function does not return anything.
You may assume that the input array will contain one or more homogeneous elements
either all numbers, or strings, or custom objects, but never a mix of these You do not
need to write checks for these conditions.
The runtime complexity of this implementation must be ON log N If the sort uses an
algorithm other than Heapsort, you will not receive any points for this portion of the
assignment, even if your function passes Gradescope.
My code:
def heapsortda: DynamicArray None:
Sorts the elements in the DynamicArray in nonascending order using the Heapsort.
The sorting happens in place without creating new data structures.
def percolatedownsortda: DynamicArray, parent: int, heapsize: int None:
Percolates the element at the given index down to restore the heap property.
leftchild parent
rightchild parent
largest parent
# Check if left child exists and is larger than the current node
if leftchild heapsize and dagetatindexleftchild dagetatindexlargest:
largest leftchild
# Check if the right child exists and is larger than the current node
if rightchild heapsize and dagetatindexrightchild dagetatindexlargest:
largest rightchild
# If the smallest is not the current node, swap with the largest child and percolate down
if largest parent:
daswapparent largest
percolatedownsortda largest, heapsize
n dalength
for i in rangen :
percolatedownsortda i n
# Perform heapsort
for end in rangen :
daswap end # Move the root max element to the end
percolatedownsortda end
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
