Sunteți pe pagina 1din 3

University of Moratuwa, Sri Lanka

Faculty of Engineering
Department of Computer Science & Engineering

In Class Quiz 18th March 2009


B.Sc. Engineering, Level 2, Semester II (CS2020)

Question 1

Let be a finite array of integers. Consider the following sorting algorithm


in the usual notation.

1.
2.
3.
4.

a) Write pseudo code to perform the exchange operation of the above algorithm (line 4).
The method signature would be and a call to this method will
th th
exchange the element of A with element of .
b) Considering the integer nature of the array elements, do you think it is possible to write
the exchange operation without the need of an additional variable? If so, write pseudo
code for the new implementation of the exchange method. Use the same method
declaration as in part a).

c) What is the significance of limiting the maximum of to – in the inner for loop?
d) Perform the above algorithm on . Give steps for and
only.

Question 2

a) Two sorting algorithms, and , have asymptotic running times of and


. Which algorithm do you think would be efficient with the growth of ?
Briefly explain your answer.
b) If either or is more efficient than the other, based on the asymptotic running
times, then will there be any usage of the “inefficient” algorithm in practice? Briefly
explain your answer.
c) Let be a finite array of integers. Consider the following insertion
sort algorithm in the usual notation.

© Saliya Ekanayake
WSO2 Inc.
1.
2.
3.
4.
5.
6.
7.

i) What can you say about the elements in for any iteration of the for
loop of lines 1 – 7?

ii) How can we incorporate insertion sort with merge sort to improve the efficiency
of sorting?

d) Suggest a method to merge two separately sorted integer arrays into one final array. No
need to write pseudo code, an example based explanation is sufficient. Hint: think of the
smallest element of a sorted array.

Question 3

a) Let be a finite array of integers. Let be the starting index and be


the ending index of (i.e. and ). Consider the following partition
algorithm associated with quick sort in the usual notation.

1.
2.
3.
4.
5.
6.
7.
8.

For any iteration of the loop in lines 3 – 6 and for any index, of what can you say
about the value of with respect to , under the following conditions of ?

i)
ii)
iii)

b) What can you say about the position of the pivot element after the completion of the
partition algorithm?

© Saliya Ekanayake
WSO2 Inc.
c) Answer the following questions based on your knowledge on max heaps and heap sort
algorithm.

i) Explain what is meant by a heap data structure?


ii) State the property that should be satisfied by a heap in order to become a max heap.
iii) Draw a tree view for the following heap.

16 14 10 8 7 9 3 2 4 1

th
d) We can use the following algorithm to make the sub tree rooted at
node of a heap.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

i) What assumption should hold true in order for the to be successful?


ii) What is the purpose of checking and in
lines 3 and 6?
iii) Apply for and . Show steps based on the
tree view.

© Saliya Ekanayake
WSO2 Inc.

S-ar putea să vă placă și