# 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?
# Here is my Heap sort python code: def heapify(arr, n, i): # Find largest among root and children largest =
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am