COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

Similar documents
CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

4. Sorting and Order-Statistics

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

COMP Data Structures

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

O(n): printing a list of n items to the screen, looking at each item once.

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CSE373 Fall 2013, Midterm Examination October 18, 2013

ECE250: Algorithms and Data Structures Midterm Review

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Scientific Computing. Algorithm Analysis

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

Overview of Sorting Algorithms

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Sorting Pearson Education, Inc. All rights reserved.

Basic Data Structures (Version 7) Name:

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Recall from Last Time: Big-Oh Notation

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Deterministic and Randomized Quicksort. Andreas Klappenecker

Total Points: 60. Duration: 1hr

Data Structures Question Bank Multiple Choice

Midterm solutions. n f 3 (n) = 3

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Computer Science Foundation Exam. May 6, Computer Science. Section 1A. No Calculators! KEY. Score: 50

INSTITUTE OF AERONAUTICAL ENGINEERING

Faster Sorting Methods

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Recitation 9. Prelim Review

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

Mergesort again. 1. Split the list into two equal parts

CS 216 Exam 1 Fall SOLUTION

Divide and Conquer 4-0

EECS 2011M: Fundamentals of Data Structures

CSCE 2014 Final Exam Spring Version A

Lecture 5: Sorting Part A

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Introduction. Sorting. Table of Contents

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CSC 273 Data Structures

University of Waterloo CS240, Winter 2010 Assignment 2

University of the Western Cape Department of Computer Science

CSci 231 Final Review

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

COMP 250 Fall Homework #4

Measuring algorithm efficiency

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Cilk, Matrix Multiplication, and Sorting

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Draw a diagram of an empty circular queue and describe it to the reader.

Q1 Q2 Q3 Q4 Q5 Q6 Total

Computer Science Foundation Exam

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

The divide and conquer strategy has three basic parts. For a given problem of size n,

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

UNIVERSITY REGULATIONS

CSE 373 Autumn 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

CSCI 104 Log Structured Merge Trees. Mark Redekopp

Divide and Conquer Sorting Algorithms and Noncomparison-based

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Cpt S 122 Data Structures. Sorting

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Matriculation number:

Programming in Haskell Aug-Nov 2015

MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

Computer Science Spring 2005 Final Examination, May 12, 2005

Stacks, Queues (cont d)

Question 7.11 Show how heapsort processes the input:

CS 310 Advanced Data Structures and Algorithms

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

COMP 250 Fall Solution - Homework #4

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

CMSC351 - Fall 2014, Homework #2

Problem Set 6 Due: 11:59 Sunday, April 29

Transcription:

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination L1: Dr Qiang Yang L2: Dr Lei Chen Date: 6 Nov 2006 Time: 6-8p.m. Venue: LTA November 7, 2006 Question Marks 1 /12 2 /8 3 /25 4 /7 5 /15 6 /15 7 /18 Total /100 1

1. (12 marks) Recursion Consider the following recursive program. //n is any positive integer //m is a positive integer between 2 and 9 void fun(int n, int m) { if (n<m) cout << n; else { fun(n/m, m); cout << n%m; (a) (3 marks) What is the output for fun(15, 2)? Answer: 1111 (b) (3 marks) Given the following recursive code. Point out the possible errors in the code. //num is a positive integer int fac(int num) { if (num 1) return 1; else { return num*fac(num+1); Answer: The recursive function will never converge to the base case (ie. num 1) as the value passed to the function is always increasing (ie. fac(num+1)). 2

(c) (6 marks) Given the following function which computes fibonacci numbers using recursive function, write a non-recursive version of function f ib. //num is a positive integer int fib(int num) { if (num==0) return 0; if (num==1) return 1; return (fib(num 1)+fib(num 2)); Answer: int fib(int n) { int f[n+1]; f[0] = 0; f[1] = 1; for (int i=2; i<= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; 3

