# Here is my Heap sort python code: def heapify(arr, n, i): # Find largest among root and children largest =

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

# Here is my Heap sort python code: def heapify(arr, n, i): # Find largest among root and children largest =

Post by answerhappygod »

# Here is my Heap sort python code:
def heapify(arr, n, i): # Find largest among root and children largest = i l = 2 * i + 1 r = 2 * i + 2 if l < n and arr < arr[l]: largest = l if r < n and arr[largest] <arr[r]: largest = r # If root is not largest, swap with largestand continue heapifying if largest != i: arr, arr[largest] =arr[largest], arr heapify(arr, n, largest)def heapSort(arr): n = len(arr) # Build max heap for i in range(n//2, -1, -1): heapify(arr, n, i) for i in range(n-1, 0, -1): # Swap arr, arr[0] = arr[0],arr # Heapify root element heapify(arr, i, 0)arr = [1, 12, 9, 5, 6, 10]heapSort(arr)n = len(arr)print("Sorted array is")for i in range(n): print("%d " % arr, end='')
After running and analyzing the above code. pls answer thefollowing:
1. Perform an asymptotic analysis of the above algorithm.2. Find the best case, worst case, and average case in time andspace complexity 3. Compare the result of heap sort best/worst/average casewith the quicksort and mergesort best, worst, and average case?
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply