University of Moratuwa, Sri Lanka

Faculty of Engineering
Department of Computer Science & Engineering In Class Quiz B.Sc. Engineering, Level 2, Semester II (CS2020) Question 1 Let in the usual notation. 1. 2. 3. 4. be a finite array of integers. Consider the following sorting algorithm 18th March 2009

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 – d) Perform the above algorithm on only. Question 2 a) Two sorting algorithms, and , have asymptotic running times of and ? in the inner for loop? and

. Give steps for

. 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 loop of lines 1 – 7? for any iteration of the for

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

d) We can use the following node of a heap. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

algorithm to make the sub tree rooted at

i) What assumption should hold true in order for the ii) What is the purpose of checking lines 3 and 6? iii) Apply for and tree view.

to be successful? and in

. Show steps based on the

© Saliya Ekanayake WSO2 Inc.