2. (8 marks) Merge Sort and Insertion Sort (a) (4 marks) Draw a binary tree to show step by step how Merge Sort sorts {142, 543, 123, 65, 453, 879, 572, 434. {142, 543, 123, 65, 453, 879, 572, 434 {142, 543, 123, 65 {453,879, 572, 434 {142, 543 {123, 65 {453, 879 {572, 434 {142 {543 {123 {65 {453 {879 {572 {434 {142, 543 {65, 123 {453, 879 {434,572 {65, 123, 142, 543 {434, 453, 572, 879 {65, 123, 142, 434, 453, 543, 572, 879 (b) (4 marks) Show under what order of input, the insertion sort will have worst-case and best-case situations for sorting the set {142, 543, 123, 65, 453, 879, 572, 434. Worst Case:the element always gets inserted at the front, so all the sorted elements must be moved at each insertion. The ith insertion requires (i-1) comparisons and moves. sorting in ascending order: {879, 572, 543, 453, 434, 142, 123, 65 sorting in descending order: {65, 123, 142, 434, 453, 543, 572, 879 Best Case: the element always gets inserted at the end, so we don t have to move anything, and only compare against the last sorted element. We have (n-1) insertions, each with exactly one comparison and no data moves per insertion. sorting in ascending order: {65, 123, 142, 434, 453, 543, 572, 879 sorting in descending order: {879, 572, 543, 453, 434, 142, 123, 65 4

3. Big-Oh (a) What is the time complexity of the equation T(n) given below?please give the tightest possible bound in big-oh notation. Assume n is a power of 2 (that is n = 2 k for some k). { T (n) = 2T (n/2) + n log n T (1) = 1 Results without derivations do not receive any points. Anawer: T (n = 2 k ) = 2T (n/2) + 2 k k = 2(2T (n/4) + 2 k 1 (k 1)) + 2 k k = 2 2 T (n/4) + 2 k (k 1) + 2 k k = 2 3 T (n/8) + 2 k (k 2) + 2 k (k 1) + 2 k k =... = 2 k T (1) + 2 k (1 + 2 + 3 +... + k) = 2 k ((k + 1) k/2 + 1) = n(((log n + 1) log n)/2 + 1) = O(n log n log n) 5

(b) Give a tightest possible bound for the Big-Oh runtime complexity of the following function in terms of the value of the input parameter n. Results without derivations do not receive any points. (Hint: The complexity of easy(n) is T (n).) 1 int easy(int n){ 2 int a, b, c; 3 if (n <= 1) 4 return 0; 5 else { 6 a = easy(n-1); 7 b = easy(n % 2); 8 c = easy(n-2)/2; 9 return a + b + c; 10 11 Answer: T(N)=T(N-1)+ T(N-2)+ T(1) T(N-1)=T(N-2)+ T(N-3)+ T(1)...... T(2)=T(1)+T(0)+T(1) F(0)*T(N)=F(0)*T(N-1)+ F(0)*T(N-2)+ F(0)*T(1) F(1)*T(N-1)=F(1)*T(N-2)+ F(1)*T(N-3)+ F(1)*T(1) F(2)*T(N-2)=F(2)*T(N-3)+ F(2)*T(N-4)+ F(2)*T(1)......... F(N-2)*T(2)=F(N-2)*T(1)+F(N-2)*T(0)+ F(N-2)*T(1) Sum up T(N)=F(N-3)*T(1)+ F(N-2)*T(1)+F(N-2)*T(0)+ N 2 i=0 F(i)*T(1) =F(N)*T(1)+(F(N)-F(1))*T(1) =2F(N)*T(1)-T(1) (5/3) N 1 = O(2 N ) 6

(c) Multiple Choice: Circle the best answer for each question. If you circle two answers, you will receive zero for that question. i. The recurrence solves to: A. O(n) B. O( n) C. O(log n) D. O(log log n) E. O(log log log n) F. O(1) { T (n) = T ( 3 n) + 1 n > 2 T (n) = 1 n 2 Answer: D.Suppose the recurrence for T(n) stops at some n = a < 2, and takes k steps, then a 3k = n,then k = log 3 (log a n),so the answer is D,O(log log n). ii. If f(n) = n+n log n,which of the following is/are true? 1) O(n) 2) O(n 2 ) 3) Ω(n 2 ) 4) Ω(n log n) 5) Θ(n log n) 6) Θ(n) A. 1) only. B. 1) & 6). C. 2) only. D. 2) & 3). E. 2) & 4) & 5). F. 4) & 5). G. 2) & 3) & 4) & 5). Answer: E. n log n < f(n) < 2n log n,so 5) is correct.then 4 must be correct. Also notice that, 2) is correct,because n log n < n 2. 7

4. In the following code for a stack machine, PUSH X means push X onto the stack, POP X means pop the top of the stack into X, and an operator without an operand (ADD, MULT) means pop the top two items off the stack, perform the indicated operation on them, and push the result back onto the stack.(note: ADD=Addition, MULT=Mulitplication.) 1) PUSH A 2) PUSH B 3) ADD 4) POP T 1 5) PUSH B 6) PUSH C 7) PUSH T 1 8) PUSH T 1 9) MULT 10) MULT 11) ADD 12) POP Z Please write down in the table below the content of the stack after each stack operation (the first two operations have already been given). In addition, please specify values of T 1 and Z using symbols A, B and C. No. The Content of the Stack 0) empty 1) A 2) A, B 3) A + B 4) empty 5) B 6) B, C 7) B, C, T 1 (or A + B) 8) B, C, T 1, T 1 (or B, C, A + B, A + B) (or B, C, T 1, A + B) (or B, C, A + B, T 1 ) 9) B, C, T1 2 (or B, C, (A + B) 2 ) 10) B, C T1 2 (or B, C (A + B) 2 ) 11) B + C T1 2 (or B + C (A + B) 2 ) 12) empty T 1 = A + B, Z = B + C (A + B) 2. 8

5. Heap (a) (10 marks) Table 1 shows an array of numbers. In Table 2, build a min-heap by inserting these numbers one by one from left to right, where you can use X to mean unoccupied. Fill in the results after each of the remaining insertions and heap-restoring operations. Note that the heap is stored in the array starting from array index 1 (so that the index 0 of the array is left empty). Index 1 2 3 4 5 6 Input array 17 10 4 16 33 3 Table 1: Input Table 2: Fill in the slots Index 1 2 3 4 5 6 Input array 17 10 4 16 33 3 Insert 17 17 X X X X X Insert 10 10 17 X X X X Insert 4 4 17 10 X X X Insert 16 4 16 10 17 X X Insert 33 4 16 10 17 33 X Insert 3 3 16 4 17 33 10 9

(b) (5 marks) Heap Sort: For the array in Table 3, perform a heap sort using the same array and no additional array. Fill in the result after each of the deletion and heap-restoration operations. Also show the array after the deleted element is inserted back in order to produce a sorted array in the end. Table 3: Fill in the slots for sorting in increasing order Index 1 2 3 4 5 6 Max Heap 33 17 16 10 4 3 Deletion number 33 17 16 10 4 3 first 17 10 16 3 4 33 second 16 10 4 3 17 33 third 10 3 4 16 17 33 fourth 4 3 10 16 17 33 fifth 3 4 10 16 17 33 sixth 3 4 10 16 17 33 10

6. Multiple Choice Questions (Stack and Linked List) (a) What is the minimum height of the decision trees whose nodes are the comparison operations and whose leave nodes are the linear ordering of N numbers? (A) O(N^2) (B) O(N) (C) O(2^N) (D) Omega(Nlog(log(N))) (E) Omega(log(N!)) (F) Omega(N^2) (G) none of the above. Answer: (G). It should be order log(n). (b) If the variables are suitably initialized and if i remains within appropriated bounds, then the following code implements stack operations Push and Pop when the stack is represented as a vector V [1,..N] and a pointer i. Push: begin V[i] = x ; i := i + 1 ; end Pop: begin i := i - 1; x := V[i] ; end (i) Which of the following gives the correct initialization for this stack implementation? (A) i := 0 (B) i := 1 (C) i := N - 1 (D) i := N (E) None of the above Answer: (B) (ii) If it is assumed that suitable changes in the initialization code were also made, which of the following changes to Push and Pop would yield a correct implementation of stacks? I. Replace the code for Push with that for Pop and vice versa. II. Make Push decrement i and Pop increment i. III. Reverse the order of the statements in both Push and Pop. 11

(A) I only (B) II only (C) III only (D) I and II (E) II and III Answer: (E) 7. Quick Sort Applying the Quick Sort with the median of three methods that you have learnt from the lecture to the given array A[]. Please complete the full sorting process with the help of the following table. You must fill in the table with the contents of A[] and the final positions of the pivot(s) by marking it with p in the space provided. Note: The content of A[] must be in ascending order. Below is the pseudocode of quicksort: (swapping is indicated in line 14) void quicksort(a[], p, r){ int pivot = median_of_three(a[],p,r); // begin partitioning int i = p; int j = r-2; for(;;){ while(a[i] < pivot){ i++; while(a[j] > pivot){ j--; if(i < j) swap(a[i], A[j]); else break; swap(a[i], A[r-1]); <------***** swapping **** // recursive sort each partition quicksort(a, p, i-1); // Sort left partition quicksort(a, i+1, r); // Sort right partition Answer: 12

Note: If the student finishes the trace using either the routine in given above, or the lecture notes, then the student is considered right. Solution 1 - median of three Index 0 1 2 3 4 5 6 7 8 A[] 89 19 65 19 55 38 19 42 38 A[] after 1st swap of pivot 38 19 19 19 42 38 55 65 89 P A[] after 2nd swap of pivot 19 19 19 38 42 38 55 65 89 P P A[] after 3rd swap of pivot 19 19 19 38 38 42 55 65 89 P P A[] after 4th swap of pivot A[] after 5th swap of pivot A[] after 6th swap of pivot A[] after 7th swap of pivot 13

Solution 2 - original Index 0 1 2 3 4 5 6 7 8 A[] 89 19 65 19 55 38 19 42 38 A[] after 1st swap of pivot 19 19 19 38 55 38 42 89 65 P A[] after 2nd swap of pivot 19 19 19 38 55 38 42 65 89 P P A[] after 3rd swap of pivot 19 19 19 38 38 42 55 65 89 P A[] after 4th swap of pivot A[] after 5th swap of pivot A[] after 6th swap of pivot A[] after 7th swap of pivot 14