1. Home
  2. Explore
  3. Computer Science
  4. Searching and Sorting Algorithms

Study methods

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

Searching and Sorting Algorithms

  • 59Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Algorithms and Growth

What is an algorithm, in the sense used in the lecture?

59 questions

  1. 1

    What is an algorithm, in the sense used in the lecture?

    1. A

      A programming language used to write computer code

    2. B

      A particular arrangement of values stored in computer memory

    3. C

      A measurement of how much memory a computer contains

    4. D

      A step-by-step set of instructions for solving a problem

    Correct answer
    D

    A step-by-step set of instructions for solving a problem

    Explanation

    An algorithm is a set of step-by-step instructions for solving a problem, such as ordering information.

  2. 2

    Which description best captures sorting information?

    1. A

      Removing items that do not satisfy a condition selected by the user

    2. B

      Finding one requested item without changing the order of other items

    3. C

      Ordering it according to a chosen rule, such as size or alphabetic order

    4. D

      Storing every item in a separate location so it can be accessed at once

    Correct answer
    C

    Ordering it according to a chosen rule, such as size or alphabetic order

    Explanation

    Sorting means ordering information according to a rule or heuristic, such as smallest to largest or alphabetically.

  3. 3

    Why does counting people one at a time have linear running time?

    1. A

      Each additional person doubles the number of steps required

    2. B

      Each additional person requires roughly one additional counting step

    3. C

      The number of steps stays fixed regardless of the number of people

    4. D

      Each additional person requires one fewer step than the previous person

    Correct answer
    B

    Each additional person requires roughly one additional counting step

    Explanation

    Counting one person at a time requires work proportional to the number of people, so the running time grows linearly.

  4. 4

    Why does repeatedly combining pairs of counts take logarithmic steps in the attendance demonstration?

    1. A

      Each round combines groups and reduces the number of active counters by about half

    2. B

      Each round checks every person again and adds one fixed step

    3. C

      Each round increases the number of active counters by a fixed amount

    4. D

      Each round removes exactly one person from the counting process

    Correct answer
    A

    Each round combines groups and reduces the number of active counters by about half

    Explanation

    Pairing and combining counts repeatedly halves the number of active groups, so the number of rounds grows logarithmically with the group size.

  5. 5

    If the number of people in a pairwise-combining attendance process is doubled, how does the number of rounds change?

    1. A

      It increases by about one round

    2. B

      It stays exactly the same

    3. C

      It increases by about two rounds

    4. D

      It doubles

    Correct answer
    A

    It increases by about one round

    Explanation

    Each round approximately halves the groups, so doubling the initial number requires only about one additional halving round.

  6. 6

    What does Big O notation primarily describe in this lecture?

    1. A

      The average number of times a function calls itself

    2. B

      The minimum memory required to store an algorithm's input

    3. C

      An asymptotic upper bound on how an algorithm's running time grows

    4. D

      The exact number of operations an algorithm performs on every input

    Correct answer
    C

    An asymptotic upper bound on how an algorithm's running time grows

    Explanation

    Big O gives an upper bound on growth; it is commonly used to describe how an algorithm can perform in a worst-case situation.

  7. 7

    Why are constant factors such as dividing the input size by two usually ignored in Big O notation?

    1. A

      They apply only when an algorithm has no loops

    2. B

      They affect memory use but cannot affect running time

    3. C

      They do not change the dominant growth category as the input becomes large

    4. D

      They guarantee that all algorithms with the same notation take equal time

    Correct answer
    C

    They do not change the dominant growth category as the input becomes large

    Explanation

    Big O focuses on the dominant term as the input grows, so constant multipliers such as one-half do not change a linear growth classification.

  8. 8

    What does Omega notation describe?

    1. A

      An asymptotic upper bound on an algorithm's running time

    2. B

      An asymptotic lower bound on an algorithm's running time

    3. C

      The amount of additional space used by an algorithm

    4. D

      An exact running time for every possible input

    Correct answer
    B

    An asymptotic lower bound on an algorithm's running time

    Explanation

    Omega describes a lower bound, often interpreted as the algorithm's best-case performance in the examples discussed.

  9. 9

    When is Theta notation appropriate?

    1. A

      When the asymptotic upper and lower bounds have the same growth rate

    2. B

      When the best case is faster than the worst case

    3. C

      When the input has been sorted before the algorithm begins

    4. D

      When an algorithm has a recursive function but no base case

    Correct answer
    A

    When the asymptotic upper and lower bounds have the same growth rate

    Explanation

    Theta can describe an algorithm when its Big O upper bound and Omega lower bound match asymptotically.

  10. 10

    Why might sorting data before searching be worthwhile in some situations but wasteful in others?

    1. A

      Sorting uses no resources if the search algorithm is binary search

    2. B

      Its cost may pay off over many searches, but may not be worthwhile for a single search

    3. C

      Sorting always makes every search slower, regardless of how often the data is searched

    4. D

      The benefit of sorting depends only on the number of values, not on how often they are searched

    Correct answer
    B

    Its cost may pay off over many searches, but may not be worthwhile for a single search

    Explanation

    Sorting has an upfront cost. Repeated searches can amortize that cost, while sorting solely to perform one search may be less efficient than using linear search directly.

  11. 11

    What is the defining memory-layout property of an array described in the lecture?

    1. A

      Its values can only be accessed by examining the entire array

    2. B

      Its values are stored in separate, unrelated memory regions

    3. C

      Its values are stored in alphabetical order

    4. D

      Its values occupy consecutive locations in memory

    Correct answer
    D

    Its values occupy consecutive locations in memory

    Explanation

    An array is a contiguous chunk of memory whose values are stored back to back.

  12. 12

    In the simple memory model used in the lecture, how does a computer inspect array values?

    1. A

      It accesses one value at a time by going to that location

    2. B

      It reads only the first and last values and infers the rest

    3. C

      It accesses values only after converting the array into a string

    4. D

      It automatically sees every value in the array simultaneously

    Correct answer
    A

    It accesses one value at a time by going to that location

    Explanation

    The model assumes the computer must access each location individually rather than see the entire array at once.

  13. 13

    An array contains nnn values and uses zero-based indexing. What is the index of its final value?

    1. A

      n−1n-1n−1

    2. B

      000

    3. C

      nnn

    4. D

      n+1n+1n+1

    Correct answer
    A

    n−1n-1n−1

    Explanation

    With zero-based indexing, the first value is at index 000, so the last of nnn values is at index n−1n-1n−1.

  14. 14

    What kind of answer did the locker-search problem require?

    1. A

      A count of how many values were smaller than the target

    2. B

      The index of every value in the entire array

    3. C

      The sorted list of all values found in the lockers

    4. D

      A Boolean answer indicating whether the target value was present

    Correct answer
    D

    A Boolean answer indicating whether the target value was present

    Explanation

    The specified output was a true-or-false answer indicating whether the target denomination was present.

  15. 15

    Which procedure describes linear search through an array?

    1. A

      Sort the array and return the location of its largest element

    2. B

      Check the middle element and discard half of an unsorted array

    3. C

      Swap neighboring elements until the target reaches the first location

    4. D

      Check elements in sequence until the target is found or no elements remain

    Correct answer
    D

    Check elements in sequence until the target is found or no elements remain

    Explanation

    Linear search examines elements sequentially and stops when it finds the target or reaches the end.

  16. 16

    What is the worst-case running time of linear search through nnn values?

    1. A

      O(n)O(n)O(n)

    2. B

      O(log⁡n)O(\log n)O(logn)

    3. C

      O(1)O(1)O(1)

    4. D

      O(n2)O(n^2)O(n2)

    Correct answer
    A

    O(n)O(n)O(n)

    Explanation

    In the worst case, the target is last or absent, so linear search may inspect all nnn values.

  17. 17

    What is the best-case running time of linear search when the target may be first?

    1. A

      Ω(1)\Omega(1)Ω(1)

    2. B

      Ω(log⁡n)\Omega(\log n)Ω(logn)

    3. C

      Ω(n)\Omega(n)Ω(n)

    4. D

      Ω(n2)\Omega(n^2)Ω(n2)

    Correct answer
    A

    Ω(1)\Omega(1)Ω(1)

    Explanation

    If the target is the first element, linear search finds it in a constant number of steps, independent of the array's size.

  18. 18

    What key condition allows binary search to decide which half of an array can be discarded?

    1. A

      The values are stored as strings rather than integers

    2. B

      The values are sorted so their order supports less-than and greater-than decisions

    3. C

      The target is known to be located at one of the ends

    4. D

      The array contains an even number of values

    Correct answer
    B

    The values are sorted so their order supports less-than and greater-than decisions

    Explanation

    Binary search relies on sorted data: comparison with the middle value indicates which half can still contain the target.

  19. 19

    Why is binary search generally incorrect on an unsorted array?

    1. A

      A comparison with the middle value cannot reliably identify which half contains the target

    2. B

      It cannot operate on arrays that use zero-based indexing

    3. C

      It always returns the index of the middle value rather than searching

    4. D

      It must compare every pair of values before examining the middle

    Correct answer
    A

    A comparison with the middle value cannot reliably identify which half contains the target

    Explanation

    Without sorted order, a smaller or larger value is not guaranteed to lie on a particular side of the middle element.

  20. 20

    For an array of 777 elements using zero-based indices, which index is the middle when the index is computed by dividing the length by 222 and rounding down?

    1. A

      444

    2. B

      333

    3. C

      777

    4. D

      3.53.53.5

    Correct answer
    B

    333

    Explanation

    Integer division of 777 by 222 rounds down to 333, the middle index in an array indexed from 000 through 666.

  21. 21

    After checking the middle element in binary search, why does the next search range exclude that element?

    1. A

      Binary search is required to skip every other element after a comparison

    2. B

      The middle element is always guaranteed to be the largest value

    3. C

      The middle element has already been checked and need not be examined again

    4. D

      The middle element cannot be represented by an array index

    Correct answer
    C

    The middle element has already been checked and need not be examined again

    Explanation

    Once the middle value has been compared with the target, it should not be checked again; the remaining range is on one side of it.

  22. 22

    What should binary search return when there are no elements left to search?

    1. A

      The middle index, because no comparison is necessary

    2. B

      True, because the search has reached its stopping condition

    3. C

      The largest value previously encountered

    4. D

      False, because the target cannot be present in an empty range

    Correct answer
    D

    False, because the target cannot be present in an empty range

    Explanation

    An empty search range means there is no remaining location that can contain the target, so the search returns false.

  23. 23

    Which C construct is suitable for checking each element of a fixed-size array in sequence?

    1. A

      A conditional statement that checks only the first location

    2. B

      A structure declaration that creates a new data type

    3. C

      A return statement placed before any comparison

    4. D

      A loop that advances an index from the first location through the last

    Correct answer
    D

    A loop that advances an index from the first location through the last

    Explanation

    A loop can increment an index and compare each array value in turn, implementing linear search.

  24. 24

    Why must a linear-search implementation return false after the loop rather than immediately after the first mismatch?

    1. A

      A return statement automatically advances the loop index

    2. B

      The first mismatch proves that the array is sorted in reverse

    3. C

      A false result is valid only when the array has no elements

    4. D

      A mismatch at one location does not rule out a match at a later location

    Correct answer
    D

    A mismatch at one location does not rule out a match at a later location

    Explanation

    The target may occur later in the array, so the search should return false only after all locations have been checked.

  25. 25

    Why can a C compiler infer an array's size when it is initialized with a list of values?

    1. A

      It determines the size by examining the loop that will search the array

    2. B

      It assumes every initialized array contains seven values

    3. C

      It can count the values provided in the initializer

    4. D

      It reads the array's final value to determine the number of locations

    Correct answer
    C

    It can count the values provided in the initializer

    Explanation

    When an array is initialized with known values in braces, the compiler can count them and infer the array length.

  26. 26

    Why is using == to compare two strings in C not the appropriate way to test whether their text matches?

    1. A

      C strings cannot be compared for equality using any function

    2. B

      The == operator automatically sorts strings instead of comparing them

    3. C

      The == operator works only when both strings have exactly one character

    4. D

      String equality requires comparing the characters, which == does not do as a text comparison

    Correct answer
    D

    String equality requires comparing the characters, which == does not do as a text comparison

    Explanation

    The lecture uses strcmp because string equality requires comparing the characters in the strings, rather than using == as for integer values.

  27. 27

    According to the described behavior of strcmp, what return value indicates that two strings are equal?

    1. A

      Any negative value

    2. B

      000

    3. C

      Any positive value

    4. D

      The length of the first string

    Correct answer
    B

    000

    Explanation

    strcmp returns 000 when the strings match; negative or positive results indicate an ordering difference.

  28. 28

    What additional information can the sign of strcmp's result provide?

    1. A

      Which string comes first in character-code order

    2. B

      The memory address where the strings are stored

    3. C

      Whether either string was created as an array

    4. D

      How many characters the strings share

    Correct answer
    A

    Which string comes first in character-code order

    Explanation

    A negative or positive result can indicate which string comes first according to character-code ordering, which can support sorting.

  29. 29

    What must a C program include to use strcmp as described?

    1. A

      No header, because strcmp is a language keyword

    2. B

      The string.h header

    3. C

      The stdlib.h header only

    4. D

      The math.h header

    Correct answer
    B

    The string.h header

    Explanation

    The program must include string.h so the compiler has the declaration for strcmp.

  30. 30

    Why can a phone number be better represented as a string than as an integer?

    1. A

      Strings automatically validate whether a phone number is callable

    2. B

      It must always be added to other phone numbers arithmetically

    3. C

      An integer can store more punctuation than a string can

    4. D

      It may contain punctuation or leading zeros that are meaningful in its written form

    Correct answer
    D

    It may contain punctuation or leading zeros that are meaningful in its written form

    Explanation

    Phone numbers can include plus signs, dashes, parentheses, or significant leading zeros, which an integer representation does not preserve as written.

  31. 31

    What is the main design risk of storing names and phone numbers in separate parallel arrays?

    1. A

      Each array is required to use a different indexing system

    2. B

      Their corresponding entries can become misaligned if the arrays' sizes or orders differ

    3. C

      The computer is unable to search either array using a loop

    4. D

      The arrays cannot contain strings when they have the same length

    Correct answer
    B

    Their corresponding entries can become misaligned if the arrays' sizes or orders differ

    Explanation

    Parallel arrays rely on matching indices and consistent ordering, so a mismatch can associate a person with the wrong number.

  32. 32

    What does a struct allow a C programmer to do in the phone-book example?

    1. A

      Make a string behave exactly like an integer

    2. B

      Sort an array without comparing any of its values

    3. C

      Access every array element simultaneously

    4. D

      Define a custom type that groups related fields, such as a name and a number

    Correct answer
    D

    Define a custom type that groups related fields, such as a name and a number

    Explanation

    A structure defines a custom data type that groups related fields together, such as each person's name and phone number.

  33. 33

    In an expression such as people[i].name, what does the dot notation do?

    1. A

      Compares the name at index i with every other name

    2. B

      Changes the array index from zero-based to one-based

    3. C

      Accesses the name field of the structure at index i

    4. D

      Creates a new array containing all names

    Correct answer
    C

    Accesses the name field of the structure at index i

    Explanation

    The dot operator accesses a particular field within a structure value.

  34. 34

    What is the central repeated action in selection sort?

    1. A

      Compare neighboring values and swap each pair that is out of order

    2. B

      Find the smallest remaining value and swap it into the next position

    3. C

      Divide the array in half and merge the two halves immediately

    4. D

      Move the largest remaining value one position to the right

    Correct answer
    B

    Find the smallest remaining value and swap it into the next position

    Explanation

    Selection sort repeatedly selects the smallest value in the unsorted portion and swaps it into the next position to be filled.

  35. 35

    After selection sort has placed the smallest remaining value at index iii, what can it conclude about earlier positions?

    1. A

      They must contain values larger than every remaining value

    2. B

      They contain values already selected in sorted order

    3. C

      They need to be searched again before the next pass

    4. D

      They are guaranteed to be in their original order

    Correct answer
    B

    They contain values already selected in sorted order

    Explanation

    Each pass places the next smallest value at the next position, so the prefix already processed is sorted.

  36. 36

    Why does selection sort swap values rather than simply insert the selected value outside the array?

    1. A

      An array cannot store a value at an index that has already been used

    2. B

      The array has fixed allocated locations, so rearranging its elements should stay within that space

    3. C

      The selected value must be copied into a new array after every comparison

    4. D

      Swapping is required because selection sort never compares values

    Correct answer
    B

    The array has fixed allocated locations, so rearranging its elements should stay within that space

    Explanation

    The demonstration treats the array as fixed-size memory; it cannot assume extra locations outside its allocated space are available.

  37. 37

    What is the number of comparisons made by the passes of selection sort, expressed as a sum for an array of nnn values?

    1. A

      log⁡2(n)+log⁡2(n−1)+⋯+log⁡2(1)\log_2(n)+\log_2(n-1)+\cdots+\log_2(1)log2​(n)+log2​(n−1)+⋯+log2​(1)

    2. B

      1+1+⋯+11+1+\cdots+11+1+⋯+1 for exactly one comparison per value

    3. C

      n+(n+1)+⋯+2nn+(n+1)+\cdots+2nn+(n+1)+⋯+2n

    4. D

      (n−1)+(n−2)+⋯+1(n-1)+(n-2)+\cdots+1(n−1)+(n−2)+⋯+1

    Correct answer
    D

    (n−1)+(n−2)+⋯+1(n-1)+(n-2)+\cdots+1(n−1)+(n−2)+⋯+1

    Explanation

    The first pass scans n−1n-1n−1 other values, the next scans n−2n-2n−2, and the process continues down to one comparison.

  38. 38

    What is the asymptotic running time of selection sort in the described analysis?

    1. A

      Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn)

    2. B

      Θ(n2)\Theta(n^2)Θ(n2)

    3. C

      Θ(n)\Theta(n)Θ(n)

    4. D

      Θ(1)\Theta(1)Θ(1)

    Correct answer
    B

    Θ(n2)\Theta(n^2)Θ(n2)

    Explanation

    Selection sort performs a quadratic number of comparisons, even when the input is already sorted, so both its upper and lower bounds are n2n^2n2.

  39. 39

    What operation is repeated in bubble sort?

    1. A

      Find the smallest value in the entire unsorted portion and move it to the front

    2. B

      Merge two sorted lists by repeatedly selecting their smallest front value

    3. C

      Compare adjacent values and swap them if they are out of order

    4. D

      Compare the middle value with the target and discard half the array

    Correct answer
    C

    Compare adjacent values and swap them if they are out of order

    Explanation

    Bubble sort scans adjacent pairs and swaps a pair when its values are out of order.

  40. 40

    If an array has indices from 000 through n−1n-1n−1, why does a bubble-sort inner loop stop at n−2n-2n−2 when it compares index iii with index i+1i+1i+1?

    1. A

      At i=n−2i=n-2i=n−2, the final array value has already been removed

    2. B

      The algorithm requires the final index to remain unchanged

    3. C

      The last comparison would always swap the two values

    4. D

      At i=n−1i=n-1i=n−1, the comparison would access the nonexistent index nnn

    Correct answer
    D

    At i=n−1i=n-1i=n−1, the comparison would access the nonexistent index nnn

    Explanation

    The right-hand index is i+1i+1i+1; allowing i=n−1i=n-1i=n−1 would make the algorithm access index nnn, beyond the array's last index.

  41. 41

    Why does basic bubble sort make repeated passes through the array?

    1. A

      One pass checks only the first array element and cannot compare any pairs

    2. B

      A pass reverses the array, so another pass is needed to restore it

    3. C

      One pass can move a largest remaining value to the end, but other values may still be unsorted

    4. D

      Each pass inserts a new value that was not previously in the array

    Correct answer
    C

    One pass can move a largest remaining value to the end, but other values may still be unsorted

    Explanation

    A pass can bubble a large value toward the end, but other elements may still be out of order, requiring further passes.

  42. 42

    What early-stop improvement gives bubble sort a best-case running time of Ω(n)\Omega(n)Ω(n)?

    1. A

      Stop after the first swap because the array is then sorted

    2. B

      Stop whenever the first adjacent pair is already in order

    3. C

      Skip all comparisons on the first pass

    4. D

      Stop after a complete pass in which no swaps were made

    Correct answer
    D

    Stop after a complete pass in which no swaps were made

    Explanation

    A pass with no swaps shows that the list is already sorted, so the algorithm can terminate after that one linear pass.

  43. 43

    What is the worst-case running time of bubble sort, even with the no-swap early-stop improvement?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(log⁡n)O(\log n)O(logn)

    3. C

      O(1)O(1)O(1)

    4. D

      O(n)O(n)O(n)

    Correct answer
    A

    O(n2)O(n^2)O(n2)

    Explanation

    The early stop helps when a pass makes no swaps, but unsorted inputs can still require many passes of adjacent comparisons, giving quadratic worst-case time.

  44. 44

    How does bubble sort's early-stop condition differ from the selection-sort procedure described?

    1. A

      It guarantees that bubble sort uses less memory than selection sort

    2. B

      It allows bubble sort to avoid comparing any adjacent values

    3. C

      It can detect an already sorted list after a pass with no swaps, while selection sort still performs its prescribed scans

    4. D

      It makes selection sort linear on every input but leaves bubble sort quadratic

    Correct answer
    C

    It can detect an already sorted list after a pass with no swaps, while selection sort still performs its prescribed scans

    Explanation

    The modified bubble sort checks whether a pass made any swaps; the described selection sort continues finding the next minimum even when the list is already ordered.

  45. 45

    What is the defining idea behind recursion in programming?

    1. A

      A function compares two values without returning from its call

    2. B

      A function calls itself to solve a smaller version of a problem

    3. C

      A function stores its input in adjacent memory locations

    4. D

      A function repeats a fixed number of statements using a loop

    Correct answer
    B

    A function calls itself to solve a smaller version of a problem

    Explanation

    A recursive function is one that calls itself, often with a smaller version of the original problem.

  46. 46

    What feature prevents a recursive algorithm from calling itself forever?

    1. A

      A loop that repeats the same problem without changing its size

    2. B

      A return statement placed only after the recursive call

    3. C

      A larger input passed to each successive call

    4. D

      A base case that ends the recursion, together with recursive calls that reduce the problem

    Correct answer
    D

    A base case that ends the recursion, together with recursive calls that reduce the problem

    Explanation

    A base case provides a stopping condition, and each recursive call must make progress toward it by reducing the problem.

  47. 47

    In recursive binary search, why does searching the left or right half make progress?

    1. A

      Each recursive call increases the number of candidate elements

    2. B

      Each recursive call repeats the search on the original full array

    3. C

      Each recursive call skips the comparison with the middle element

    4. D

      Each recursive call searches a smaller range than the previous call

    Correct answer
    D

    Each recursive call searches a smaller range than the previous call

    Explanation

    Binary search recursively examines only one side of the checked middle element, reducing the search range each time.

  48. 48

    Which description best distinguishes a base case from a recursive case?

    1. A

      A base case increases the input; a recursive case leaves the input unchanged

    2. B

      A base case always uses a loop; a recursive case always uses an array

    3. C

      A base case returns a direct answer; a recursive case calls the algorithm on a smaller problem

    4. D

      A base case sorts the whole input; a recursive case checks only one element

    Correct answer
    C

    A base case returns a direct answer; a recursive case calls the algorithm on a smaller problem

    Explanation

    A base case can be answered immediately, whereas a recursive case relies on solving a smaller instance of the problem.

  49. 49

    What is iteration in the contrast made with recursion?

    1. A

      Storing related values in a custom type

    2. B

      Using loops to solve a problem

    3. C

      Using a function that calls itself

    4. D

      Sorting data by comparing adjacent values

    Correct answer
    B

    Using loops to solve a problem

    Explanation

    Iteration refers to solving a problem with loops, while recursion uses functions that call themselves.

  50. 50

    How does the recursive description of a pyramid of height nnn express its structure?

    1. A

      Two pyramids of height nnn placed side by side

    2. B

      A single row repeated exactly once

    3. C

      A pyramid of height n−1n-1n−1 plus one additional row

    4. D

      A pyramid of height n+1n+1n+1 minus its top row

    Correct answer
    C

    A pyramid of height n−1n-1n−1 plus one additional row

    Explanation

    The pyramid is defined as a smaller pyramid of height n−1n-1n−1 with one more row added.

  51. 51

    In the recursive pyramid implementation, why is a condition such as n≤0n\leq 0n≤0 useful?

    1. A

      It ensures that every call draws at least one additional row

    2. B

      It makes the recursive calls use a larger height each time

    3. C

      It sorts the rows before they are printed

    4. D

      It stops drawing when there are no rows left and prevents further recursive calls

    Correct answer
    D

    It stops drawing when there are no rows left and prevents further recursive calls

    Explanation

    When the height reaches zero or less, there is nothing to draw, so returning provides the base case that ends recursion.

  52. 52

    What memory trade-off can arise from a deeply recursive implementation compared with an iterative one?

    1. A

      Recursion always uses less memory because it avoids loops

    2. B

      Iteration requires a separate copy of the input for every loop pass

    3. C

      Recursive calls never use memory once a base case is present

    4. D

      Each function call uses additional memory, and sufficiently many calls can exceed available memory

    Correct answer
    D

    Each function call uses additional memory, and sufficiently many calls can exceed available memory

    Explanation

    Each recursive call requires memory for its call state; deep recursion can use too much memory, unlike the corresponding loop-based approach described.

  53. 53

    What is the base case stated for merge sort?

    1. A

      A list with two values must always be merged with an empty list

    2. B

      A list with one value is already sorted

    3. C

      A list with no values must be divided into two equal halves

    4. D

      A list with nnn values is sorted when its first value is smallest

    Correct answer
    B

    A list with one value is already sorted

    Explanation

    A single value is already sorted, so merge sort can stop dividing when it reaches a list of size one.

  54. 54

    When merging two already sorted lists, how can the next smallest value be chosen?

    1. A

      Choose values randomly until both lists are empty

    2. B

      Take the final value from each list and choose the larger one

    3. C

      Re-sort both lists from the beginning after every value is moved

    4. D

      Compare the first unmerged value in each list and take the smaller one

    Correct answer
    D

    Compare the first unmerged value in each list and take the smaller one

    Explanation

    Because each list is sorted, its first unmerged value is its smallest remaining value; comparing the two fronts identifies the next value for the merged list.

  55. 55

    Why does merging two sorted lists with a total of nnn values take O(n)O(n)O(n) time?

    1. A

      The number of comparisons depends only on the logarithm of each list's length

    2. B

      Each value is considered and moved into the merged order once

    3. C

      The algorithm repeatedly sorts the same two lists from scratch

    4. D

      Every value must be compared with every other value

    Correct answer
    B

    Each value is considered and moved into the merged order once

    Explanation

    The merge advances through the sorted lists, processing each of the nnn values once, so its work is linear in the total number of values.

  56. 56

    Which sequence of steps describes merge sort?

    1. A

      Search the middle, discard one half, then return the matching index

    2. B

      Sort the left half, sort the right half, then merge the sorted halves

    3. C

      Find the smallest value, swap it to the front, then repeat on the rest

    4. D

      Compare adjacent values, swap them, then stop after one pass

    Correct answer
    B

    Sort the left half, sort the right half, then merge the sorted halves

    Explanation

    Merge sort recursively sorts each half of the input and then merges those sorted halves.

  57. 57

    Why does merge sort have O(nlog⁡n)O(n\log n)O(nlogn) running time in the described analysis?

    1. A

      It performs constant work for each value across nnn levels

    2. B

      It performs quadratic comparisons but skips half of them on each pass

    3. C

      It examines only log⁡2n\log_2 nlog2​n values and ignores the rest

    4. D

      It performs linear merging work across about log⁡2n\log_2 nlog2​n levels of division

    Correct answer
    D

    It performs linear merging work across about log⁡2n\log_2 nlog2​n levels of division

    Explanation

    The input is divided in half for about log⁡2n\log_2 nlog2​n levels, and each level involves a total of O(n)O(n)O(n) merging work.

  58. 58

    What asymptotic classification is given for merge sort's lower and upper bounds in this version?

    1. A

      Its upper bound is nlog⁡nn\log nnlogn and its lower bound is 111

    2. B

      Both are nnn, so its running time is Θ(n)\Theta(n)Θ(n)

    3. C

      Both are nlog⁡nn\log nnlogn, so its running time is Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn)

    4. D

      Its upper bound is n2n^2n2 and its lower bound is nnn

    Correct answer
    C

    Both are nlog⁡nn\log nnlogn, so its running time is Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn)

    Explanation

    The described merge-sort algorithm has matching O(nlog⁡n)O(n\log n)O(nlogn) and Ω(nlog⁡n)\Omega(n\log n)Ω(nlogn) bounds, which yields Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn).

  59. 59

    What space requirement is identified as a trade-off for merge sort?

    1. A

      It needs space proportional only to the number of recursive levels

    2. B

      It requires no storage beyond the original array

    3. C

      It requires an additional copy for every comparison, regardless of input size

    4. D

      It needs extra space to hold and merge sorted portions of the input

    Correct answer
    D

    It needs extra space to hold and merge sorted portions of the input

    Explanation

    The merge demonstration uses extra space to hold the sorted portions while combining them; the lecture characterizes this as requiring twice as much space.

Question 1 of 59
Question 1 of 59

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent