Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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 HERE

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

PR 17-1 What events would require partnership liquidation?

Answered: 1 week ago

Question

=+What sanctions are available to employers

Answered: 1 week ago

Question

=+ If strikes occur, are they legally regulated?

Answered: 1 week ago

Question

=+industrial action Under what circumstances can unions strike?

Answered: 1 week ago