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 : DYN_ARR Size/Cap: 7/8[100,20,6,200,90,150,300] Expected : DYN_ARR Size/Cap: 7/8[300,200,150,100,90,20,6] : Return: None Student : DYN_ARR Size/Cap: 7/8[6,20,90,100,150,200,300] : Return: None DynamicArray has different contents Test Failed: False is not true
Description : This is a test with random values Input : DYN_ARR Size/Cap: 6/8[84848,90990,44,48273,69294,39081] Expected : DYN_ARR Size/Cap: 6/8[90990,84848,69294,48273,39081,44] : Return: None Student : DYN_ARR Size/Cap: 6/8[44,39081,48273,69294,84848,90990] : Return: None DynamicArray has different contents Test Failed: False is not true
This is the problem:
heapsort(da: DynamicArray)-> None:
Write a function that receives a DynamicArray and sorts its content in non-ascending 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 O(N 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 heapsort(da: DynamicArray)-> None:
"""
Sorts the elements in the DynamicArray in non-ascending order using the Heapsort.
The sorting happens in place without creating new data structures.
"""
def _percolate_down_sort(da: DynamicArray, parent: int, heap_size: int)-> None:
"""
Percolates the element at the given index down to restore the heap property.
"""
left_child =2* parent +1
right_child =2* parent +2
largest = parent
# Check if left child exists and is larger than the current node
if left_child < heap_size and da.get_at_index(left_child)> da.get_at_index(largest):
largest = left_child
# Check if the right child exists and is larger than the current node
if right_child < heap_size and da.get_at_index(right_child)> da.get_at_index(largest):
largest = right_child
# If the smallest is not the current node, swap with the largest child and percolate down
if largest != parent:
da._swap(parent, largest)
_percolate_down_sort(da, largest, heap_size)
n = da.length()
for i in range((n //2)-1,-1,-1):
_percolate_down_sort(da, i, n)
# Perform heapsort
for end in range(n -1,0,-1):
da._swap(0, end) # Move the root (max element) to the end
_percolate_down_sort(da,0, end)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!