卷別
資料結構
選擇國立中央大學 2026 · 考試 · 第 28 題
考科資料結構
Answer Questions 28-30 according to the following C code.
1 #include <stdio.h>
2 #include <stdlib.h>
3 int counter=0;
4 void max_heapify(int A[],int n,int i) {
5 int largest=i;
6 int left=2*i+1;
7 int right=2*i+2;
8 counter++;
9 if (left<n && A[left]>A[i])
10 largest=left;
11 if (right<n && A[right]>A[largest])
12 largest=right;
13 if (largest!=i) {
14 int temp=A[i];
15 A[i]=A[largest];
16 A[largest]=temp;
17 max_heapify(A,n,largest);
18 }
19 }
20 void build_heap(int A[],int n) {
21 for (int i = (n/2)-1;i>=0;i--)
22 max_heapify(A,n,i);
23 }
24 int main(){
25 int A[]={4,1,3,2,16,9,10,14,8,7};
26 int n = sizeof(A)/sizeof(A[0]);
27 build_heap(A,n);
28 for (int i=0;i<n;++i)
29 printf("%d,",A[i]);
30 printf("\n");
31 printf("counter:%d\n",counter);
32 return 0;
33 }
What does the for-loop between Lines 28 and 29 print?
- (A)4,1,3,2,16,9,10,14,8,7,
- (B)16,14,10,8,7,9,3,2,4,1,
- (C)1,2,3,4,7,8,9,10,14,16,
- (D)16,14,10,8,7,3,9,1,4,2,
- (E)16,14,10,9,8,7,4,3,2,1,