Searching algorithms
Which of the following Big O notations is equivalent to O(12·N +6·N^3 + 1000)? O(1000) O(N) O(N^3)
O(N^3)
Rules for determining Big O notation of composite functions. c + O(f(x))
O(f(x))
Rules for determining Big O notation of composite functions. c · O(f(x))
O(f(x))
Rules for determining Big O notation of composite functions. g(x) + O(f(x))
O(g(x) + O(f(x)))
Rules for determining Big O notation of composite functions. g(x) · O(f(x))
O(g(x)·O(f(x)))
Which function best represents the number of operations in the worst-case? nVal = N steps = 0 while (nVal > 0) { nVal = nVal / 2 steps = steps + 1 } O(log N) O(N^2) O(N)
O(log N) nVal is decreased by half each iteration, so the loop will execute log2N times. f(N)=1+log2N⋅3+1 O(1+log2N⋅3+1)= O(log2N)= O(logN)
Linear search
a search algorithm that starts from the beginning of a list, and checks each element until the search key is found or the end of the list is reached.
A linear search has a _____ runtime complexity.
O(N)
Determine the simplified Big O notation. log2N
O(logN) O(log2N) = O(logN/log2) = O(logN⋅1/log2) = O(logN)
O(N log N) has a _____ runtime complexity.
log-linear
For Binary Search an N element list, the maximum number of steps required to reduce the search space to an empty sublist is
(log2 N) + 1 (steps not checks) checks would be log2 N
Big O growth rate order
1, logN, N, NlogN, N^2, N^3, 2^N, N!
A contact list is searched for Bob. Assume the following contact list: Amy, Bob, Chris, Holly, Ray, Sarah, Zoe What is the second contact searched?
Bob
A contact list is searched for Bob. Assume the following contact list: Amy, Bob, Chris, Holly, Ray, Sarah, Zoe What is the first contact searched?
Holly
O(5) has a _____ runtime complexity
constant
O(N + N^2) has a _____ runtime complexity.
quadratic
Determine the simplified Big O notation. 10 · O(N^2)
O(N^2)
Which of the following Big O notations is equivalent to O(734·N)? O(N) O(734) O(734*N^2)
O(N)
Determine the big-O worst-case runtime for each algorithm. 1) for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { if (numbers[i] < numbers[j]) { ++eqPerms } else { ++neqPerms } } } O(N) O(N^2)
O(N^2) O(N)⋅(O(N)⋅O(1))=O(N^2)
Determine the big-O worst-case runtime for each algorithm. for (i = 0; i < N; i++) { for (j = 0; j < (N - 1); j++) { if (numbers[j + 1] < numbers[j]) { temp = numbers[j] numbers[j] = numbers[j + 1] numbers[j + 1] = temp } } } O(N) O(N^2)
O(N^2) O(N)⋅O(N)⋅O(1)=O(N^2)
A for loop of the form for (i = 0; i < N; ++i) {} that does not have nested loops or function calls, and does not modify i in the loop will always has a complexity of O(N). True False
True
The complexity of the algorithm below is O(1). if (timeHour < 6) { tollAmount = 1.55 } else if (timeHour < 10) { tollAmount = 4.65 } else if (timeHour < 18) { tollAmount = 2.35 } else { tollAmount = 1.55 } True False
True
for (i = 0; i < 24; ++i) { if (timeHour < 6) { tollSchedule[i] = 1.55 } else if (timeHour < 10) { tollSchedule[i] = 4.65 } else if (timeHour < 18) { tollSchedule[i] = 2.35 } else { tollSchedule[i] = 1.55 } } True False
True The statements within the for loop have a O(1) runtime. The for loop executes a constant number of times, so the for loop's runtime is 24⋅O(1)=O(1). A loop with a constant number of iterations executes O(1) times.
Answer the following questions assuming each comparison takes 1 µs. Given a list of 1024 elements, what is the runtime for binary search if the search key is less than all elements in the list?
10, log2 N compare for binary
Answer the following questions assuming each comparison takes 1 µs. Given a list of 1024 elements, what is the runtime for linear search if the search key is less than all elements in the list?
1024, N compare for linear search
Given list: ( 20, 4, 114, 23, 34, 25, 45, 66, 77, 89, 11 ). How many list elements will be checked if the search key is not found using linear search?
11
Given list: ( 4, 11, 17, 18, 25, 45, 63, 77, 89, 114 ). How many list elements will be checked to find the value 77 using binary search?
2
Given a list of 10,000 elements, and if each comparison takes 2 µs, what is the fastest possible runtime for linear search?
2 µs
Given a list of 10,000 elements, and if each comparison takes 2 µs, what is the longest possible runtime for linear search?
20,000 µs
Given list: ( 20, 4, 114, 23, 34, 25, 45, 66, 77, 89, 11 ). How many list elements will be checked to find the value 114 using linear search?
3
Given list: ( 4, 11, 17, 18, 25, 45, 63, 77, 89, 114 ). How many list elements will be checked to find the value 17 using binary search?
3
Given list: ( 4, 11, 17, 18, 25, 45, 63, 77, 89, 114 ). Given an array with 32 elements, how many list elements will be checked if the key is less than all elements in the list, using binary search?
5
Given list: ( 20, 4, 114, 23, 34, 25, 45, 66, 77, 89, 11 ). How many list elements will be compared to find 77 using linear search?
9
For a list with N elements, linear search thus requires at most
N comparisons
The total number of operations for selection sort is proportional to
N*N
Which of the following Big O notations is equivalent to O(N+9999)?
O(N)
Which function best represents the number of operations in the worst-case? negCount = 0 for(i = 0; i < N; ++i) { if (numbers[i] < 0 ) { ++negCount } } f(N) = 2 + 4N + 1 O(4N + 3) O(N)
O(N) The total number of operations include: 1 operation before the loop, 1 operation at the start of the loop, 4 operations that execute each of the N loop iterations, and 1 operation for the final loop condition check. f(N)=4⋅N+3 O(4⋅N+3)=O(N)
Which function best represents the number of operations in the worst-case? for (i = 0; i < N; ++i) { if ((i % 2) == 0) { outVal[i] = inVals[i] * i } } O(1) O(N2) O(N)
O(N) outVal[i] = inVals[i] * i; executes every other loop iteration, so the total number of operations include: 1 operation at the start of the loop, 3 operations every loop iteration, 1 operation every other loop iteration, and 1 operation for the final loop condition check. f(N)=1+3⋅N+(N/2)+1 O(1+3⋅N+(N/2)+1)=O(N)
A selection sort has a _____ runtime complexity.
O(N^2)
Determine the big-O worst-case runtime for each algorithm. for (i = 0; i < N; ++i) { for (j = i; j < N - 1; ++j) { cVals[i][j] = inVals[i] * j } } O(N^2) O(N^3)
O(N^2)
Determine the simplified Big O notation. 10 + O(N^2)
O(N^2)
Determine the big-O worst-case runtime for each algorithm. for (i = 0; i < N; i = i + 2) { for (j = 0; j < N; j = j + 2) { cVals[i][j] = inVals[i] * j } } O(N) O(N^2)
O(N^2) The outer and inner for loops iterate N2N2 times, which is O(N). So, the runtime complexity is O(N)⋅O(N)=O(N^2)
algorithm's runtime
the time the algorithm takes to execute
Determine the big-O worst-case runtime for each algorithm. for (i = 0; i < N; ++i) { sum = 0 for (j = 0; j < N; ++j) { for (k = 0; k < N; ++k) { sum = sum + aVals[i][k] * bVals[k][j] } cVals[i][j] = sum } } O(N) O(N^2) O(N^3)
O(N^3)
Determine the simplified Big O notation. 2·N^3 + O(N^2)
O(N^3)
Determine the simplified Big O notation. 3·N · O(N^2)
O(N^3)
Binary search
a faster algorithm for searching a list if the list's elements are sorted and directly accessible (such as an array). Binary search first checks the middle element of the list. If the search key is found, the algorithm returns the matching location. If the search key is not found, the algorithm repeats the search on the remaining left sublist (if the search key was less than the middle element) or the remaining right sublist (if the search key was greater than the middle element).
Big O notation
a mathematical way of describing how a function (running time of an algorithm) generally behaves in relation to the input size. In Big O notation, all functions that have the same growth rate (as determined by the highest order term of the function) are characterized using the same Big O notation. In essence, all functions that have the same growth rate are considered equivalent in Big O notation.
Which function best represents the number of operations in the worst-case? i = 0 sum = 0 while (i < N) { sum = sum + numbers[i] ++i } f(N) = 3N + 2 f(N) = 3N + 3 f(N) = 2 + N (N + 1)
f(N) = 3N + 3 2 operations execute before the loop. The loop condition and 2 operations in the loop executes N times, yielding 3N operations. The loop condition is checked 1 additional time before the loop terminating. The total operations as a function of N can be expressed as 2 + 3N + 1, or 3N + 3.