Algorithm Complexity Analysis
- 47Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary purpose of a search algorithm?
What is the primary purpose of a search algorithm?
47 questions
- 1
What is the primary purpose of a search algorithm?
- A
To arrange every item in a collection into order
- B
To replace a collection with a recursive procedure
- C
To reduce every computation to constant time
- D
To find an item or group of items in a collection
Correct answer
DTo find an item or group of items in a collection
Explanation
A search algorithm is used to find an item or group of items from a collection.
- A
- 2
What distinguishes an explicit search collection from an implicit search collection?
- A
An explicit collection can contain numbers, whereas an implicit collection can contain only strings
- B
An explicit collection must be sorted, whereas an implicit collection must be unsorted
- C
An explicit collection uses recursion, whereas an implicit collection uses only iteration
- D
An explicit collection contains identifiable stored elements, whereas an implicit collection is defined by a range or rule
Correct answer
DAn explicit collection contains identifiable stored elements, whereas an implicit collection is defined by a range or rule
Explanation
An explicit collection consists of identifiable elements such as student records, while an implicit collection may be defined by a range of possible values, such as numbers between two endpoints.
- A
- 3
Which example represents a search over an implicit collection?
- A
Finding a record in a database indexed by identification number
- B
Finding a square root by considering possible numerical values
- C
Finding a word in a sorted dictionary of words
- D
Finding a student in a stored list of student records
Correct answer
BFinding a square root by considering possible numerical values
Explanation
Square-root methods such as exhaustive enumeration, bisection search, and Newton-Raphson search possible values from an implicitly defined numerical range.
- A
- 4
What is the key advantage of linear search over an unsorted list?
- A
It automatically rearranges the list during the search
- B
It has logarithmic worst-case complexity
- C
It always examines only half of the list
- D
It does not require the list to be sorted
Correct answer
DIt does not require the list to be sorted
Explanation
Linear search examines list elements in sequence and can operate on a list in arbitrary order.
- A
- 5
What is the worst-case time complexity of linear search over a list of length n?
- A
O(logn)
- B
O(n)
- C
O(n2)
- D
O(1)
Correct answer
BO(n)
Explanation
In the worst case, the target is absent or appears at the end, so linear search examines all n elements. Its complexity is O(n).
- A
- 6
Why does returning immediately after finding an item improve average linear-search performance but not worst-case performance?
- A
The early return adds a second loop to the algorithm
- B
The list becomes sorted only after the search finishes
- C
The algorithm must always examine every element twice
- D
The item may still be absent or located at the final position
Correct answer
DThe item may still be absent or located at the final position
Explanation
An early return can reduce the number of checks when the item appears early, but the worst case still requires examining the entire list.
- A
- 7
What additional condition allows bisection search to discard half of a list after each comparison?
- A
The list must have an even number of elements
- B
The list must contain only unique values
- C
The list must be sorted
- D
The list must be stored in a dictionary
Correct answer
CThe list must be sorted
Explanation
Bisection search relies on sorted order to determine whether the target can occur in the lower or upper half of the remaining list.
- A
- 8
During bisection search, what happens when the target is greater than the middle element?
- A
The search continues only in the upper half
- B
The entire list is searched linearly
- C
The search continues only in the lower half
- D
The list is sorted again before continuing
Correct answer
AThe search continues only in the upper half
Explanation
Because the list is sorted, a target greater than the middle element cannot be in the lower half, so the search continues in the upper half.
- A
- 9
Why does bisection search require O(logn) steps on a sorted list?
- A
The algorithm examines one additional element at every step
- B
The list is copied into two complete lists at every step
- C
The algorithm performs a constant number of comparisons total
- D
The remaining problem size is divided by two at every step
Correct answer
DThe remaining problem size is divided by two at every step
Explanation
After k steps, the remaining fraction is approximately 1/2k. Reaching one element requires k=log2n steps.
- A
- 10
Why is the work inside each recursive bisection-search call considered constant time in the described implementation?
- A
The implementation sorts the list during every recursive call
- B
The implementation checks every element before making a recursive call
- C
The implementation passes index boundaries rather than copying sublists
- D
The implementation stores every possible sublist in memory
Correct answer
CThe implementation passes index boundaries rather than copying sublists
Explanation
The implementation passes pointers or index boundaries, so each call performs only a constant amount of additional work besides the recursive reduction.
- A
- 11
Why can sorting once be worthwhile when many searches will be performed?
- A
Sorting eliminates the need to compare the target with list elements
- B
The one-time sorting cost can be amortized across many logarithmic searches
- C
Sorting makes every search take constant time regardless of list size
- D
Sorting makes the list smaller after each search
Correct answer
BThe one-time sorting cost can be amortized across many logarithmic searches
Explanation
Although sorting has an initial cost, that cost can be spread across many searches, each of which can then use bisection search in O(logn) time.
- A
- 12
Why can sorting a list not have a worst-case complexity better than linear time under the lecture's argument?
- A
Sorting always requires recursively splitting the list into single elements
- B
A sorted list must contain fewer elements than the original list
- C
Every sorting algorithm must compare every pair of elements
- D
The algorithm must inspect every element at least once to determine its placement
Correct answer
DThe algorithm must inspect every element at least once to determine its placement
Explanation
A sorting algorithm must at least look at each of the n elements, so sorting requires at least O(n) work.
- A
- 13
What is the defining strategy of BOGO sort?
- A
Repeatedly compare adjacent elements and swap inverted pairs
- B
Repeatedly select the smallest remaining element
- C
Recursively split the list and merge sorted sublists
- D
Randomly or exhaustively generate permutations until one is sorted
Correct answer
DRandomly or exhaustively generate permutations until one is sorted
Explanation
BOGO sort repeatedly rearranges the elements, checking whether the resulting permutation is sorted.
- A
- 14
What are the best-case and average-case complexities of BOGO sort as described?
- A
Best case O(n) and average case approximately O(n!)
- B
Best case O(n2) and average case O(n)
- C
Best case O(nlogn) and average case O(n2)
- D
Best case O(1) and average case O(logn)
Correct answer
ABest case O(n) and average case approximately O(n!)
Explanation
If the list is already sorted, BOGO sort needs only a linear check. Its average behavior is associated with trying permutations, giving approximately O(n!) work.
- A
- 15
Why is the worst-case behavior of randomly implemented BOGO sort especially problematic?
- A
It requires the list to contain duplicate elements
- B
It cannot determine whether a list is already sorted
- C
There is no useful finite upper bound on how long random attempts may continue
- D
It always stops after exactly n comparisons
Correct answer
CThere is no useful finite upper bound on how long random attempts may continue
Explanation
Random rearrangements could continue indefinitely before producing a sorted ordering, so the worst-case running time has no useful practical bound.
- A
- 16
What is the central operation of bubble sort?
- A
Compare adjacent elements and swap them when they are in the wrong order
- B
Select the smallest element in the entire remaining list
- C
Split the list into two recursively smaller lists
- D
Generate random permutations until the list is ordered
Correct answer
ACompare adjacent elements and swap them when they are in the wrong order
Explanation
Bubble sort repeatedly compares neighboring elements and swaps an inverted pair, moving larger elements toward the end.
- A
- 17
Why is it called bubble sort?
- A
The algorithm creates two lists that expand like bubbles
- B
The algorithm compares only elements at the center of the list
- C
Large elements are repeatedly moved toward the end of the list
- D
The algorithm stores elements inside circular bubble-shaped structures
Correct answer
CLarge elements are repeatedly moved toward the end of the list
Explanation
The larger elements appear to bubble toward the end of the list through repeated adjacent swaps.
- A
- 18
When can bubble sort safely terminate?
- A
After exactly one pass through the list
- B
After a complete pass makes no swaps
- C
When the list has been split into single-element sublists
- D
After the first element reaches the front
Correct answer
BAfter a complete pass makes no swaps
Explanation
If a complete pass makes no swaps, every adjacent pair is in order, so the entire list is sorted.
- A
- 19
What ordering guarantee does one complete bubble-sort pass provide?
- A
The largest currently unsorted element reaches the end of the unsorted region
- B
The list is divided into two sorted halves
- C
The smallest element reaches the beginning of the entire list
- D
Every element is placed in its final position
Correct answer
AThe largest currently unsorted element reaches the end of the unsorted region
Explanation
Repeatedly moving the larger member of each adjacent pair causes the largest unsorted element to reach the end after a pass.
- A
- 20
What is the worst-case time complexity of bubble sort?
- A
O(n2)
- B
O(n)
- C
O(logn)
- D
O(1)
Correct answer
AO(n2)
Explanation
Bubble sort may make O(n) passes, with each pass performing O(n) work, producing O(n2) worst-case complexity.
- A
- 21
What is the defining strategy of selection sort?
- A
Randomly permute the list until it becomes ordered
- B
Merge two already sorted lists into one larger list
- C
Move the largest element to the end using adjacent swaps
- D
Find the smallest remaining element and place it at the next sorted position
Correct answer
DFind the smallest remaining element and place it at the next sorted position
Explanation
Selection sort repeatedly selects the smallest element in the unsorted suffix and places it at the next position in the sorted prefix.
- A
- 22
After i steps of selection sort, what can be guaranteed?
- A
The remaining suffix has been divided into equal-sized parts
- B
The first i elements are sorted and no larger than any element in the remaining suffix
- C
The last i elements are sorted and no smaller than any element in the prefix
- D
Exactly i adjacent swaps have occurred
Correct answer
BThe first i elements are sorted and no larger than any element in the remaining suffix
Explanation
Each step places the next-smallest remaining element at the end of the sorted prefix, so the first i elements are sorted and precede all elements in the suffix.
- A
- 23
What is the loop invariant used to justify selection sort?
- A
The number of swaps equals the number of elements in the sorted prefix
- B
The unsorted suffix is ordered, and every suffix element is no larger than every prefix element
- C
The list contains exactly one misplaced element after every pass
- D
The sorted prefix is ordered, and every prefix element is no larger than every suffix element
Correct answer
DThe sorted prefix is ordered, and every prefix element is no larger than every suffix element
Explanation
The invariant states that the prefix is sorted and that no element in the prefix is larger than the smallest element in the suffix.
- A
- 24
Why is the base case of the selection-sort loop invariant immediate?
- A
The first comparison automatically places every element correctly
- B
An empty prefix is trivially sorted and imposes no ordering violation
- C
The entire list is already sorted before the loop begins
- D
The suffix contains only one element at the base case
Correct answer
BAn empty prefix is trivially sorted and imposes no ordering violation
Explanation
At the beginning, the prefix is empty. An empty sequence is sorted, and there is no prefix element that could violate the relationship with the suffix.
- A
- 25
What is the worst-case time complexity of selection sort?
- A
O(n)
- B
O(n2)
- C
O(1)
- D
O(logn)
Correct answer
BO(n2)
Explanation
Selection sort makes approximately n passes and examines a decreasing but still linear number of remaining elements on each pass, resulting in O(n2) complexity.
- A
- 26
What is the main divide-and-conquer idea behind merge sort?
- A
Select the smallest element and append it to the output one at a time
- B
Split the list into smaller lists, sort them recursively, and merge the results
- C
Compare adjacent elements until no swaps remain
- D
Generate every permutation and retain the first sorted one
Correct answer
BSplit the list into smaller lists, sort them recursively, and merge the results
Explanation
Merge sort divides the list into smaller subproblems, recursively sorts them, and then combines the sorted sublists.
- A
- 27
What is the base case for merge sort?
- A
A list with zero or one element is returned because it is already sorted
- B
A list with two elements is always swapped
- C
A list with n elements is sorted by linear search
- D
A list is returned only after every element has been compared with every other element
Correct answer
AA list with zero or one element is returned because it is already sorted
Explanation
Lists of length zero or one are already sorted, so merge sort returns them without further division.
- A
- 28
How does merge sort combine two sorted lists?
- A
Concatenate them and randomly permute the combined list
- B
Place the smallest element of each list at the end of the result
- C
Repeatedly compare their first remaining elements and append the smaller one
- D
Compare every element in the first list with every element in the second
Correct answer
CRepeatedly compare their first remaining elements and append the smaller one
Explanation
The merge operation compares the first remaining element of each sorted list, appends the smaller one, and continues until one list is empty.
- A
- 29
What happens after one of the two lists becomes empty during a merge?
- A
The remaining elements are randomly rearranged
- B
The remaining elements of the other sorted list are copied to the result
- C
The entire merge operation is restarted
- D
The remaining elements are discarded because they are already known
Correct answer
BThe remaining elements of the other sorted list are copied to the result
Explanation
Once one sorted list is empty, all remaining elements of the other list can be appended directly in their existing order.
- A
- 30
Why does merging two sorted lists require linear work?
- A
Every pair of elements must be compared before any element is copied
- B
Only the first element of each list is ever examined
- C
The lists are recursively split again during the merge
- D
Each element is copied once, and each comparison advances at least one list index
Correct answer
DEach element is copied once, and each comparison advances at least one list index
Explanation
The merge scans through the two lists, copying each element and advancing an index, so the work is proportional to the combined list length.
- A
- 31
What is the maximum number of comparisons needed to merge two sorted lists with a combined length of n?
- A
Exactly one comparison
- B
At most n comparisons
- C
At most logn comparisons
- D
Exactly n2 comparisons
Correct answer
BAt most n comparisons
Explanation
Each comparison removes one element from consideration, and the process ends when one list is empty, so at most n comparisons are needed.
- A
- 32
Why does merge sort have O(nlogn) complexity?
- A
It performs constant work because every sublist is sorted immediately
- B
It performs O(n2) comparisons but copies only O(logn) elements
- C
There are O(n) levels, each performing O(logn) total work
- D
There are O(logn) levels, each performing O(n) total merging work
Correct answer
DThere are O(logn) levels, each performing O(n) total merging work
Explanation
Each level of recursive splitting and merging processes all n elements in total, and halving the problem creates O(logn) levels. Thus the total is O(nlogn).
- A
- 33
At a fixed level of merge sort's recursion tree, why is the total amount of merging work O(n)?
- A
The sublists become smaller, but their combined number of elements remains n
- B
Only one sublist exists at each level
- C
The algorithm examines only the first element of each sublist
- D
The number of elements doubles at every level
Correct answer
AThe sublists become smaller, but their combined number of elements remains n
Explanation
Although there are more sublists at lower levels, their lengths sum to the original list length n, so all merges at that level together require O(n) work.
- A
- 34
Compared with bubble sort and selection sort, what is the main asymptotic advantage of merge sort?
- A
Merge sort avoids all comparisons, unlike both quadratic algorithms
- B
Merge sort works only on already sorted lists, making it faster by assumption
- C
Merge sort has O(nlogn) worst-case complexity instead of O(n2)
- D
Merge sort has O(1) worst-case complexity instead of O(n2)
Correct answer
CMerge sort has O(nlogn) worst-case complexity instead of O(n2)
Explanation
Bubble sort and selection sort are quadratic in the worst case, while merge sort achieves O(nlogn) worst-case complexity.
- A
- 35
In the lecture's comparison-based framework, what is the significance of merge sort's worst-case complexity?
- A
It guarantees constant-time sorting for sufficiently large lists
- B
It applies only when the input list is already sorted
- C
It represents the best worst-case performance discussed for general sorting
- D
It is slower than every quadratic algorithm on all inputs
Correct answer
CIt represents the best worst-case performance discussed for general sorting
Explanation
The lecture presents O(nlogn) as the best worst-case complexity achieved by the sorting methods under discussion, while noting that average-case improvements may exist in other settings.
- A
- 36
What does it mean to amortize the cost of sorting across multiple searches?
- A
Each search independently performs a complete sort
- B
The one-time sorting cost is distributed across the cost of many subsequent searches
- C
The list is shortened after every search so sorting becomes unnecessary
- D
The sorting cost is ignored because sorting takes no time
Correct answer
BThe one-time sorting cost is distributed across the cost of many subsequent searches
Explanation
When a list is searched many times, the initial sorting cost can be spread across those searches, making the average cost per search more favorable.
- A
- 37
If a list of length n is sorted once and then searched k times using bisection search, what is the search portion of the total cost?
- A
O(k2logn)
- B
O(k+n2)
- C
O(logk+n)
- D
O(klogn)
Correct answer
DO(klogn)
Explanation
Each bisection search costs O(logn), so k searches cost O(klogn), in addition to the one-time sorting cost.
- A
- 38
Which ordering correctly lists the growth rates from generally more efficient to less efficient as input size increases?
- A
Exponential, quadratic, log-linear, linear, logarithmic, constant
- B
Linear, constant, quadratic, logarithmic, exponential, log-linear
- C
Constant, logarithmic, linear, log-linear, quadratic, exponential
- D
Logarithmic, exponential, constant, quadratic, linear, log-linear
Correct answer
CConstant, logarithmic, linear, log-linear, quadratic, exponential
Explanation
The lecture emphasizes the hierarchy from constant through logarithmic, linear, log-linear, quadratic, and exponential growth, with earlier classes generally scaling better.
- A
- 39
What is the purpose of abstraction in computational problem solving?
- A
To replace all data structures with numerical constants
- B
To require users to understand every internal operation of a procedure
- C
To encapsulate a computation in a procedure so users can rely on its behavior without knowing its implementation
- D
To prevent procedures from being reused in other programs
Correct answer
CTo encapsulate a computation in a procedure so users can rely on its behavior without knowing its implementation
Explanation
Abstraction hides implementation details while exposing a reliable interface or behavior, allowing computation to be delegated and reused.
- A
- 40
How do classes and methods help modularize a software system?
- A
They eliminate the need for algorithms and procedures
- B
They ensure every operation is performed recursively
- C
They combine data with the operations that act on that data
- D
They store only numerical values without associated behavior
Correct answer
CThey combine data with the operations that act on that data
Explanation
Classes and methods provide a way to organize data together with the operations that manipulate it, supporting modular system design.
- A
- 41
What is the computational-thinking strategy called divide and conquer?
- A
Break a problem into simpler versions of the same problem and combine their solutions
- B
Try every possible solution without reducing the problem
- C
Solve only the largest portion of a problem and ignore the rest
- D
Replace an algorithm with a fixed constant response
Correct answer
ABreak a problem into simpler versions of the same problem and combine their solutions
Explanation
Divide and conquer decomposes a problem into smaller related subproblems, solves them, and constructs the overall solution from their results.
- A
- 42
What role does recursion play in computational problem solving?
- A
It requires solving the entire problem repeatedly without reduction
- B
It prevents a problem from having a base case
- C
It guarantees that every algorithm has constant complexity
- D
It expresses a problem in terms of simpler instances of the same problem
Correct answer
DIt expresses a problem in terms of simpler instances of the same problem
Explanation
Recursion is useful because it breaks a problem into simpler versions of itself and stops at a base case.
- A
- 43
What are the three A's of computational thinking presented in the lecture?
- A
Addition, approximation, and analysis
- B
Arrays, arithmetic, and assembly
- C
Abstraction, automation, and algorithms
- D
Access, allocation, and assignment
Correct answer
CAbstraction, automation, and algorithms
Explanation
The three A's are abstraction, automation, and algorithms.
- A
- 44
What does automation mean in computational thinking?
- A
Mechanizing an abstraction as a sequence of executable steps
- B
Solving a problem only through manual calculation
- C
Removing all procedures from a program
- D
Storing an abstraction without specifying any process
Correct answer
AMechanizing an abstraction as a sequence of executable steps
Explanation
Automation means turning an abstraction into a mechanized process that a computer can execute as a sequence of steps.
- A
- 45
Why can describing an algorithm improve a person's reasoning about a problem?
- A
It forces the person to state the process precisely and clarify the underlying ideas
- B
It guarantees that the algorithm will have constant complexity
- C
It replaces the need to test the algorithm on any examples
- D
It allows the person to avoid identifying the problem's inputs and outputs
Correct answer
AIt forces the person to state the process precisely and clarify the underlying ideas
Explanation
Writing or explaining an algorithm requires precise communication, which helps crystallize and clarify the reasoning behind the proposed solution.
- A
- 46
What does analyzing algorithmic complexity help determine?
- A
How computational cost changes as the input size grows
- B
Whether a data structure can store only numerical values
- C
How many lines of comments appear in a program
- D
Whether a program contains any syntactic errors
Correct answer
AHow computational cost changes as the input size grows
Explanation
Complexity analysis estimates how the cost of an algorithm grows as the size of its input increases.
- A
- 47
Why is computational thinking applicable to fields such as biology, chemistry, mathematics, and engineering?
- A
It replaces domain knowledge with random experimentation
- B
It applies only to problems involving sorted numerical lists
- C
It provides methods for representing problems and designing algorithms or simulations to analyze them
- D
It restricts problem solving to computer hardware design
Correct answer
CIt provides methods for representing problems and designing algorithms or simulations to analyze them
Explanation
Computational thinking allows problems from many fields to be represented, decomposed, automated, and analyzed through algorithms and simulations.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.