Chapter 5 - Searching and Algorithms

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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 explanation: Binary search checks indices 15, 7, 3, 1, and 0. After checking index 0, the algorithm returns -1, indicating the key was not found.

What is Binary Search?

An algorithm to find a value in a sorted list where you check the middle first, then cut the list in half depending on the value of the item. You repeat the process until you find the value.

a recursive algorithm must have a _______ _______

BASE CASE => A case where a recursive algorithm completes without applying itself to a smaller subproblem.

A loop is never a constant time operation. (T/F)

FALSE explanation: A loop with a constant number of iterations can be a constant time operation. Ex: The value of variable y in the loop below does not affect the number of operations. for (i = 0; i < 10; i++) sum += y

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 microseconds explanation: The fastest runtime happens when the search element is located at the front of the list.

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 explanation: The longest runtime happens when the search key is last element in the list or not found in the list.

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

The base case is what ensures that a recursive algorithm eventually terminates. (T/F)

True => A sequence of steps that endlessly reapplied itself would never terminate. A recursive algorithm terminates due to the base case.

Big O Notation

Used in computer science to describe the performance or complexity of an algorithm. Big-O specifically described the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.

recursive function

a function that calls itself; commonly used to implement recursive algorithms.

The hardware running the code is the only thing that affects what is and what is not a constant time operation. (T/F)

FALSE => The programming language also affects what is a constant time operation. Ex: A programming language could provide variable size floating point values as the only available numerical type and implement arithmetic operations in software. Since the values are not fixed size, arithmetic operations would not be constant time.

The presence of a base case is what identifies an algorithm as being recursive. (T/F)

False => The algorithm doesn't apply itself in the base case. A recursive algorithm is identified by the presence of a step wherein the algorithm applies itself to a smaller subproblem.

A recursive algorithm applies itself to a smaller subproblem in all cases. (T/F)

False => in the base case, the recursive algorithm completes without applying itself

What is the big-O notation for the best-case runtime? i = 0 belowThresholdSum = 0.0 belowThresholdCount = 0 while (i < N && numbers[i] <= threshold) { belowThresholdCount += 1 belowThresholdSum += numbers[i] i += 1 } avgBelow = belowThresholdSum / belowThresholdCount O(1) O(N)

O(1) The big-O notation for an algorithm's best-case runtime represents the growth rate for the shortest execution. The shortest execution occurs if numbers[0] > threshold, in which case the loop does not execute.

Which of the following Big O notations is equivalent to O(734·N)? O(734 * N^2) O(734) O(N)

O(N) Constants in product terms are omitted. For O(734·N), the constant 734 is omitted, yielding O(N).

Which of the following Big O notations is equivalent to O(N+9999)? O(1) O(N) O(9999)

O(N) explanation: For O(N+9999), N is the highest order term, yielding O(N)

What is the big-O notation for the worst-case runtime?for (i = 0; i < N; ++i) { if ((i % 2) == 0) { outVal[i] = inVals[i] * i } } O(1) O(N/2) O(N)

O(N) explanation: 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.

A linear search has a _____ runtime complexity. O(log N) O(N) O(N^2)

O(N) explanation: A linear search may need to search all N elements within a list.

What is the big-O notation for the worst-case runtime? 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) explanation: 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)

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) The outer for loop iterates O(N) times. For each of these iterations, the inner for loop iterates O(N) times. The statements in the inner for loop have O(1) runtime. So, the runtime is: O(N)⋅(O(N)⋅O(1))=O(N^2).

A selection sort has a _____ runtime complexity. O(N) O(N log N) O(N^2)

O(N^2) explanation: The total number of operations for selection sort is proportional to N · N.

Which of the following Big O notations is equivalent to O(12·N +6·N + 1000)? O(1000) O(N^3) O(N)

O(N^3) O(12·N + 6·N^3+ 1000) = O(6·N^3) = O(N^3)

Assuming nVal is an integer, what is Assuming nVal is an integer, what is the big-O notation for the worst-case runtime? nVal = N steps = 0 while (nVal > 0) { nVal = nVal / 2 steps = steps + 1 } O(log N) O(N/2) O(N)

O(logN) nVal is decreased by half each iteration, so the loop will execute log2N times.

O(N + N^2) has a _____ runtime complexity. linear-quadratic exponential quadratic

Quadratic explanation: O(N + N^2) reduces to O(^2N). The highest power is 2, so the runtime complexity is quadratic.

The statement below that assigns x with y is a constant time operation. y = 10 x = y (T/F)

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 The if-else statements execute a constant number of operations, which has a big-O complexity of O(1).

The complexity of the algorithm below is O(1). 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

The 3 constant time operations in the code below can collectively be considered 1 constant time operation. x = 26.5 y = 15.5 z = x + y (T/F)

TRUE explanation: Mathematically, a constant times a constant is still a constant. Therefore 3, or any other constant number of constant time operations, can collectively be considered 1 constant time operation.

Certain hardware may execute division more slowly than multiplication, but both may still be constant time operations. (T/F)

TRUE => Division is often slower than multiplication on many processors. But each division operation takes the same amount of time regardless of operand values, so is still a constant time operation.

Operation Example In the code below, suppose str1 is a pointer or reference to a string. The code only executes in constant time if the assignment copies the pointer/reference, and not all the characters in the string. str2 = str1 (T/F)

TRUE =? Copying all characters in the string would require more operations for longer strings. But assignment of a pointer/reference is a constant time operation.

What is the worst-case runtime of an algorithm?

The runtime complexity for an input that results in the longest execution

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 have a complexity of O(N). True False

The statements within the for loop must only have constant time complexity, or O(1). So, the for loop's complexity is N⋅O(1) = O(N).

A recursive algorithm

an algorithm that breaks the problem into smaller subproblems and applies the algorithm itself to solve the smaller subproblems.

Constant time operation

an operation that, for a given processor, always operates in the same amount of time, regardless of input values

O(5) has a _____ runtime complexity. constant linear exponential

constant explanation: 5 does not change with the input size, so the runtime of O(5) remains constant.

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 explanation: 2 operations execute before the loop. The loop condition and 2 operations in the loop execute N times, yielding 3N operations. The loop condition is checked 1 additional time before the loop terminates. The total operations as a function of N can be expressed as 2 + 3N + 1, or 3N + 3.

O(N log N) has a _____ runtime complexity. constant linearithmic logarithmic

linearithmic explanation: The first N indicates the runtime complexity is linear, then multiplying with log N results in a linearithmic runtime complexity. 3)

What is Asymptotic notation?

the classification of runtime complexity that uses functions that indicate only the growth rate of a bounding function

What is an algorithm's runtime?

the time the algorithm takes to execute


Set pelajaran terkait

Life Insurance Exam Practice Questions

View Set

BIOLOGY - Unit 1 ( DNA and Cell Structure )

View Set

Bio: exam 3 review launchpad questions

View Set

Geography 107 Exam 2 HW Questions

View Set

Secured Transactions Priority Practice Qs

View Set

RESEARCH: TEXT EXPLICIT, TEXT IMPLICIT, SCRIPT SPECIFIC

View Set

Constitutional Law Unsure/Incorrect Question

View Set