Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For Q2c. If we don't care about the original ordering, then we can use a heap to design an algorithm that runs faster than the
For Q2c. If we don't care about the original ordering, then we can use a heap to design an algorithm that runs faster than the one in part (b). Design and implement an algorithm that returns an array of the k largest elements of A using a heap.
2(b) Now suppose that the order of the array was important. Design and implement an algorithm that returns an array of the k largest elements of A in their original order, and it should run in (nk) time. For example, BiggestinOrder([0,5,1,3,4), 3) should return (5,3,4]. def BiggestInOrder(A, k): # YOUR CODE HERE #store copy of number copy = [] for data in A: copy.append(data) #sort number in reverse order A. sort(reverse=True) getFirstK = A[:k] order Sort=[] for data in A: if data in getFirstK: order Sort.append(data) if len(orderSort) ==k: break return order Sort res - BiggestInOrder([0,5,1,3,4], 3); print (res) [5, 4, 3] 2(c) If we don't care about the original ordering, then we can use a heap to design an algorithm that runs faster than the one in part (b). Design and implement an algorithm that returns an array of the k largest elements of A using a heap. def BiggestOutOfOrder (A, k): # YOUR CODE HEREStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started