1. Home
  2. Explore
  3. Computer Science
  4. Algorithm Complexity Analysis

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Algorithm Complexity Analysis

  • 65Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Search Algorithms

What is a search algorithm?

65 questions

  1. 1

    What is a search algorithm?

    Model answer

    A search algorithm finds an item or group of items in a collection.

    Explanation

    Searching is used in applications such as web search and locating a student record.

  2. 2

    What is the difference between an implicit and an explicit collection in a search problem?

    Model answer

    An implicit collection is defined indirectly, such as all numbers in an interval, while an explicit collection is directly represented, such as a list of student records.

    Explanation

    Square-root approximation methods search an implicit collection, whereas finding a record in a list searches an explicit collection.

  3. 3

    Give two examples of search algorithms that operate on an implicit collection.

    Model answer

    Exhaustive enumeration, bisection search, and Newton-Raphson methods are examples of searches over implicit collections.

    Explanation

    These methods search among possible numerical values rather than among explicitly stored list elements.

  4. 4

    How does linear search operate on a list?

    Model answer

    It examines list elements one at a time until it finds the target or reaches the end of the list.

    Explanation

    Linear search is a brute-force or exhaustive-enumeration method.

  5. 5

    Why does linear search not require a sorted list?

    Model answer

    Because it checks each element independently, the elements may appear in any order.

    Explanation

    Linear search does not use relationships between neighboring elements.

  6. 6

    What is the worst-case complexity of linear search, and when does it occur?

    Model answer

    Its worst-case complexity is O(n)O(n)O(n), occurring when the target is absent or is last in the list.

    Explanation

    In the worst case, every one of the nnn elements must be examined.

  7. 7

    Why does returning immediately after finding an item improve average-case linear-search performance but not worst-case performance?

    Model answer

    It can stop early when the item appears near the beginning, but an absent or final item still requires examining the entire list.

    Explanation

    Worst-case analysis considers inputs that force the maximum amount of work.

  8. 8

    How can linear search take advantage of a sorted list?

    Model answer

    It can stop and return false as soon as it reaches an element larger than the target.

    Explanation

    All later elements are even larger, so the target cannot occur after that point.

  9. 9

    What is the worst-case complexity of linear search on a sorted list?

    Model answer

    It remains O(n)O(n)O(n) because the target may be absent or located near the end.

    Explanation

    Sorting may improve some searches, but it does not change the worst-case linear scan.

  10. 10

    Describe the main procedure of bisection, or binary, search.

    Model answer

    Examine the middle element, determine whether the target is smaller or larger, and continue searching only the corresponding half.

    Explanation

    The method repeatedly discards the half of the list that cannot contain the target.

  11. 11

    What condition must hold before binary search can be used on a list?

    Model answer

    The list must be sorted.

    Explanation

    The decision to discard one half depends on the ordering of the list.

  12. 12

    Why does binary search have logarithmic complexity?

    Model answer

    Each step reduces the remaining problem by one-half, so after kkk steps the remaining size is approximately n/2kn/2^kn/2k; reaching one element requires k=log2nk = \\log_2 nk=log2​n steps.

    Explanation

    The number of halvings needed to reduce nnn elements to one is logarithmic in nnn.

  13. 13

    Why is the work inside each binary-search step considered constant?

    Model answer

    Each step performs only a fixed number of comparisons and updates indices or pointers rather than copying the list.

    Explanation

    The total cost is therefore O(log⁡n)O(\log n)O(logn) rather than multiplied by the list length.

  14. 14

    What is the main tradeoff involved in sorting a list before searching it?

    Model answer

    Sorting adds an upfront cost, but it can reduce the cost of each subsequent search from linear to logarithmic.

    Explanation

    The tradeoff is worthwhile particularly when the same collection will be searched many times.

  15. 15

    Why can sorting not have a cost lower than linear time in the general setting described?

    Model answer

    A sorting algorithm must inspect each of the nnn elements at least once to determine their positions, so it requires at least O(n)O(n)O(n) work.

    Explanation

    An algorithm that never examines an element cannot reliably place it in sorted order.

  16. 16

    What does it mean to amortize the cost of sorting?

    Model answer

    It means spreading the one-time sorting cost across many searches performed on the sorted collection.

    Explanation

    The sorting expense becomes less significant as the number of searches increases.

  17. 17

    For kkk searches on a list of nnn elements, what is the approximate cost of using binary search after sorting?

    Model answer

    The cost is approximately O(sort(n)+klog⁡n)O(\text{sort}(n) + k\log n)O(sort(n)+klogn).

    Explanation

    The first term is the one-time sorting cost, and the second term accounts for kkk logarithmic searches.

  18. 18

    When is sorting before searching especially advantageous?

    Model answer

    It is advantageous when many searches will be performed, because the one-time sorting cost is amortized over the searches.

    Explanation

    For sufficiently large kkk, repeated linear searches cost more than sorting once and using binary search.

  19. 19

    What is the goal of a sorting algorithm?

    Model answer

    Its goal is to arrange the elements of a list in a specified order, such as increasing numerical order.

    Explanation

    Sorting creates an ordered collection that can support efficient processing and searching.

  20. 20

    What is BOGO sort, or permutation sort?

    Model answer

    It repeatedly rearranges a list randomly, or examines permutations, until it encounters a sorted arrangement.

    Explanation

    The method is intentionally inefficient and is also called monkey sort, stupid sort, slow sort, or shotgun sort.

  21. 21

    What are the best-case and average-case complexities of BOGO sort?

    Model answer

    Its best case is O(n)O(n)O(n), while its average case is approximately O(n!)O(n!)O(n!).

    Explanation

    The best case checks an already sorted list, whereas the average case may require examining many permutations.

  22. 22

    Why is the worst-case behavior of randomized BOGO sort effectively unbounded?

    Model answer

    Random rearrangement may continue indefinitely without producing a sorted permutation.

    Explanation

    There is no fixed maximum number of random attempts before success.

  23. 23

    How does bubble sort process a list?

    Model answer

    It repeatedly compares adjacent elements and swaps them when they are out of order, causing large elements to move toward the end.

    Explanation

    The repeated movement of larger elements gives the algorithm its name.

  24. 24

    What condition allows bubble sort to stop?

    Model answer

    It stops after a complete pass makes no swaps.

    Explanation

    A swap-free pass shows that every adjacent pair is already in the correct order.

  25. 25

    What does bubble sort guarantee after its first pass?

    Model answer

    The largest element in the list is at the end.

    Explanation

    The largest element is repeatedly swapped to the right as the pass proceeds.

  26. 26

    What does bubble sort guarantee after its second pass and subsequent passes?

    Model answer

    After the second pass, the second-largest element is in the second-to-last position; generally, after each pass, the next-largest unsorted element is placed correctly.

    Explanation

    Each pass fixes one additional element at the end of the list.

  27. 27

    What is the worst-case complexity of bubble sort?

    Model answer

    Its worst-case complexity is O(n2)O(n^2)O(n2).

    Explanation

    It may perform up to nnn passes, with each pass examining O(n)O(n)O(n) adjacent pairs.

  28. 28

    Why is bubble sort classified as quadratic even though some inputs require fewer passes?

    Model answer

    Worst-case analysis considers inputs that require the maximum number of passes, producing two nested linear processes and therefore O(n2)O(n^2)O(n2) work.

    Explanation

    The actual number of passes may be smaller for favorable inputs, but the worst-case order remains quadratic.

  29. 29

    How does selection sort construct a sorted list?

    Model answer

    It finds the smallest element in the unsorted portion and places it at the beginning of that portion.

    Explanation

    The sorted prefix grows by one element on each pass.

  30. 30

    What invariant does selection sort maintain?

    Model answer

    After iii steps, the first iii elements are sorted, and every element in that prefix is no larger than any element remaining in the suffix.

    Explanation

    This invariant describes both the ordering of the prefix and its relationship to the unsorted remainder.

  31. 31

    How does the loop-invariant proof establish that selection sort is correct?

    Model answer

    The invariant is true for the empty prefix, and each iteration moves the smallest suffix element to the end of the sorted prefix, preserving the invariant until the whole list is sorted.

    Explanation

    The base case and preservation step establish correctness by induction.

  32. 32

    Why is the base case of the selection-sort invariant straightforward?

    Model answer

    An empty prefix is already sorted, and it contains no element that could exceed an element in the suffix.

    Explanation

    The invariant holds automatically before any elements have been selected.

  33. 33

    How many major passes does selection sort make?

    Model answer

    It makes at most nnn passes, with the suffix starting at positions 0,1,2,…,n−10, 1, 2, \ldots, n-10,1,2,…,n−1.

    Explanation

    Each pass advances the boundary between the sorted prefix and unsorted suffix by one position.

  34. 34

    What is the worst-case complexity of selection sort?

    Model answer

    Its worst-case complexity is O(n2)O(n^2)O(n2).

    Explanation

    The algorithm performs a linear search through a shrinking suffix on each of a linear number of passes.

  35. 35

    Why does the particular number of swaps in a selection-sort implementation not change its asymptotic complexity?

    Model answer

    The dominant work is the repeated comparisons through the remaining suffix, which is quadratic regardless of whether elements are swapped immediately or only once per pass.

    Explanation

    Different swap implementations may change constant factors but not the order of growth.

  36. 36

    What is the divide-and-conquer strategy used by merge sort?

    Model answer

    It divides a list into smaller sublists, recursively sorts them, and then combines the sorted sublists.

    Explanation

    The algorithm solves smaller versions of the same problem before constructing the full solution.

  37. 37

    What is the base case of merge sort?

    Model answer

    A list with zero or one element is already sorted.

    Explanation

    No comparisons or rearrangement are needed for such a list.

  38. 38

    How does merge sort divide a nontrivial list?

    Model answer

    It splits the list into two sublists, typically at the midpoint, and recursively applies merge sort to each sublist.

    Explanation

    Each recursive call operates on a problem approximately half the size of the previous one.

  39. 39

    How are two sorted lists merged?

    Model answer

    Compare the first remaining element of each list, append the smaller one to the result, and repeat until one list is empty; then append the remainder of the other list.

    Explanation

    The sorted order means only the front elements need to be compared.

  40. 40

    Why can the remainder of one list be copied directly after the other list becomes empty?

    Model answer

    Because the remaining list is already sorted, and all of its elements are at least as large as the elements already placed in the result.

    Explanation

    The merge process has already selected every smaller available element.

  41. 41

    Why is merging two sorted lists linear in their combined length?

    Model answer

    Each element is copied into the result once, and the algorithm examines the lists in a single pass, so the work is O(n)O(n)O(n) for nnn total elements.

    Explanation

    The number of comparisons is also linear in the number of elements.

  42. 42

    What is the maximum number of comparisons needed to merge two sorted lists?

    Model answer

    It is no greater than the number of elements in the longer list, up to the linear bound determined by the combined input size.

    Explanation

    Each comparison removes one front element from one of the lists, and the remaining elements can be copied without further comparisons.

  43. 43

    Why does merge sort have logarithmic depth?

    Model answer

    Each division halves the problem size, so reducing a list of size nnn to lists of size one requires O(log⁡n)O(\log n)O(logn) levels.

    Explanation

    For example, a list of size 888 becomes subproblems of sizes 444, 222, and 111.

  44. 44

    Why does merge sort perform O(n)O(n)O(n) work at each recursion level?

    Model answer

    Although the sublists become smaller, the total number of elements across all sublists at a level remains nnn, and each element is processed a constant number of times during merging.

    Explanation

    The number of subproblems increases as their individual sizes decrease, preserving a linear total workload per level.

  45. 45

    What is the overall worst-case complexity of merge sort?

    Model answer

    Merge sort has worst-case complexity O(nlog⁡n)O(n\log n)O(nlogn).

    Explanation

    There are O(log⁡n)O(\log n)O(logn) recursion levels, each requiring O(n)O(n)O(n) merging work.

  46. 46

    Why is merge sort called a log-linear algorithm?

    Model answer

    Its running time is proportional to nlog⁡nn\log nnlogn, combining linear work per level with a logarithmic number of levels.

    Explanation

    Log-linear growth is slower than quadratic growth for sufficiently large inputs.

  47. 47

    Compare bubble sort and selection sort with merge sort in terms of worst-case complexity.

    Model answer

    Bubble sort and selection sort are O(n2)O(n^2)O(n2), while merge sort is O(nlog⁡n)O(n\log n)O(nlogn).

    Explanation

    Merge sort scales better because it uses recursive division and linear-time merging.

  48. 48

    Why is merge sort preferable to quadratic sorting algorithms for large lists?

    Model answer

    Its O(nlog⁡n)O(n\log n)O(nlogn) growth is substantially slower than the O(n2)O(n^2)O(n2) growth of bubble sort and selection sort as nnn increases.

    Explanation

    The difference in growth rates becomes increasingly important for large inputs.

  49. 49

    In what sense is merge sort described as the best worst-case sorting method in this discussion?

    Model answer

    No faster general worst-case sorting algorithm was identified in the discussion; O(nlog⁡n)O(n\log n)O(nlogn) was treated as the best available worst-case growth rate.

    Explanation

    Some algorithms may have better average-case behavior under particular conditions, but the focus here is worst-case performance.

  50. 50

    Why is worst-case complexity often emphasized over average-case complexity?

    Model answer

    Worst-case analysis gives a guaranteed upper bound on the work required, even for unfavorable inputs.

    Explanation

    An algorithm that is usually fast may still be unacceptable if certain inputs cause excessive work.

  51. 51

    How does the hierarchy of algorithmic growth rates guide algorithm selection?

    Model answer

    For large inputs, algorithms with slower-growing complexity, such as logarithmic or linear algorithms, are generally preferred over quadratic or exponential algorithms.

    Explanation

    The goal is to choose an algorithm as high as possible in the efficiency hierarchy.

  52. 52

    What computational tasks can be represented using data structures?

    Model answer

    Data structures such as tuples, lists, dictionaries, and more complex structures can represent and organize knowledge for computation.

    Explanation

    Choosing an appropriate representation is a central part of designing a computational solution.

  53. 53

    How do iteration and loops function as computational metaphors?

    Model answer

    They provide a way to express repeated actions systematically, allowing an algorithm to process multiple elements or repeat a procedure.

    Explanation

    Search and sorting algorithms use loops to apply the same operations across a collection.

  54. 54

    How does recursion help solve problems?

    Model answer

    Recursion breaks a problem into simpler instances of the same problem and combines their solutions.

    Explanation

    Merge sort illustrates recursion by sorting smaller sublists before merging them.

  55. 55

    What is abstraction in computational problem solving?

    Model answer

    Abstraction captures a computation inside a procedure while hiding its internal implementation details.

    Explanation

    Users can rely on a procedure's behavior without needing to know how it produces its result.

  56. 56

    What does it mean for a procedure to provide a contract?

    Model answer

    A procedure contract specifies what result the procedure will deliver, allowing other code to use it without depending on its internal operations.

    Explanation

    The contract supports delegation and modular reasoning.

  57. 57

    How do classes and methods help modularize software systems?

    Model answer

    They combine data with the operations that act on that data, organizing a system into reusable modules.

    Explanation

    This structure makes complex systems easier to design and manage.

  58. 58

    What are the three A's of computational thinking?

    Model answer

    They are abstraction, automation, and algorithmic thinking.

    Explanation

    These describe choosing useful representations, mechanizing processes, and specifying procedures for solving problems.

  59. 59

    What does abstraction involve when solving a computational problem?

    Model answer

    It involves identifying the right representation, separating important components, and decomposing a problem into relationships that can be used to solve it.

    Explanation

    Good abstraction removes irrelevant details while preserving the structure needed for reasoning.

  60. 60

    What does automation mean in computational thinking?

    Model answer

    Automation means mechanizing an abstraction by expressing it as a sequence of steps that a computer can execute.

    Explanation

    The process is translated into a programming language or other executable form.

  61. 61

    Why does expressing an algorithm clearly improve problem solving?

    Model answer

    Describing the steps precisely forces the problem solver to clarify and crystallize the underlying reasoning.

    Explanation

    A precise algorithm exposes ambiguities that informal descriptions may hide.

  62. 62

    What does computational problem solving involve?

    Model answer

    It involves transforming a problem from fields such as biology, chemistry, mathematics, or physics into an algorithm that can simulate or evaluate the situation.

    Explanation

    Computational thinking applies general methods across many disciplines.

  63. 63

    Why is it important to recognize that some problems are inherently more difficult than others?

    Model answer

    Different problems have different complexity requirements, so the best possible solution depends partly on the problem's inherent difficulty.

    Explanation

    Complexity classes help compare how computational work grows across problem types.

  64. 64

    How does divide and conquer support computational problem solving?

    Model answer

    It breaks a difficult problem into smaller instances, solves those instances, and constructs the overall solution from them.

    Explanation

    This strategy is central to efficient algorithms such as merge sort.

  65. 65

    What is the role of recursion beyond sorting algorithms?

    Model answer

    Recursion is a general method for expressing solutions in terms of simpler versions of the same problem and appears in many kinds of computational tasks.

    Explanation

    Its usefulness extends beyond any single algorithm or application area.

1 of 65
Question 1 of 65

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent