Tracing Array Element Swaps
What is the printed output of the heap array (excluding index 0) after executing HeapTest?
public class HeapTest {
public static void main(String[] args) {
int[] heap = {Integer.MIN_VALUE, 10, 20, 30, 40, 50}; // 1-indexed heap; index 0 is unused
// Swap elements at indices 2 and 5
int temp = heap[2];
heap[2] = heap[5];
heap[5] = temp;
// Swap elements at indices 3 and 4
temp = heap[3];
heap[3] = heap[4];
heap[4] = temp;
for(int i = 1; i < heap.length; i++){
System.out.print(heap[i] + " ");
}
}
}
A
10 50 40 30 20
B
10 40 50 30 20
C
10 20 40 30 50
D
10 50 30 40 20
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | varunrajaram1 | 1 | 1 | 0m 00s | 100 |
| #2 | leftoverjluck | 1 | 1 | 0m 50s | 50 |
| #3 | patrickmeijer009 | 1 | 3 | 0m 32s | 48 |
| #4 | kshiva2009m | 1 | 1 | 1m 09s | 31 |
| #5 | 108185265 | 1 | 1 | 1m 25s | 15 |
| #6 | 7017046 | 1 | 1 | 2m 40s | -60 |
| #7 | lsj08030922 | 1 | 1 | 3m 25s | -105 |
| #8 | y.seong2027 | 0 | 1 | 1m 59s | -129 |
| #9 | songqiuhui2012 | 2 | 5 | 11m 08s | -498 |
| #10 | psak12 | 1 | 1 | 1h 36m | -5,716 |
APFIVE