Deck 15: Running Time Analysis
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/56
Play
Full screen (f)
Deck 15: Running Time Analysis
1
Which of the following is the Big-Oh of this function: T(n) = n + (2n + 8)?
A) O(1)
B) O(n)
C) O(n2)
D) O(n log n)
A) O(1)
B) O(n)
C) O(n2)
D) O(n log n)
B
2
What is a handwaving method?
A) An estimation method for evaluating the dominant term
B) A method involving iterating several times until we can identify a pattern
C) A method of guessing the value of T1(n) as a function of n and then checking to see if the guess is correct
D) A Master Theorem based on strict mathematics
A) An estimation method for evaluating the dominant term
B) A method involving iterating several times until we can identify a pattern
C) A method of guessing the value of T1(n) as a function of n and then checking to see if the guess is correct
D) A Master Theorem based on strict mathematics
A
3
It has become common in the industry to say Big-Oh instead of Big-Theta.
True
4
For n = 1,000, Bubble Sort executes more statements than Merge Sort, which executes more statements than Sequential Search, which executes more statements than __________.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
5
Between Sequential Search or Binary Search, which one will execute more statements when searching an array of a million users for a particular user name?
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
6
If n = 10 and the order of magnitude is n2, how many statements will be executed?
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
7
Most programmers tend to disregard ____________ when writing code.
A) software requirements
B) hardware specifications
C) memory utilization issues
D) exploding cost of memory
A) software requirements
B) hardware specifications
C) memory utilization issues
D) exploding cost of memory
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following has the lowest order of magnitude?
A) n log n
B) n
C) n2
D) log n
A) n log n
B) n
C) n2
D) log n
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
9
When programming, you want as tight an upper bound as possible.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
10
Algorithms with n as the exponent of the function should always be the first choice, as the running time will be better.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
11
It is necessary to know exactly how many statements are executed in order to determine the Big-Oh running time of the function.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following will result in the shortest running time?
A) 3n = 3 * 3n - 1
B) 3n = 2(3n - 1) + 3n - 1
C) 3n = 3n - 1 + 3n - 1 + 3n - 1
D) Each will produce the same result, so the running time is the same.
A) 3n = 3 * 3n - 1
B) 3n = 2(3n - 1) + 3n - 1
C) 3n = 3n - 1 + 3n - 1 + 3n - 1
D) Each will produce the same result, so the running time is the same.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
13
When trying to develop and identify a pattern using iteration, which of the following is the best strategy?
A) Precisely compute all terms.
B) Leave terms as patterns.
C) Use exponents as much as possible.
D) Use handwaving to get a precise result.
A) Precisely compute all terms.
B) Leave terms as patterns.
C) Use exponents as much as possible.
D) Use handwaving to get a precise result.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
14
Inefficient coding will increase the running time, possibly significantly.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
15
Using the following code segment could help identify whether the program is running efficiently by counting the number of statement executions.
int counter = 0;
int temp, indexOfMax = 0;
for ( int i = 0; i < arr.length - 1; i++ )
{
indexOfMax = 0;
animate( i, 0, counter );
…
int counter = 0;
int temp, indexOfMax = 0;
for ( int i = 0; i < arr.length - 1; i++ )
{
indexOfMax = 0;
animate( i, 0, counter );
…
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
16
What is the Big-Oh of the function T(n) = n2 - 2n + 81?
A) O(1)
B) O(n)
C) O(n2)
D) O(n log n)
A) O(1)
B) O(n)
C) O(n2)
D) O(n log n)
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
17
Merge Sort is more efficient than Bubble Sort because Merge Sort is O(n2) and Bubble Sort is O(n log n).
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
18
If the order of magnitude is n2 and n = 5, ____ statements will be executed.
A) 10
B) 25
C) 32
D) None of these is correct.
A) 10
B) 25
C) 32
D) None of these is correct.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
19
If the order of magnitude is log n and n = 1 million, __________ statements will be executed.
A) 10
B) 2.23
C) approximately 1 million
D) approximately 20
A) 10
B) 2.23
C) approximately 1 million
D) approximately 20
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
20
A function f (n) is Big-Theta of another function g(n) if and only if:
A) f(n) is Big-Omega of g(n) and f(n) is Big-Oh of g(n).
B) f(n) is Big-Omega of g(n) and g(n) is Big-Oh of f(n).
C) for any n <= n1, f (n) <= c1 * g(n), and for any n <= n2, f (n) <= c2 * g(n).
D) for any n >= n1, f (n) <= c1 * g(n), and for any n <= n2, f (n) >= c2 * g(n).
A) f(n) is Big-Omega of g(n) and f(n) is Big-Oh of g(n).
B) f(n) is Big-Omega of g(n) and g(n) is Big-Oh of f(n).
C) for any n <= n1, f (n) <= c1 * g(n), and for any n <= n2, f (n) <= c2 * g(n).
D) for any n >= n1, f (n) <= c1 * g(n), and for any n <= n2, f (n) >= c2 * g(n).
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
21
The pattern T(n) = 2k T(n / 2k) + kn is:
A) O(n)
B) O(log n)
C) O(n log n)
D) O(n2)
A) O(n)
B) O(log n)
C) O(n log n)
D) O(n2)
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
22
In trying to compute running time, we are interested in:
A) relative time.
B) absolute time.
C) a precise mathematical expression.
D) All of these are correct.
A) relative time.
B) absolute time.
C) a precise mathematical expression.
D) All of these are correct.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
23
Running times represented using the Big-Oh notation are interested in:
A) an upper-bound running time.
B) as tight an upper bound as possible.
C) a condition such that for an n sufficiently big, the expression inside Big-Oh is a lower bound of the running time.
D) All of these are correct.
A) an upper-bound running time.
B) as tight an upper bound as possible.
C) a condition such that for an n sufficiently big, the expression inside Big-Oh is a lower bound of the running time.
D) All of these are correct.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
24
Which of the following is the Big-Oh of this function: T(n) = T(n / 2k) + 5 * k?
A) O(log n)
B) O(n)
C) O(n2)
D) O(n log n)
A) O(log n)
B) O(n)
C) O(n2)
D) O(n log n)
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
25
To analyze the running time of a code sequence or a method, you can count the number of times each statement is executed and calculate a total count of statement executions.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
26
Merge Sort and Quick Sort are two sorting algorithms implemented recursively.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
27
The strategy we use to compute the running time of a method where we start with a recurrence relation and we iterate several times to develop a pattern is known as __________.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
28
A function has the running time T( n ) = 4 n3 − 6 n2 + 7 n + 9. What is its running time in Big-Oh notation?
A) O( 1 )
B) O( n )
C) O( n2 )
D) O( n3 )
A) O( 1 )
B) O( n )
C) O( n2 )
D) O( n3 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
29
What is the typical running time of a single loop with a counting variable going from 0 to n − 1?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
30
What is the typical running time of a double loop with a counting variable going from 0 to n − 1 for both the outer loop and the inner loop?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
31
What is the typical running time of a single comparison using an if statement?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
32
What is the average running time of a sequential search when searching a single-dimensional array of size n?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
33
What is the best-case running time of sequential search when searching a single-dimensional array of size n?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
34
What is the running time of a method with the following recurrence relation: T( n ) = T( n − 1 ) + T( n − 1 ), with T( 0 ) = 1?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
35
What is the running time of a method with the following recurrence relation: T( n ) = T( n − 1 ) + 1, with T( 0 ) = 1?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
36
What is the running time of a method with the following recurrence relation: T( n ) = T( n − 1 ) + 2, with T( 0 ) = 1?
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( 2n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
37
What is the average-case running time of binary search when searching a single-dimensional sorted array of size n?
A) O( n )
B) O( log n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( log n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
38
What is the best-case running time of binary search when searching a single-dimensional sorted array of size n?
A) O( n )
B) O( log n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( log n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
39
What is the average-case running time of insertion sort, bubble sort, and selection sort when sorting a single-dimensional array of size n?
A) O( n )
B) O( log n )
C) O( 1 )
D) O( n2 )
A) O( n )
B) O( log n )
C) O( 1 )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
40
What is the average-case running time of merge sort when sorting a single-dimensional array of size n?
A) O( n2 log n )
B) O( 2n )
C) O( n log n )
D) O( n2 )
A) O( n2 log n )
B) O( 2n )
C) O( n log n )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
41
What is the best-case running time of merge sort when searching a single-dimensional array of size n?
A) O( n2 log n )
B) O( n )
C) O( n log n )
D) O( n2 )
A) O( n2 log n )
B) O( n )
C) O( n log n )
D) O( n2 )
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
42
In terms of Big-Oh notation, a running time of 2n is equivalent to a running time of n.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
43
The running time of a recursive method is always better than the running time of a non-recursive method.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
44
The running time of a method is always the same no matter how that method is coded.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
45
When we measure the speed performance of an algorithm, we use the expression __________.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
46
Compare the running times of these two statements. Justify your answer.
T(n) = 2 + (3 * n + 2) + (n2 - 2)
T(n) = 2 + (3 + n + 2) + (n - 2)
T(n) = 2 + (3 * n + 2) + (n2 - 2)
T(n) = 2 + (3 + n + 2) + (n - 2)
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
47
Evaluate whether the following code is efficient. If not, recommend a better code.
return powerOf2B( n - 1 ) + powerOf2B( n - 1 );
return powerOf2B( n - 1 ) + powerOf2B( n - 1 );
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
48
Analyze whether you should be more concerned about the best-case scenario or the worst-case scenario when determining which algorithm to use.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
49
How does the while loop execution in Insertion Sort show that it is going to have a longer running time than Merge Sort?
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
50
Explain why it is increasingly important for programs to be efficient.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
51
Identify what must be changed to make the following a true statement: Algorithms that have a running time where n is the logarithm of the function, such as log n, take a very large number of statement executions and are very slow; they should be used only if no better algorithm can be found.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
52
Evaluate why the somewhat-complex rules for Big-Oh can be simplified into keeping only the dominant term and ignoring its coefficient.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
53
Identify the dominant term and Big-Oh for the following:
4 * n * log n + 8 * n + log n + 16
4 * n * log n + 8 * n + log n + 16
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
54
Identify the dominant term and the Big-Oh for the following: 5 * n + 287
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
55
Andre runs an Insertion Sort and a Bubble Sort, using the same data, on the same computer. Estimate if one will have the shorter running time. Why do you think it is necessary to specify that the same computer is used to implement both algorithms?
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
56
In the following code, identify the first and second base case, and evaluate whether this code would have an efficient running time.
1 public static int recursiveBinarySearch
2 ( int [ ] arr, int key, int start, int end )
3 {
4 if ( start <= end )
5 {
6 int middle = ( start + end ) / 2;
7 if ( arr[middle] == key )
8 return middle;
9 else if ( arr[middle] > key )
10 return recursiveBinarySearch( arr, key, start, middle - 1 );
12 else
13 return recursiveBinarySearch( arr, key, middle + 1, end );
14 }
15 else
16 return -1;
17 }
1 public static int recursiveBinarySearch
2 ( int [ ] arr, int key, int start, int end )
3 {
4 if ( start <= end )
5 {
6 int middle = ( start + end ) / 2;
7 if ( arr[middle] == key )
8 return middle;
9 else if ( arr[middle] > key )
10 return recursiveBinarySearch( arr, key, start, middle - 1 );
12 else
13 return recursiveBinarySearch( arr, key, middle + 1, end );
14 }
15 else
16 return -1;
17 }
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck