Vous êtes sur la page 1sur 2

# University of Ontario Institute of Technology INFR 2820U: Algorithms & Data Structures Assignment 1 Date Handed out: Sept

20, 2011 Due Date: Oct 4, 2011 via WebCT. This assignment covers the material from the notes of Lecture 2. Submit a single file with your answers via WebCTs submission system. No other mean of submission (e.g., email, printouts, etc.) is accepted. Acceptable formats include Word, pdf, and postscript. Exercise I (80 points) Consider the following function: // // // // // This function finds the average of the values in the array list, then counts the number of values that are greater than or equal to the average, and the number of values that are smaller than the average.

void Mystery (int list[], int size) { float average, sum = 0; int biggerOrEqualThanAverage=0; for (int index=0; index < size; index++) sum = sum + list[index]; average = sum / size; for(int index = 0; index < size; index=index++) if (list[index]>=average) biggerOrEqualThanAverage ++; cout << The average of the list is :<< average << endl; cout << There are <<biggerOrEqualThanAverage << values bigger than or equal to the average \n; cout << There are <<size - biggerOrEqualThanAverage << values smaller than the average \n; } a) Using the print operation ( cout)as a basic operation: 1. Find the maximum and minimum number of basic operations executed by the function Mystery() if the value of the parameter size is 10. 2. Find the number of operations executed by the function if the value of the size is n. 3. What is the order (Big-Oh) of the function Mystery()? Prove your answer. b) Using the greater than operation or equal (>=) as a basic operation:

1. Find the maximum and minimum number of basic operations executed by the function if the value of the parameter size is 20. Briefly justify your answer. 2. Find the number of operations executed by the function Mystery() if the value of the parameter size is n. Briefly justify your answer. 3. What is the order (Big-Oh) of the function Mystery()? c) Using the increment operation (++) as a basic operation: 1. Find the maximum and minimum number of basic operations executed by the function if the value of the parameter size is 1000. Briefly justify your answer. 2. Find the number of operations executed by the function Mystery() if the value of the parameter size is n. Briefly justify your answer. 3. What is the order (Big-Oh) of the function Mystery()? Prove your answer. d) Write your own program to compute the actual running time of the function Mystery(). Run your program for various sizes of the list, collect and plot the running time of your program. Comment on the graph using the results obtained in question I.c . Exercise II (20 points) If the algorithm doIt() has an efficiency of 5n basic operations, calculate the run-time efficiency and the big Oh complexity of the following program segment: i = 1 loop i<= n doIt() i = i+ 1 end loop

Exercise III-Bonus Question (20 points) Prove that a function that performs f(n)= 2n3+2n2lgn+15 basic operations is O(n3).