高考申論題
110年
[電力工程] 計算機概論
第 五 題
五、詳細說明下列 Java 語言程式的執行過程,並寫出程式的輸出。(20 分)
public class Test
{
public static void main(String[ ] args)
{
int [] numbers = {60, 20, 55, 30, 40, 20};
for(int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;
while(position>0 && numbers[position - 1] > key)
{
numbers[position] = numbers[position - 1];
position--;
}
numbers[position] = key;
for(int count = 0; count < numbers.length; count++)
System.out.print(numbers[count] + " ");
System.out.println();
}
}
}
📝 此題為申論題
思路引導 VIP
這題要求分析一段 Java 程式。首先要辨識出這段程式的演算法類型。觀察 inner while loop:它是將一個 key 往前面已排序的部分插入,並將較大的值後移,這是典型的「插入排序(Insertion Sort)」。作答時需按步驟追蹤外層 for 迴圈的每一次迭代,並精確寫出每次 print 的結果。注意:原始陣列中有兩個 20,要觀察其穩定性(Stability)。
🤖
AI 詳解
AI 專屬家教
【考點分析】 本題考查對 Java 語法、陣列操作及經典演算法「插入排序(Insertion Sort)」執行流程的追蹤能力。 【理論/法規依據】
▼ 還有更多解析內容