| preferred AP College board partner for AP classes
AP Computer Science A/Unit 1: Primitive Types
Start Practice TestPractice Test
About Exam
hard Solved by 2 students
Min Heap Sift Up Algorithm
< Prev
Next >

What is printed to the console when the following code is executed?

public class HeapifyTest {
    public static void main(String[] args) {
        int[] heap = {5, 8, 12, 15, 3}; // 0-indexed min heap (except element 3 is out of place)
        
        int child = 4;  // value 3
        while(child > 0) {
            int parent = (child - 1) / 2;
            if(heap[child] < heap[parent]) {
                int temp = heap[child];
                heap[child] = heap[parent];
                heap[parent] = temp;
                child = parent;
            } else {
                break;
            }
        }
        
        for(int i = 0; i < heap.length; i++){
            System.out.print(heap[i] + " ");
        }
    }
}
A

3 8 12 15 5

B

3 5 12 15 8

C

5 3 12 15 8

D

5 8 12 15 3

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1richa.tuli13 0m 16s 64
#2turan.musayev.2807202001 0m 58s -68
#3lsj0803092212 8h 47m -31,569
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy