week 13 algo
week 13 algo
**Algorithm**
Step 1: - Start
Step 2: - Declare int n, choice, k, arr[n]
Step 3: - Print "Enter the size of the tree: "
Step 4: - Input n
Step 5: - Print "Enter the elements of the tree: "
Step 6: - For i = 0 to n - 1:
Step 7: - Input arr[i]
Step 8: - Call buildMaxHeap(arr, n)
Step 9: - Do:
Step 10: - Print "Menu:\n1. Display Heap\n2. Find Kth Largest Element\n3.
Extract Maximum Element\n4. Exit"
Step 11: - Input choice
Step 12: - Switch choice:
Step 13: - Case 1:
Step 14: - Call displayHeap(arr, n)
Step 15: - Break
Step 16: - Case 2:
Step 17: - Print "Enter the value of K: "
Step 18: - Input k
Step 1: - Start
Step 2: - For i = (n / 2) - 1 down to 0:
Step 3: - Call heapify(arr, n, i)
Step 4: - Stop
Step 1: - Start
Step 2: - If *n <= 0, return -1
Step 3: - Set maxElement = arr[0]
Step 4: - Set arr[0] = arr[*n - 1]
Step 5: - Decrement *n
Step 1: - Start
Step 2: - Print "Heap elements: "
Step 3: - For i = 0 to n - 1:
Step 4: - Print arr[i] followed by a space
Step 5: - Print newline
Step 6: - Stop