Merge k-sorted Arrays • Create a priority queue. This priority queue will contain numbers from the jagged arrays as prio

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

Merge k-sorted Arrays • Create a priority queue. This priority queue will contain numbers from the jagged arrays as prio

Post by answerhappygod »

Merge k-sorted Arrays• Create a priority queue. This priority queue will contain numbersfrom the jaggedarrays as priorities and the row index from which they were addedas the items. Thus,the priority queue will contain objects of PriorityQueuePair class,and it will use thePriorityQueuePairComparator to compare these objects.• Create an dynamic array. This will store the numbers from all therows in sorted order.• Insert the first number of each row of lists into the priorityqueue – the item is therow index and the priority is the number itself.• Create an array indexes having the same length as lists. Fill thearray with 1. Thisarray will help us keep track of the numbers from each row thathave already been
11
added to the priority queue.• As long as (the priority queue is not empty), do thefollowing:– Extract the minimum element from the priority queue.– Add the priority of the minimum element to the dynamic array;this gives youthe value of the current smallest.– Let minItem be the item of minimum element; this gives you therow index ofthe current smallest.– If indexes[minItem] is less than the size of the row at indexminItem, then thereare still numbers left in the row corresponding to minItem. We willadd the nextone given by indexes[minItem]∗ insert a new element into the priority queue – the item isminItem andpriority is the value at row minItem and columnindexes[minItem]∗ increment indexes[minItem]• Return the dynamic array.
_____________________________________________________________
please complete method above and previous solutions given onchegg are wrong. I have final submission today please help.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply