Professional Documents
Culture Documents
總分:
總分:113 %
Name:
Student ID #:
Question Score
1 (33%)
2 (15%)
3 (15%)
4 (10%)
5 (20%)
6 (20%)
Total
1. [Simple questions: 33%] 簡答題 (含是非選擇),無需任何說明。
(a) [5%] 依照 asymptotic complexity 的順序由小排到大:
(i) n2lgn, (ii) 8lgn, (iii) lg ((n – 2)!), (iv) lg (n2–1000)
(d) [2%] (是非題) 如果將 merge sort 轉換成 decision tree 的模型,其中最短
的路徑可以是Θ(n)?
MAX-HEAPIFY(A, i)
1 l → LEFT(i)
2 r → RIGHT(i)
3 if l ≤ heap-size[A] and A[l] > A[i]
4 then largest ← l
5 else largest ← i
6 if r ≤ heap-size[A] and A[r] > A[largest]
7 then largest ← r
8 if largest ≠ i
9 then SWAP(A[i], A[largest])
10 MAX-HEAPIFY(A, largest)
4. [Sorting: 10%] 給定 n 個介於 0 與 n3 – 1 的數,建議如何在 O(n) 的時間內完
成排序的工作。
5. [Sorting: 20%] 給三個元素,利用(a) Insertion sort 和(b) Quicksort 分別需要花
多少次的 comparisons 來完成排序的工作?請分別依照 the best case 和 the
worst case,詳細寫下你的過程。你覺得在兩種情形下各用哪種方法較適合
呢?以下附相關的 pseudo codes 供參考。
INSERTION-SORT(A)
1 for j ←2 to length[A]
2 do key ← A[j]
3 /∗ Insert A[j] into the sorted sequence A[1..j-1] */
4 i ←j−1
5 while i > 0 and A[i] > key
6 do A[i + 1] ← A[i]
7 i ←i−1
8 A[i +1] ← key
QUICKSORT(A, p, r)
1 if p<r
2 then q ← PARTITION(A, p, r)
3 QUICKSORT(A, p, q-1)
4 QUICKSORT(A, q+1, r)
PARTITION(A, p, r)
1 x ← A[r]
2 i←p−1
3 for j ← p to r − 1
4 do if A[j] ≤ x
5 then i ← i + 1
6 SWAP(A[i], A[j])
7 SWAP(A[i + 1], A[r])
8 return i+1
6. [Hashing : 20%] 回答以下兩個關於 hashing 的小題。
(a) 請分別用 linear probing 和 double hashing 的方式計算輸入 30, 4, 9, 10, 5,
17, 16, 15, 14, 13 依序放入大小為 11 的 hash table 之後的結果。我們假設
linear probing 所使用的 hash function 為 h1(k) = k mod 11 而 double hashing
所使用的 hash functions 為 h1(k) = k mod 11 and h2(k) = 1 + (k mod 7)。
(b) 你可能會注意到輸入的數字中有一個 17, 16, 15, 14, 13 的遞減串列,請問
這樣串列的出現對於使用 linear probing 和 double hashing 分別有什麼影
響?