Alg DS1 Example Test 1
Alg DS1 Example Test 1
In exercise 1 do not write code. In exercises 2-5 try to write ecient code
(structograms). Deletion includes explicit deallocation. Specify the types
and modes of the formal parameters of the functions and procedures, and
the return types of functions.
1.a Illustrate the operation of insertion sort on the array h4, 6, 20 , 8, 5, 2i.
Illustrate the last insertion in detail.
1.b Illustrate merge sort on the sequence A = h4, 3, 6, 2, 9, 8, 40 i. Illustrate
the last merge in detail. Give the result of each merge operation on the
appropriate subarray in turn. The items must be separated by commas.
1.cIllustrate merge sort on array h3; 41; 52; 26; 38; 57; 9; 49i. Show the last
merge in detail.
1.d Illustrate quicksort on the array A = h4, 3, 6, 2, 9, 8, 4i. We suppose that
function partition() always selects the rst item of the current subarray as
pivot. Give the subarrays computed by each partition(A, p, r) calls. The
pivot must be distinguished by a '+' prex. The items are separated by
commas. For example: 4,2,+5,8
1.e Illustrate the operation of
function partition of quicksort on array h3; 41; 52; 26; 38; 57; 9; 49i.
Let us suppose that the algorithm selects 41 as pivot.
2. Let us consider class Queue with its constructor, destructor, add(x:T ),
rem():T , and length():N operations.
T (n) ∈ Θ(n) for the destructor, and T (n) ∈ Θ(1) for all the other opera-
tions must be satised where n is the length of the queue.
Draw the UML box of the Queue, and the necessary structograms in order
to implement the operations in the following cases.
2.a Let us represent the queue with a private S1L. (If the list is nonempty,
an extra pointer refers to its last element.)
2.bLet us represent the queue using a private one-way list with trailer node.
(One pointer refers to the rst node, and an extra pointer refers to the trailer.)
2.c Let us represent the queue using a private, cyclic one-way list with
a header/trailer node. (The pointer identifying the list refers to its
header/trailer node.)
Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . Neptun code: . . . . . . . . . . . . . . . . . . . . .
2.d Let us represent the queue using a private, cyclic one-way list without
header/trailer node. (If the list is nonempty, the pointer identifying it refers
to its last node. If it is empty, it is identied by a pointer.)
5.a Let us suppose that L, H : E1* identify two strictly increasing H1Ls.
Write procedure union_intersection(L,H :E1*) rearranging the lists so
that list L contains the sorted union of the keys found in the original lists,
and list H contains the sorted intersection of the keys found in the original
lists. Both lists must remain strictly increasing. Allocating and deallocating
objects must be avoided in this program. And keys should not be copied
from one object into another.
Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . Neptun code: . . . . . . . . . . . . . . . . . . . . .
5.b Let us suppose that L, H : E2* identify two strictly increasing C2Ls.
Write procedure union_intersection(L,H :E2*) rearranging the lists so
that list L contains the sorted union of the keys found in the original lists,
and list H contains the sorted intersection of the keys found in the original
lists. Both lists must remain strictly increasing. Allocating and deallocating
objects must be avoided in this program. And keys should not be copied
from one object into another.
M Tunion_intersection (nL , nH ) ∈ O(nL + nH ) is to be satised where nL and
nH are the lengths of the lists.