You are on page 1of 6

TCP2101 Algorithm Design and Analysis

Session 1 2018/2019
Lab 7 – Divide and Conquer

Lab outcomes

By the end of today’s lab, you should be able to


 Understand how Merge Sort works.
 Understand how Radix Sort works.
 Understand how Quick Select works.

Exercise 1

Complete the mergesort pseudocode below

Input Parameters: array a, start index p, end index r.


Output Parameter: array a sorted.
Mergesort (a, p, r) {
// determine to continue to call mergesort or not
If (p<r){
// Divide: divide array into two nearly equal parts.
tmp = (p+r)/2
// Recur: sort each half.
Mergesort(a,p,m)
Mergesort(a,m+1,r)
// Conquer: merge the two sorted halves.
Merge(a,p,m,r)
}
}

Exercise 2

Using the mergesort algorithm, sort the array of 7 integers below.

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;

public class TestMergeSort {


public static void main (String[] args) {
// Put your code here!
int n = 7;
int[] A = { 96, 48, 91, 94, 31, 77, 2 };
System.out.println ("Before MergeSort " + Arrays.toString(A));
startMergeSort (A, A.length);
System.out.println ("After MergeSort " + Arrays.toString(A));
}

static void startMergeSort (int[] A, int n) {


int[] Temp = new int[n];
mergesort (A, Temp, 0, n-1);
}

static void mergesort (int[] A, int[] Temp, int p, int r) {


if (p<r){
int tmp = (p+r)/2;
mergesort(A,Temp,p,tmp);
mergesort(A,Temp,tmp+1,r);
merge(A,Temp,p,tmp,r);
}
}

static void merge (int[] A, int[] Temp, int p, int m, int r) {


int i, j;
for (i = m + 1; i > p; i--)
Temp[i - 1] = A[i - 1];
for (j = m; j < r; j++)
Temp [r + m - j] = A[j + 1];
// Misch-Schritt
for (int k = p; k <= r; k++)
if (Temp[j] < Temp [i])
A[k] = Temp[j--];
else
A[k] = Temp[i++];
}
}

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.

Original array Array after d = 3 Array after d = 2 Array after d = 1


38 155 09 009
9 65 37 037
38 37 38 038
37 197 38 038
155 38 155 065
197 38 65 155
65 9 197 197

b) What is the time complexity for Radix Sort?


O[n+N]

Final exam example

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]

1st 2nd 3rd 4th 5th


COW COW SEA SEA BOX
DOG DOG TEAR FACE COW
RUG RUG MOB SEA DOG
SEA SEA TAB TEAR FACE
ROW ROW FACE MOB HOUSE
MOB MOB DOG DOG MOB
TEAR BOX RUG HOUSE ROW
FACE TAB HOUSE COW RUG
BOX TEAR COW ROW SEA
TAB FACE ROW BOX TAB

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.

p r p <= r Pivot Array after Partition pi k == pi


0 7 T 91 31 77 4 42 55 91 96 94 6 F
0 4 T 55 31 4 42 55 77 91 96 94 4 F

4th smallest element is at index 3 which is a[3] = 55

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.

p r p <= r pivot Array after Partition pi k == pi


0 7 T 91 31 77 4 42 55 91 96 94 6 F
0 4 T 55 31 4 42 55 77 91 96 94 4 F
0 2 T 42 31 4 42 55 77 91 96 94 2 T

3RD smallest element is at index 2 which is a[]

What is the time complexity for Quick-Select algorithm?


O{n2]

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]

S = {9, 11, 2, 6, 5, 4, 8, 12, 3}


4th smallest element is in index 3=k

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

Final exam example

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

You might also like