You must locate a value in a small list whose elements are in no particular order. Which algorithm is the most appropriate choice?
06 — Algorithms and Correctness Online Quiz Questions
Use this free practice quiz with 20 questions to review 06 — Algorithms and Correctness, test your knowledge, and prepare for your next test or exam.
True or false: Binary search requires its input list to be sorted in order to guarantee its usual search behavior.
- A
True
- B
False
A linear search examines a list of 25 elements. What is the maximum number of element comparisons it can make?
In an algorithm specification, a statement describing what is guaranteed to be true after successful execution is a .
Which worst-case running time best describes merge sort for a list of n elements?
- A
O(n)
- B
O(n log n)
- C
O(n²)
- D
O(1)
True or false: Testing an algorithm on many examples alone proves that it is correct for every valid input.
- A
True
- B
False
What value does the recursive factorial algorithm return for factorial(0)?
A loop-invariant proof has three parts: maintenance, plus before the first iteration and when the loop stops.
Select all components that belong in a useful algorithm specification.
- A
Inputs
- B
Outputs
- C
Preconditions
- D
Postconditions
- E
The programmer's preferred variable names
- F
The color of the user interface
Which statement best explains when insertion sort can be a practical choice?
- A
It always has O(n log n) running time and requires O(n) extra space.
- B
It requires the input list to be sorted before it starts.
- C
It uses O(1) extra space and can work well on nearly sorted lists.
- D
It cannot preserve the relative order of equal records.
Select all statements that are components of total correctness.
- A
Partial correctness
- B
Termination
- C
Testing on sample inputs
- D
Use of constant extra space
- E
A recursive implementation
Explain the difference between an algorithm specification and an implementation, and explain one practical benefit of keeping them separate.
Which statement is an appropriate loop invariant for insertion sort immediately before iteration i?
- A
Before iteration i, the entire list A is already sorted.
- B
Before iteration i, A[0:i] is sorted and contains the elements originally in those positions.
- C
After iteration i, the list contains only unique values.
- D
Before iteration i, the unsorted suffix is guaranteed to be in decreasing order.
A program must find a value in a small list whose elements are not sorted. Which algorithm is the most appropriate choice?
- A
Binary search, because it always examines every element
- B
Linear search, because it works without sorted input
- C
Merge sort, because it rearranges the list first
- D
Insertion sort, because it searches a sorted prefix
Which condition is the base case in the merge-sort procedure described in the material?
- A
A list with zero or one element
- B
A list with exactly two elements
- C
A list containing no duplicate values
- D
A list whose elements are already in reverse order
Which statement correctly describes insertion sort?
- A
It repeatedly halves the list and requires O(n) extra space
- B
It always runs in O(n log n) time because it builds a prefix
- C
It inserts each next element into a sorted prefix and can take O(n²) time
- D
It compares each element with every other element but never moves values
True or false: Testing an algorithm on many examples alone is sufficient to prove that it is correct for every valid input.
- A
True
- B
False
Using the factorial definition in the material, what is the value of factorial(5)?
What is the single term for the part of a loop-invariant proof that shows the invariant is true before the first iteration?
Why is sorted input a necessary precondition for binary search?
- A
It must use constant extra space
- B
It must preserve the relative order of duplicate records
- C
It examines every element before returning an answer
- D
It relies on ordering to discard a half of the remaining range