Professional Documents
Culture Documents
Session 1 2018/2019
Lab 7 – Divide and Conquer
Lab outcomes
Exercise 1
Exercise 2
96 48 91 94 31 77 2
Fill up the table below based on the end of each Merge.
p r Array
0 1 48 96 91 94 31 77 2
2 3 48 96 91 94 31 77 2
0 3 48 91 94 96 31 77 2
4 5 48 91 94 96 31 77 2
4 6 48 91 94 96 2 31 77
0 6 2 31 48 77 91 94 96
TCP2101-L7/1-2018.19/WKS
Exercise 3
The following Java program shows the incomplete MergeSort algorithm. Implement and
complete the program then compare the output of the program with the above table, is it the
same?
import java.util.Arrays;
TCP2101-L7/1-2018.19/WKS
Exercise 4
Radix Sort is used to sort the following array. The pseudocode for the radix sort is shown
below.
38 9 38 37 155 197 65
Algorithm radixSort(S, N)
Input sequence S of d-tuples such that (0, …, 0) (x1, …, xd) and
(x1, …, xd) (N - 1, …, N - 1) for each tuple (x1, …, xd) in S
Output sequence S sorted in lexicographic order
for i d downto 1
bucketSort(S, N)
a) Fill up the table below based on the content at the end of each "for" loop iteration or i.
Illustrate the operation of the radix-sort algorithm on the following list of words (assuming that each
cycle sorts in alphabetical order).
COW, DOG, RUG, SEA, ROW, MOB, TEAR, FACE, BOX, TAB, HOUSE
[5 marks]
TCP2101-L7/1-2018.19/WKS
HOUSE HOUSE BOX RUG TEAR
Exercise 5
The following Quick-Select algorithm is used to find the k-th smallest element in the array
below. Note that the pivot is selected from the last element of the current subarray. Assume the
Partition function is stable
31, 77, 4, 96, 42, 94, 55, 91
Algorithm QuickSelect (a, p, r, k)
Input Parameters: array a, start index p, end index r,
target index k.
Output Parameter: a[k] at the correct position.
if (p <= r)
pi = Partition (a, p, r) // pivot index.
if (k == pi)
return
if (k < pi)
QuickSelect (a, p, pi - 1, k)
else
QuickSelect (a, pi + 1, r, k)
Use Quick-Select to find the 4th smallest element in the array. Fill up the table below based on
the end of each call to partition.
Use Quick-Select to find the 3rd smallest element in the array. Fill up the table below based on
the end of each call to partition.
TCP2101-L7/1-2018.19/WKS
Final exam example
Explain (step-by-step) how to use quick-select algorithm to find the 4th smallest element in the array
S = {9, 11, 2, 6, 5, 4, 8, 12, 3}. For every partition in quick-select algorithm, the selected pivot is
always the first element. State the time complexity for the quick-select algorithm.
[4 marks]
1P:
Pivot = 9
S = {2 6 5 4 8 3 9 11 12}, pi>k, call partition on left partition
2P:
Pivot = 2
S = {2 6 5 4 8 3 9 11 12}, pi<k, call partition on right partition
3P:
Pivot = 6
S = {2 5 4 3 6 8 9 11 12}, pi<k, call partition on left
4P:
Pivot = 5
S = {2 4 3 5 6 8 9 11 12}, pi<k, 4th smallest element found
The following Quick-Select algorithm is used to find the k-th smallest element in the array below.
Notice that the pivot is selected from the last element of the current subarray. Use Quick-Select to find
the 4th smallest element in the array. Assume the Partition function is stable.
[7 marks]
Array: 37, 83, 1, 100, 46, 93, 66, 55, 50, 88, 22
S = 37, 83, 1, 100, 46, 93, 66, 55, 50, 88, 22
P = 22
S = 1 22 37 83 100 46 93 66 55 50 88
P = 88
S = 1 22 37 46 66 55 50 83 88 93 100
P = 83
S = 1 22 37 46 66 55 50 83 88 93 100
P = 50
TCP2101-L7/1-2018.19/WKS
S = 1 22 37 46 50 66 55 83 88 93 100
P = 46
S = 1 22 37 46 50 66 55 83 88 93 100
P = k element found
TCP2101-L7/1-2018.19/WKS