109 年 國立高雄大學資訊工程學系碩士班《資料結構》
第 1 題5 分
Given a vector of integers, consider the problem of sorting it in increasing order. What is the cost of the insertion sort algorithm in terms of in the worst case?
(a)
(b)
(c)
(d)
登入後即可作答並保存紀錄。
Insertion sort 的時間複雜度在最壞情況下是 。
核心觀念: 插入排序法 (Insertion Sort) 的時間複雜度分析。
解題過程:
插入排序法的基本思想是逐步建立一個已排序的陣列。對於輸入的陣列,它一次處理一個元素,並將該元素插入到前面已排序的子陣列中的正確位置。
考慮一個包含 個整數的向量。在最壞的情況下,輸入陣列是完全逆序的。例如,對於陣列 ,當我們處理每一個元素時,都需要將它與前面所有已排序的元素進行比較和移動,才能找到其正確的位置。
具體來說,對於第 個元素(從 1 開始計數),在最壞情況下,它需要與前面 個元素進行比較,並可能移動 次。
- 當處理第二個元素時,最多需要 1 次比較和 1 次移動。
- 當處理第三個元素時,最多需要 2 次比較和 2 次移動。
- ...
第 2 題5 分
Given a vector of integers, consider the problem of sorting it in increasing order. What is the cost of the insertion sort algorithm in terms of in the best case?
(a)
(b)
(c)
(d)
登入後即可作答並保存紀錄。
插入排序法 (Insertion Sort) 在最佳情況下的時間複雜度是 。
核心觀念: 插入排序法 (Insertion Sort) 的最佳情況時間複雜度分析。
解題過程:
插入排序法的最佳情況發生在輸入的陣列已經是排序好的情況下。例如,對於陣列 。
當我們處理第 個元素時,它需要與前面已排序的子陣列進行比較,以找到其正確的位置。
- 在最佳情況下,第 個元素總是比前面所有元素都大(或者剛好排在最後)。
- 因此,對於第 個元素,只需要與已排序子陣列的最後一個元素進行一次比較,就可以確定它的位置(即插入到最後)。
- 不需要進行任何元素的移動。
對於一個包含 個元素的陣列,我們需要處理 個元素。
- 對於第二個元素,進行 1 次比較。
- 對於第三個元素,進行 1 次比較。
- ...
- 對於第 個元素,進行 1 次比較。
第 3 題10 分
Show that by applying the definition (not using limits).
登入後即可作答並保存紀錄。
核心觀念
本題考查 Big-O 的定義。
對非負函數 與 ,若存在常數 與正整數 ,使得對所有 ,
則稱
本題需證明存在固定常數 ,使 被 上界住,且不可使用極限。
解題方法
將階乘展開:
當 時,每一個因子皆滿足
因此, 的 個因子都不大於 ,相乘可得
第 4 題20 分
A palindrome is a string that can be read backwards, and forwards with the same result. For instance, the following string is a palindrome:
abbbdacadbbba
Write an algorithm in pseudocode using a stack to test if a string is a palindrome.
登入後即可作答並保存紀錄。
核心觀念
堆疊(Stack)具備後進先出(LIFO)之特性。將字串字元依序推入(Push)堆疊後,依序彈出(Pop)所獲得的序列即為原字串的反轉序列。若原字串與反轉序列完全一致,則該字串即為迴文(Palindrome)。
演算法虛擬碼(Pseudocode)
第 5 題20 分
For the max-heap below, draw the resultant max-heap from adding 65 and deleting the maximum element (in this order). Show the detailed steps.
🖼️【此處有附圖,請對照原卷】
(圖為一個 max-heap,節點值為:90, 52, 81, 25, 18, 45, 73, 12, 13, 71)
登入後即可作答並保存紀錄。
核心觀念
Max-heap 必須同時符合:
- 完全二元樹:新增節點放在最後一個空位置。
- 最大堆積性:每個父節點值皆不小於其子節點。
新增元素使用「向上調整」;刪除最大值使用「最後節點補到根,再向下調整」。
解題方法
初始 max-heap 以陣列表示為:
其樹狀結構為:
90
/ \
52 81
/ \ / \
25 18 45 73
/ \ /
12 13 71
一、加入 65
將 放入最後空位,成為 的右子節點:
90
/ \
52 81
/ \ / \
25 18 45 73
/ \ / \
12 13 71 65
因為 ,交換:
此時 的父節點為 ,且 ,再次交換:
加入 後的 max-heap 為:
第 6 題
- (a) For the following data sequence:
5, 9, 3, 15, 2, 17
Show how to generate the binary search tree. (10%)
(b) Using the binary search tree constructed from (a), show the detailed steps about how to find which integer is closest to 8. (Hint: you may use any meaningful distance metric for computing the distance between two values.) (10%)
(c) The question in (b) is for finding the nearest neighbor of a given 1-dimensional instance. Now suppose you are given the following six 2-dimensional instances:
(5, 7), (9, 4), (3, 6), (15, 2), (2, 5), (17, 8)
Show how to generate the k-d tree (k-dimensional tree), where k = 2. (10%)
(d) Show how to use the k-d tree from (c) to find the nearest neighbor of (14,3). (Hint: you may use Euclidean distance for computing the distance between any pair of 2-dimensional instances.) (10%)
登入後即可作答並保存紀錄。
(a) 建立二元搜尋樹(Binary Search Tree, BST)
建構規則:依序讀入資料,若新數值小於目前節點走左子樹,大於走右子樹。
插入步驟:
- 插入 :建立根節點 。
- 插入 :,位於 的右子節點。
- 插入 :,位於 的左子節點。
- 插入 :,位於 的右子節點。
- 插入 :,位於 的左子節點。
- 插入 :,位於 的右子節點。
二元搜尋樹結構:
5
/ \
3 9
/ \
2 15
\
17
【答案】二元搜尋樹結構如上圖所示。
(b) 尋找最接近 8 的整數
距離採用絕對值距離 。自根節點向下搜尋,同時維護最小距離 與對應的最近節點。
搜尋步驟:
- 訪問根節點 :
- 計算距離:。
- 目前最近節點設為 ,。
- 因 ,向右子樹搜尋。
- 訪問節點 :
- 計算距離:。
- 因 ,更新最近節點為 ,。
- 因 ,向左子樹搜尋。
- 到達 NULL(空節點):
- 搜尋終止。
【答案】最接近 8 的整數為 9(距離為 1)。
(c) 建立 2-d Tree ()
建構規則:
- 依深度 輪流交替切分軸(從 開始)。
- 偶數層():依 座標比較。
- 奇數層():依 座標比較。
依序插入步驟:
- 插入 (深度 0,依 ):建立根節點 。
- 插入 (深度 0,依 ):,作為 之右子節點。
- 插入 (深度 0,依 ):,作為 之左子節點。
- 插入 :
- 深度 0(依 ): 走右至 。
- 深度 1(依 ): 作為 之左子節點。
- 插入 :
- 深度 0(依 ): 走左至 。
- 深度 1(依 ): 作為 之左子節點。
- 插入 :