Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2. Convert a given array of integers into a heap. You will do that by applying a certain number of swaps to the array. Swap

image text in transcribed

image text in transcribed

image text in transcribed

2. Convert a given array of integers into a heap. You will do that by applying a certain number of swaps to the array. Swap is an operation which exchanges elements a, and ajof the array a for some i and j. Note that you will need to use a min-heap instead of a max-heap in this problem. Input Format. The first line of the input contains single integer n. The next line contains n space- separated integers ai Constraints. 1 n 100 000; 0 sijsn 10S ao,a -1 109. All ai are distinct. Output Format. The first line of the output should contain single integer mthe total number of swaps. The next m lines should contain the swap operations used to convert the array a into a heap. Each swap is described by a pair of integers ij- the 1-based indices of the elements to be swapped. After applying all the swaps in the specified order the array must become a min-heap. You also required to measure the elapsed time, using System.currentTimeMillis). Please print the elapsed time in seconds Sample 1. Input: 54321 Output: 1 4 Explanation: After swapping elements 4 in position 1 and 1 in position 4 the array becomes 5 1 3 24. After swapping elements 5 in position 0 and 1 in position 1 the array becomes 15 324. After swapping elements 5 in position 1 and 2 in position 3 the array becomes 1 2354, which is already a heap, because ao 12 a1,ao 1

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

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

More Books

Students also viewed these Databases questions

Question

Question Attempts: Unlimited 10051

Answered: 1 week ago

Question

(1 point) Calculate 3 sin x cos x dx.

Answered: 1 week ago

Question

Using Language That Works

Answered: 1 week ago

Question

4. Are my sources relevant?

Answered: 1 week ago