線上測驗 Chapter 2 Getting Start Insertion Sort Given an insertion

advertisement
線上測驗 Chapter 2 Getting Start
1. Insertion Sort
Given an insertion sort with the pseudo-code below and a number of sequence A
= { 19, 26, 5, 47, 34, 10 }. Please answer the following questions:
INSERTION-SORT(A)
1 for j = 2 to A.length
2
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
7
8
A[i+1] = A[i]
i=i–1
A[i+1] = key
a. How many times of swapping will be needed to sort the sequence A, i.e. how
many times is line 6 been executed ? Ans : 7
2. Merge Sort
Given a merge sort with the pseudo-code below and a number of sequence A =
{19, 26, 5, 47, 34, 10} with parameters p = 1 and r = 6.
Please answer the following questions:
MERGE-SORT( A, p, r )
1 if p < r
2
q = ⌊(𝑝 + 𝑟)/2⌋
3
MERGE-SORT( A, p, q)
4
MERGE-SORT(A, q+1, r)
5
MERGE(A, p , q, r)
MERGE( A, p, q, r )
1 𝑛1 = q – p + 1
2 𝑛2 = r – q
3 let L[1.. 𝑛1 +1] and R[1.. 𝑛2 +1] be new arrays
4 for i = 1 to 𝑛1
5
L[i] = A[p + i - 1]
6 for j = 1 to 𝑛2
7
R[j] = A[q + j]
8
L[ 𝑛1 +1] = ∞
9 R[𝑛2 +1] = ∞
10 i = 1
11 j = 1
12 for k = p to r
13
if L[i] ≤ R[j]
14
A[k] = L[i]
15
i=i+1
16
else
17
A[k] = R[j]
18
j=j+1
a. How many times of the function MERGE is executed to complete the sorting?
Ans : 5
3. Use mathematical induction to show that when n is an exact power of 2, the
solutions of the recurrence
𝑇(𝑛) = {
2
𝑛
2
4𝑇 ( ) + 𝑛
2
if 𝑛 = 2
if 𝑛 = 2𝑘 , for 𝑘 > 1
are:
a). T(n) = lg 𝑛.
b). T(n) = 𝑛 lg 𝑛.
c). T(n) = 𝑛2 lg 𝑛.
d). T(n) = 𝑛2 .
4.
1
𝑇(𝑛) = {
Find T(125) = ?
𝑛
2𝑇 ( ) + 2𝑛
5
if 𝑛 = 1
if 𝑛 > 1
Ans : 398
5. Insertion Sort vs. Selection Sort
Given an insertion sort and a selection sort with the pseudo-codes below:
INSERTION-SORT( A )
1 for j = 2 to A.length
2
key = A[j]
3
// Insert A[j] into the sorted sequence A[1..j-1].
4
i=j-1
5
6
7
8
while i > 0 and A[i] > key
A[i+1] = A[i]
i=i–1
A[i+1] = key
SELECTION-SORT( A )
1 /* advance the position through the entire array */
2 for j = 0 to A.length-1
3 /* find the min element in the unsorted A[ j..A.length-1 ] */
4
5
6
/* assume the min is the first element */
iMin = j
/* test against elements after j to find the smallest */
7 for i = j+1 to A.length
8
/* if this element is less, then it is the new minimum */
9
if A[ i ] < A[ iMin ]
10
/* found new minimum; remember its index */
11
iMin = i
12
/* iMin is the index of the minimum element.
13
14
Swap it with the current position */
if iMin != j
swap(A[ j ], A[ iMin ])
a. To sort the sequence {6, 10, 18, 95, 156, 243, 500, 999}, which sorting algorithm
has better time-complexity?
(1) Insertion Sort (2) Selection Sort (3) They have the same time-complexity.
b. To sort the sequence {36, 28, 25, 19, 15, 9, 4, 1}, which sorting algorithm has
better time-complexity?
(1) Insertion Sort (2) Selection Sort (3) They have the same time-complexity.
c. For the average case, which sorting algorithm has better time-complexity?
(1) Insertion Sort (2) Selection Sort (3) They have the same time-complexity.
6. Which of the following is False about merge sort algorithm?
a). It is a comparison sorting algorithm.
The unsorted array is divided into sublists, which are in turn divided into more
b).
sublists.
The unsorted array is divided into sublists, each sublist is then sorted recursively
c).
by re-applying the algorithm.
d). All of the above statements are true
7. Bubble Sort
Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly
swapping adjacent elements that are out of order.
BUBBLESORT(A)
1 for i = 1 to A.length-1
2
3
4
for j = A.length downto i+1
if A[ j ] < A[ j-1 ]
exchange A[ j ] with A[ j-1 ]
a. Please choose the correct process of array A= [1, 43, 6, 79, 50, 2] during the
execution of the BUBBLESORT(A).
i. [1,43,6,79,50,2] -> [1,2,6,79,50,43] -> [1,2,6,43,50,79]
ii. [1,43,6,79,50,2] -> [1,6,43,2,50,79] -> [1,2,6,43,50,79]
iii. [1,43,6,79,50,2] -> [1,6,43,79, 50,2] -> [1,6,43,79,2,50] -> [1,6,43,2,79,50] ->
[1,6,43,2,50,79] -> [1,6,2,43,50,79] -> [1,2,6,43,50,79]
iv. [1,43,6,79,50,2] -> [1,6,43,79, 50,2] -> [1,6,43,2,79,50] -> [1,6,2,43,50,79]
ANS. i
b. What is the worst-case running time of BUBBLESORT?
i. O(n3)
ii. O(n2)
iii. O(n)
iv. O(log n)
ANS. ii
c. What is the best-case running time of BUBBLESORT?
i. O(n3)
ii. O(n2)
iii. O(n)
iv. O(log n)
ANS. iii
8. Horner’s rule
The following code fragment implements Horner’s rule for evaluation a polynomial
𝑛
P(x) = ∑𝑘=0 𝑎𝑘 𝑥 𝑘
= 𝑎0 + 𝑥(𝑎1 + 𝑥(𝑎2 + ⋯ + 𝑥(𝑎𝑛−1 + 𝑥𝑎𝑛 ) … )) ,
Given the coefficients 𝑎0 𝑎1, , . . . , 𝑎𝑛 and a value for x:
1 y=0
2 for i = n downto 0
3
y=
Please choose the correct equation to fill in the blank.
A. 𝑎𝑖 + 𝑥
B. 𝑥 ∗ 𝑖
C. 𝑎𝑖
D. 𝑎𝑖 + 𝑥 ∗ 𝑦
ANS. D
Download