0% found this document useful (0 votes)
37 views3 pages

Alg DS1 Example Test 1

This document contains instructions for several algorithm exercises involving data structures like queues, linked lists, and binary search trees. Students are asked to: 1) Illustrate sorting algorithms like insertion sort, merge sort, and quicksort on sample arrays and linked lists. 2) Design structograms to implement queue operations like add, remove, and length using different linked list implementations. 3) Write functions to remove maximal elements, insert elements in sorted order, and delete entire linked lists. 4) Repeat some exercises for circular doubly linked lists, modifying elements only through given procedures. 5) Write a union-intersection procedure to combine sorted linked lists without copying keys or allocating new nodes.

Uploaded by

deaarasheed77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

Alg DS1 Example Test 1

This document contains instructions for several algorithm exercises involving data structures like queues, linked lists, and binary search trees. Students are asked to: 1) Illustrate sorting algorithms like insertion sort, merge sort, and quicksort on sample arrays and linked lists. 2) Design structograms to implement queue operations like add, remove, and length using different linked list implementations. 3) Write functions to remove maximal elements, insert elements in sorted order, and delete entire linked lists. 4) Repeat some exercises for circular doubly linked lists, modifying elements only through given procedures. 5) Write a union-intersection procedure to combine sorted linked lists without copying keys or allocating new nodes.

Uploaded by

deaarasheed77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . Neptun code: . . . . . . . . . . . . . . . . . . . . .

1. Algorithms and Data Structures I. example test

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.)

3.a Pointer H refers to the header of an unsorted H1L. Write function


remove_max(H ) which removes an element with maximal key from the list
and returns the address of the element removed. T (n) ∈ Θ(n) where n is the
length of the list.

3.b Pointer H refers to the header of a non-increasingly sorted H1L. Write


function sorted_insert(H, x) which inserts a new element with key x into the
list, so that the list remains non-increasingly sorted. M T (n) ∈ Θ(n) where
n is the length of the list.

3.cPointer L identies an acyclic one-way list. Write function delete_list(L)


which removes and deallocates (deletes) all the elements of the list. T (n) ∈
Θ(n) where n is the length of the list.

4.a Pointer H refers to the header of an unsorted C2L. Write function


remove_max(H ) which removes an element with maximal key from the list
and returns the address of the element removed. T (n) ∈ Θ(n) where n is the
length of the list. List modications must not be done directly, only through
the procedures out(q ), precede(q, r), and follow(p, q ).

4.b Pointer H refers to the header of a non-increasingly sorted C2L. Write


function sorted_insert(H, x) which inserts a new element with key x into the
list, so that the list remains non-increasingly sorted. M T (n) ∈ Θ(n) where
n is the length of the list.

4.c Pointer H refers to the header of a non-increasingly sorted C2L. Write


function delete_list(H ) which removes and deallocates (deletes) all the el-
ements of the list. T (n) ∈ Θ(n) where n is the length of the list. List
modications must not be done directly, only through the procedures out(q ),
precede(q, r), and follow(p, q ).

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: . . . . . . . . . . . . . . . . . . . . .

M Tunion_intersection (nL , nH ) ∈ O(nL + nH ) is to be satised where nL and


nH are the lengths of the lists.
In order to solve this problem, we can leave the common elements of the
lists in list H , and move the other elements of list H into list L. (No element
will be moved from L to H .)

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.

You might also like