Professional Documents
Culture Documents
2011
FINAL EXAMINATION
IT111
DATA STRUCTURES AND ALGORITHMS
DURATION:
3 HOURS
DATE:
IT111
Question 1
May 2011
Given below a list integers: 34, 125, 5, 19, 77, 263, 19, -2, 38
(a)
Sort the list of integers using Insertion Sort approach. Assume the numbers
are stored in a one-dimensional array. Show the contents of the array after
each loop of execution. Sort in ascending order.
(6 marks)
(b)
Sort the list of integers using the Quick sort approach. Use the first number
in the list as the pivot key. Show the sorting process as a simple recursive
tree. Sort in ascending order.
(6 marks)
(Total: 12 marks)
Question 2
You are given a general tree T as follows:
A
B
E
(a)
(b)
(c)
IT111
May 2011
Question 3
You are given the following recursive function.
Function NICE (P)
Int A, B, C, P
BEGIN
IF P <= 0 THEN A 0
ELSE
BEGIN
B P MOD 10
C P DIV 10
A B * NICE( C)
END
Return (A)
END
Diagram 1.1
Note: The operator MOD returns the integer remainder of a division
The operator DIV returns the integer part of a division
(a)
Draw a recursive tree to show the tracing of the function above for P =5693.
(10 marks)
(b)
(c)
IT111
May 2011
Question 4
The adjacency set list for a graph is given as below
Vertex
1
2
3
4
5
Set
{2, 3, 4, 5}
{6}
{7}
{8}
{}
Diagram 1.2
(i)
(ii)
Question 5
Write a complete algorithm of a binary search algorithm. You may write using a
psuedocode version or choose any programming language that you know. Trace the
working of your algorithm for the following list of items:
2, 5, 8, 9, 12, 34, 59, 65
The target key is: 59
(Total : 14 marks)
Question 6
A circular queue of characters is stored in memory location 200 to 209 inclusive. At
the start, the queue is empty and both the pointers of the queue are set at location 200.
(a)
(b)
(c)
(2 marks)
(6 marks)
(2 marks)
(Total: 10 marks)
IT111
May 2011
Question 7
A stack is stored in memory location 500 to 510 inclusive. Three pointers are used to
track the size and data positions in the stack. The description of the pointers are given
below:
Base Pointer (BP): Point to the base (first) location of the stack
Top Pointer (TP): Point to the last available location in the stack
Stack Pointer (SP): Point to the next available location in the stack
(i)
(ii)
Show the contents of the stack after the execution of the following
program segment. Assume the stack is empty in the beginning. Show
the contents at each loop for the second FOR loop.
FOR count = 1 to 10
BEGIN
PUSH (count)
END
FOR count = 1 to 5
BEGIN
If count MOD 5 = 0 THEN
PUSH (count)
ELSE x POP()
END
Diagram 1.3
(6 marks)
(Total: 10 marks)
IT111
May 2011
Question 8
Assume you are given a linked list where each node in the list has .info and .next
fields. The .info field consist of a single character (Eg; A, B, G ,.). The .next
field is pointing to the next node. A head pointer is pointing to the front of the linked
list. A procedure REDUCE is written to carry out some work on the linked list. A
recursive version of the procedure is given below.
Line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Statement
PROCEDURE REDUCE (head: pointer)
second: pointer
BEGIN
IF (head <> NULL) THEN
second = head*.next
IF (second <> NULL) THEN
IF (head*.info = second*.info)
head*.next = second*.next
REDUCE (head)
ELSE REDUCE (Head*.next)
END
END
END
END
Diagram 1.4
(i)
If the linked list stores the characters listed below, what will be the
final result after executing the procedure above?
A, A, B, B, B, A, B, C, C, C, D, ,D, B
(4 marks)
(Total: 10 Marks)