1. Home
  2. Explore
  3. Computer Science
  4. Memory and Pointers

Study methods

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

Memory and Pointers

  • 53Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Array Search

Why is an array a useful way to model computer memory?

53 questions

  1. 1

    Why is an array a useful way to model computer memory?

    1. A

      It lets the computer inspect every stored value simultaneously

    2. B

      It guarantees that the values are stored in sorted order

    3. C

      It organizes memory into addressable positions that can hold data

    4. D

      It makes each value accessible without using its position

    Correct answer
    C

    It organizes memory into addressable positions that can hold data

    Explanation

    Memory can be viewed as bytes arranged in positions, and arrays use indexed positions to organize data. A computer still accesses array elements step by step rather than taking in the entire array at once.

  2. 2

    What is the output of the locker-search problem described in the lecture?

    1. A

      The index of the largest value in the lockers

    2. B

      A sorted copy of all values in the lockers

    3. C

      A Boolean indicating whether the target value is present

    4. D

      The number of lockers examined before stopping

    Correct answer
    C

    A Boolean indicating whether the target value is present

    Explanation

    The search problem asks whether the target number appears among the lockers, so its output is true or false.

  3. 3

    When the values are unsorted and there is no additional information, why might linear search need to inspect every element?

    1. A

      The computer must sort the array before it can compare any values

    2. B

      Linear search checks only the first and last elements, so it must repeat those checks

    3. C

      The target could be the last element or absent, so earlier checks cannot rule out later positions

    4. D

      An array requires every element to be inspected before any comparison is allowed

    Correct answer
    C

    The target could be the last element or absent, so earlier checks cannot rule out later positions

    Explanation

    In the worst case, the target is at the end or does not occur at all. With no ordering information, each element may need to be checked before concluding whether the target is present.

  4. 4

    What should a linear search return if it reaches the end of the array without finding the target?

    1. A

      True, because the search completed successfully

    2. B

      The index of the final element, because it was checked last

    3. C

      The middle value, because no matching value was found

    4. D

      False, because every element was checked without a match

    Correct answer
    D

    False, because every element was checked without a match

    Explanation

    If the loop checks every array element and finds no match, the target is not present, so the Boolean result should be false.

  5. 5

    What condition makes binary search applicable to an array?

    1. A

      The array must contain an even number of elements

    2. B

      The elements must be sorted so comparisons can eliminate one side

    3. C

      The elements must be stored as strings rather than numbers

    4. D

      The target must be larger than the middle element

    Correct answer
    B

    The elements must be sorted so comparisons can eliminate one side

    Explanation

    Binary search relies on sorted order: comparing the target with the middle element indicates which half could contain it.

  6. 6

    After binary search compares the target with the middle value, how does it choose which half to search?

    1. A

      It always searches the half with more elements

    2. B

      It searches both halves unless the middle value is the target

    3. C

      It searches left if the target is smaller and right if the target is larger

    4. D

      It searches left if the target is larger and right if the target is smaller

    Correct answer
    C

    It searches left if the target is smaller and right if the target is larger

    Explanation

    In a sorted array, values to the left of the middle are smaller, and values to the right are larger. The comparison therefore identifies which half can contain the target.

  7. 7

    What should a binary search do when the remaining search interval contains no elements?

    1. A

      Continue dividing the empty interval into smaller intervals

    2. B

      Return true because the interval has been fully processed

    3. C

      Return false because no remaining position can contain the target

    4. D

      Restart the search from the beginning of the original array

    Correct answer
    C

    Return false because no remaining position can contain the target

    Explanation

    An empty search interval means every potentially relevant position has been eliminated without a match, so the target is absent.

  8. 8

    Why can binary search be much faster than linear search on a large sorted array?

    1. A

      It moves the target value to the middle before searching

    2. B

      It checks the array from both ends and meets in the middle

    3. C

      Each comparison can discard about half of the remaining candidates

    4. D

      It compares every value simultaneously using array indexing

    Correct answer
    C

    Each comparison can discard about half of the remaining candidates

    Explanation

    Binary search repeatedly halves the remaining search space, requiring a number of steps that grows logarithmically rather than linearly with the array size.

  9. 9

    What does big OOO notation describe in the lecture’s treatment of algorithm efficiency?

    1. A

      The lower bound on the running time for the luckiest input

    2. B

      An upper bound on how an algorithm’s running time grows

    3. C

      The amount of memory used, regardless of the algorithm’s steps

    4. D

      The exact number of seconds an algorithm takes on one computer

    Correct answer
    B

    An upper bound on how an algorithm’s running time grows

    Explanation

    Big OOO is used as an approximate upper bound on running time, commonly associated with how the algorithm behaves in the worst case.

  10. 10

    Why are O(n)O(n)O(n) and O(n/2)O(n/2)O(n/2) treated as the same order of growth?

    1. A

      Both expressions describe algorithms that always take exactly nnn steps

    2. B

      The number of elements is assumed to be two for every input

    3. C

      Big OOO measures only the best case, where the two costs are equal

    4. D

      Big OOO ignores constant factors because they matter less as input size grows

    Correct answer
    D

    Big OOO ignores constant factors because they matter less as input size grows

    Explanation

    Big OOO focuses on how growth changes with input size and disregards constant multipliers such as dividing the number of steps by two.

  11. 11

    Why can logarithms with different fixed bases be treated as the same order in big OOO analysis?

    1. A

      A logarithm’s base changes only the algorithm’s best-case time

    2. B

      Big OOO ignores logarithmic terms altogether

    3. C

      Changing the base changes the logarithm only by a constant factor

    4. D

      Different bases produce identical numerical values for every input

    Correct answer
    C

    Changing the base changes the logarithm only by a constant factor

    Explanation

    Logarithms with different fixed bases differ by a constant multiplier, and big OOO ignores constant factors.

  12. 12

    What does \Omega notation describe in the lecture’s terminology?

    1. A

      The exact average time across all possible inputs

    2. B

      The number of different values an algorithm can return

    3. C

      A lower bound on an algorithm’s running time

    4. D

      An upper bound on an algorithm’s memory use

    Correct answer
    C

    A lower bound on an algorithm’s running time

    Explanation

    Omega is used to describe a lower bound on running time, often illustrated through an algorithm’s best-case behavior.

  13. 13

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

    1. A

      \Omega(1), because one comparison can find it

    2. B

      \Omega(\log n), because the search space is halved

    3. C

      \Omega(n^2), because the array must be repeatedly scanned

    4. D

      \Omega(n), because every element must be compared

    Correct answer
    A

    \Omega(1), because one comparison can find it

    Explanation

    If the target is the first element checked, linear search finds it after one comparison, giving a constant best-case lower bound.

  14. 14

    Why can counting every item in a list have both O(n)O(n)O(n) and \Omega(n) running time?

    1. A

      Every item must be inspected to produce an accurate count, even in the best case

    2. B

      The count is known from the array’s first value in the worst case

    3. C

      Counting stops as soon as the first item is found, but may inspect all items

    4. D

      The algorithm divides the list in half until the count is determined

    Correct answer
    A

    Every item must be inspected to produce an accurate count, even in the best case

    Explanation

    An accurate count requires examining all nnn items regardless of their values, so both the upper and lower bounds are linear.

  15. 15

    Which running-time case does the lecture emphasize more when evaluating an algorithm’s performance?

    1. A

      The worst case, because it describes an upper bound on the work required

    2. B

      The best case, because it describes the most favorable possible input

    3. C

      The case with the smallest input, because it is easiest to measure

    4. D

      The case in which the input is already sorted, regardless of the algorithm

    Correct answer
    A

    The worst case, because it describes an upper bound on the work required

    Explanation

    The lecture notes that computer scientists typically care more about an algorithm’s upper bound and worst-case performance than about an unusually favorable best case.

  16. 16

    What does \Theta notation indicate in the lecture?

    1. A

      The upper and lower bounds have the same order of growth

    2. B

      The running time is known exactly in seconds

    3. C

      The algorithm uses the same amount of memory for every input

    4. D

      The algorithm’s best case is faster than its worst case

    Correct answer
    A

    The upper and lower bounds have the same order of growth

    Explanation

    Theta is a shorthand used when an algorithm’s upper and lower bounds are identical in order of growth.

  17. 17

    Why can a better search algorithm fail to make an overall task faster if it requires sorting first?

    1. A

      Binary search requires the array to become unsorted again

    2. B

      Sorting guarantees that the target will be the first value

    3. C

      The cost of sorting may outweigh the time saved by later searches

    4. D

      Sorting makes the values unsuitable for comparison

    Correct answer
    C

    The cost of sorting may outweigh the time saved by later searches

    Explanation

    Sorting has a cost of its own. Whether sorting followed by faster searches is worthwhile depends on factors such as how often the sorted data will be searched.

  18. 18

    Why should a phone number generally be represented as a string rather than an integer?

    1. A

      It may contain symbols or leading zeros that are meaningful as written

    2. B

      A string automatically sorts phone numbers numerically

    3. C

      It must support arithmetic operations such as addition and multiplication

    4. D

      Integer types cannot store any sequence of decimal digits

    Correct answer
    A

    It may contain symbols or leading zeros that are meaningful as written

    Explanation

    Phone numbers may include dashes, parentheses, plus signs, or leading zeros. These are formatting characters or meaningful parts of the identifier, not quantities for arithmetic.

  19. 19

    Why does comparing two strings with == not perform the intended character-by-character equality test in C?

    1. A

      A string is represented as an array of characters rather than a single primitive value

    2. B

      C does not allow arrays to contain alphabetic characters

    3. C

      The == operator compares only the first character of every integer

    4. D

      Strings in C are always stored as floating-point values

    Correct answer
    A

    A string is represented as an array of characters rather than a single primitive value

    Explanation

    The lecture explains that a string is an array of characters, so comparing strings requires comparing their contents rather than treating each whole string like a simple scalar value.

  20. 20

    What does the C function strcmp return when its two input strings are identical?

    1. A

      One

    2. B

      Zero

    3. C

      A negative number

    4. D

      A positive number

    Correct answer
    B

    Zero

    Explanation

    strcmp returns zero when the strings match; a positive or negative result indicates alphabetical ordering when they differ.

  21. 21

    What header is needed to use strcmp in the program shown?

    1. A

      string.h

    2. B

      stdio.h

    3. C

      cs50.h

    4. D

      stdlib.h

    Correct answer
    A

    string.h

    Explanation

    The lecture identifies string.h as the header that declares strcmp.

  22. 22

    In the phone-book example, why must the program return immediately after printing a matching person’s number?

    1. A

      A successful match requires the program to return a negative value

    2. B

      Returning immediately sorts the names alphabetically

    3. C

      Otherwise, it may continue and execute the final not-found behavior

    4. D

      The number can only be printed after the array is fully scanned

    Correct answer
    C

    Otherwise, it may continue and execute the final not-found behavior

    Explanation

    Returning immediately ends the search after a match. If execution instead continues to the end, the program could also print the not-found message despite having found the person.

  23. 23

    In the program convention described, what return value indicates that main completed successfully?

    1. A

      -1

    2. B

      The index of the matching array element

    3. C

      1

    4. D

      0

    Correct answer
    D

    0

    Explanation

    The lecture presents zero as the conventional successful return value from main, with one used to indicate failure in the examples.

  24. 24

    What design risk arises from storing names and phone numbers in separate arrays at matching indexes?

    1. A

      A phone number at a given index must be a different data type from its name

    2. B

      The arrays can become misaligned, associating a name with the wrong number

    3. C

      The arrays cannot be searched using a loop

    4. D

      The compiler automatically sorts one array but not the other

    Correct answer
    B

    The arrays can become misaligned, associating a name with the wrong number

    Explanation

    Separate arrays rely on keeping corresponding entries at the same index. Changes such as sorting one array without the other can break that relationship.

  25. 25

    What does a C struct provide in the person example?

    1. A

      A special string type that converts phone numbers into integers

    2. B

      A built-in method for sorting all names alphabetically

    3. C

      A container that groups related fields, such as a name and phone number

    4. D

      A loop that searches multiple arrays at the same time

    Correct answer
    C

    A container that groups related fields, such as a name and phone number

    Explanation

    A structure groups multiple data fields together, allowing a person’s name and phone number to be stored as related information.

  26. 26

    What is the purpose of typedef in the custom person type example?

    1. A

      It defines a type name so the program can declare values of that type

    2. B

      It converts a structure into a sorted array

    3. C

      It automatically fills every structure field with user input

    4. D

      It compares two structures and returns whether they match

    Correct answer
    A

    It defines a type name so the program can declare values of that type

    Explanation

    The lecture introduces typedef as a way to define a type, enabling the program to declare an array whose elements are persons.

  27. 27

    In an array of person structures, what does people[i].name refer to?

    1. A

      The number field of the person stored at index i

    2. B

      The name field of the person stored at index i

    3. C

      The name of the array’s data type

    4. D

      The ith character in the name of the first person

    Correct answer
    B

    The name field of the person stored at index i

    Explanation

    The index selects a person in the array, and the dot notation selects that person’s name field.

  28. 28

    How does grouping a person’s name and number in one structure improve the phone-book design?

    1. A

      It makes the array automatically expand whenever a person is added

    2. B

      It keeps related values together when people are rearranged or sorted

    3. C

      It removes the need to search through the phone book

    4. D

      It ensures that every phone number can be used in arithmetic

    Correct answer
    B

    It keeps related values together when people are rearranged or sorted

    Explanation

    Keeping related fields together avoids relying on separate arrays remaining in matching order, making it safer to rearrange records.

  29. 29

    What is the central operation of bubble sort?

    1. A

      Divide the array into halves and merge the sorted halves

    2. B

      Repeatedly choose the smallest remaining element and move it into place

    3. C

      Compare neighboring elements and swap them when they are out of order

    4. D

      Compare the target only with the middle element of the array

    Correct answer
    C

    Compare neighboring elements and swap them when they are out of order

    Explanation

    Bubble sort examines adjacent elements and swaps an out-of-order pair. Repeated passes move larger values toward the right.

  30. 30

    Why is bubble sort named for the behavior demonstrated in the lecture?

    1. A

      The array is divided into bubbles that are sorted independently

    2. B

      The algorithm compares each value with every other value simultaneously

    3. C

      The algorithm places the smallest value in the center of the array

    4. D

      Larger values move or “bubble” toward the right side over repeated passes

    Correct answer
    D

    Larger values move or “bubble” toward the right side over repeated passes

    Explanation

    During repeated adjacent comparisons and swaps, larger values gradually move toward their correct positions on the right.

  31. 31

    What is the worst-case running-time order of the basic bubble sort described?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(nlog⁡n)O(n\log n)O(nlogn)

    Correct answer
    C

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

    Explanation

    The described version uses an outer pass and an inner sequence of adjacent comparisons, each on the order of nnn, leading to O(n2)O(n^2)O(n2) work.

  32. 32

    Why does the basic bubble sort have a best-case running time of \Omega(n^2) in the lecture’s initial pseudocode?

    1. A

      It performs its prescribed passes even when the input is already sorted

    2. B

      It uses binary search to find every element’s position

    3. C

      It must compare each pair of elements only once

    4. D

      It cannot detect whether two adjacent elements are out of order

    Correct answer
    A

    It performs its prescribed passes even when the input is already sorted

    Explanation

    The initial pseudocode blindly repeats the passes and does not stop early, so it still performs quadratic work on an already sorted list.

  33. 33

    How can bubble sort improve its best-case running time?

    1. A

      Sort the left half before comparing adjacent elements

    2. B

      Stop when a full pass makes no swaps

    3. C

      Stop after finding the smallest element once

    4. D

      Skip every comparison involving the first element

    Correct answer
    B

    Stop when a full pass makes no swaps

    Explanation

    If an entire pass makes no swaps, the list is already sorted. Stopping then allows bubble sort to finish after a single pass in the best case.

  34. 34

    What is the best-case running-time order of bubble sort when it stops after a pass with no swaps?

    1. A

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

    2. B

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

    3. C

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

    4. D

      Ω(nlog⁡n)\Omega(n\log n)Ω(nlogn)

    Correct answer
    A

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

    Explanation

    Even when the input is already sorted, the algorithm must compare neighboring elements in one pass, requiring on the order of nnn comparisons.

  35. 35

    What is the central operation of selection sort?

    1. A

      Find the smallest remaining element and place it at the next position

    2. B

      Divide the array repeatedly until each part has one element

    3. C

      Search for a target by comparing it with the middle element

    4. D

      Swap every adjacent pair that is out of order

    Correct answer
    A

    Find the smallest remaining element and place it at the next position

    Explanation

    Selection sort repeatedly selects the smallest element from the unsorted portion and places it at the next position in the sorted portion.

  36. 36

    Why does selection sort inspect the entire remaining unsorted portion before selecting its next element?

    1. A

      It has to move every value one position to the left before each selection

    2. B

      It must confirm that the array contains an even number of elements

    3. C

      It needs to compare each value with the first value only

    4. D

      It cannot know which value is smallest without checking the remaining candidates

    Correct answer
    D

    It cannot know which value is smallest without checking the remaining candidates

    Explanation

    A computer cannot infer that the first value it sees is the smallest; it must inspect all remaining candidates to identify the minimum.

  37. 37

    Why might selection sort swap the smallest value with the value currently occupying its destination rather than shift every intervening value?

    1. A

      A swap guarantees that all remaining values are already sorted

    2. B

      Shifting is not allowed when an array has a fixed size

    3. C

      A swap can place the minimum without moving many other elements individually

    4. D

      Shifting would make the minimum value impossible to compare

    Correct answer
    C

    A swap can place the minimum without moving many other elements individually

    Explanation

    Swapping the minimum with the destination value avoids shifting many elements. The displaced value can be handled during later passes.

  38. 38

    What is the worst-case running-time order of selection sort?

    1. A

      O(nlog⁡n)O(n\log n)O(nlogn)

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    C

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

    Explanation

    Selection sort scans the remaining elements repeatedly. The total work is proportional to n+(n−1)+⋯+1n+(n-1)+\cdots+1n+(n−1)+⋯+1, which grows quadratically.

  39. 39

    Why does selection sort still have a quadratic lower bound when the input is already sorted?

    1. A

      It still scans the remaining values to confirm the next smallest element

    2. B

      It is unable to recognize a sorted list because it compares strings

    3. C

      It restarts the algorithm from the beginning after each selection

    4. D

      It must swap every neighboring pair on every pass

    Correct answer
    A

    It still scans the remaining values to confirm the next smallest element

    Explanation

    Even if the input is already sorted, selection sort still performs its repeated searches for the smallest remaining value, so its work remains quadratic.

  40. 40

    Which distinction between bubble sort and selection sort is accurate?

    1. A

      Bubble sort chooses the next minimum; selection sort repeatedly swaps adjacent pairs

    2. B

      Bubble sort needs sorted input; selection sort works only on unsorted input

    3. C

      Bubble sort divides the list in half; selection sort merges sorted halves

    4. D

      Bubble sort fixes adjacent inversions; selection sort repeatedly chooses the next minimum

    Correct answer
    D

    Bubble sort fixes adjacent inversions; selection sort repeatedly chooses the next minimum

    Explanation

    Bubble sort operates on neighboring pairs, while selection sort searches the remaining unsorted portion for its smallest element on each pass.

  41. 41

    What is iteration in programming, as described in the lecture?

    1. A

      Repeating instructions using a loop

    2. B

      Combining two sorted lists into one

    3. C

      Grouping multiple fields into a structure

    4. D

      A function calling itself on a smaller input

    Correct answer
    A

    Repeating instructions using a loop

    Explanation

    Iteration refers to repeating work, commonly by using a loop.

  42. 42

    What makes a function recursive?

    1. A

      It contains two nested loops

    2. B

      It processes an array in alphabetical order

    3. C

      It returns a Boolean value to its caller

    4. D

      It calls itself as part of its own execution

    Correct answer
    D

    It calls itself as part of its own execution

    Explanation

    A function is recursive when it references and calls itself in its own code.

  43. 43

    Why must a recursive function have a base case?

    1. A

      It ensures that every possible input produces the same result

    2. B

      It makes each recursive call receive a larger problem

    3. C

      It guarantees that the function uses a loop instead of a function call

    4. D

      It provides a stopping condition that prevents endless recursive calls

    Correct answer
    D

    It provides a stopping condition that prevents endless recursive calls

    Explanation

    A base case identifies a situation that can be handled directly without another recursive call, preventing recursion from continuing indefinitely.

  44. 44

    How does the recursive Mario-pyramid function construct a pyramid of height hhh?

    1. A

      Print all rows with one nested loop and then call the function again

    2. B

      Print the row of height hhh first, then draw a pyramid of height h+1h+1h+1

    3. C

      Divide the height into two equal pyramids and merge them

    4. D

      Draw a pyramid of height h−1h-1h−1, then print the additional row

    Correct answer
    D

    Draw a pyramid of height h−1h-1h−1, then print the additional row

    Explanation

    The recursive definition treats a pyramid of height hhh as a pyramid of height h−1h-1h−1 followed by one additional row of hhh bricks.

  45. 45

    In the recursive pyramid example, what should happen when the height reaches zero?

    1. A

      Call the function with height negative one

    2. B

      Return without drawing anything

    3. C

      Restart the pyramid at its original height

    4. D

      Print one more row of bricks

    Correct answer
    B

    Return without drawing anything

    Explanation

    Height zero is the base case: there is nothing to draw, so the function returns rather than calling itself again.

  46. 46

    Why does the recursive pyramid code print the smallest row before the larger rows?

    1. A

      The base case prints the entire pyramid at once

    2. B

      The function sorts the rows by their number of bricks

    3. C

      Recursive calls reach the base case first, then execution returns through earlier calls

    4. D

      The loop always counts down from the original height

    Correct answer
    C

    Recursive calls reach the base case first, then execution returns through earlier calls

    Explanation

    Calls descend to smaller heights until reaching the base case. As those calls return, each earlier call prints its row, producing rows in increasing height.

  47. 47

    What are the three main steps of merge sort for a list with more than one item?

    1. A

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

    2. B

      Find the minimum, swap it into place, and repeat

    3. C

      Compare adjacent values, swap them, and repeat until no swaps occur

    4. D

      Search the middle, discard one half, and return the target’s index

    Correct answer
    A

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

    Explanation

    Merge sort recursively sorts the two halves and then merges those sorted halves into one sorted list.

  48. 48

    What is the base case for merge sort?

    1. A

      A list with one item is already sorted, so return it

    2. B

      A list with no items must be merged with the original list

    3. C

      A list that contains the largest possible value must be returned

    4. D

      A list with two items is always already sorted

    Correct answer
    A

    A list with one item is already sorted, so return it

    Explanation

    A one-item list is sorted by definition, so merge sort can return it without doing further work.

  49. 49

    When merging two sorted lists, which value should be placed next in the output?

    1. A

      The value that appeared first in the original input

    2. B

      The middle value of the original unsorted list

    3. C

      The smaller of the first unmerged values in the two lists

    4. D

      The larger of the last values in the two lists

    Correct answer
    C

    The smaller of the first unmerged values in the two lists

    Explanation

    Because both input lists are sorted, their first unmerged values are their respective smallest remaining values. Choosing the smaller one maintains sorted order.

  50. 50

    Why does merge sort require additional space in the merge step described?

    1. A

      It converts each number into a string before comparing it

    2. B

      It stores a separate copy of every recursive function call as an array

    3. C

      It needs room to combine elements from the two sorted halves in order

    4. D

      It must preserve the original unsorted list while searching for a target

    Correct answer
    C

    It needs room to combine elements from the two sorted halves in order

    Explanation

    The example uses extra space to place elements from the two sorted halves into their correctly merged order.

  51. 51

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

    1. A

      It scans all nnn elements once and then performs n2n^2n2 swaps

    2. B

      Each of about log⁡n\log nlogn levels processes all nnn elements during merging

    3. C

      It compares only log⁡n\log nlogn elements and makes nnn recursive calls

    4. D

      It divides the list into nnn parts and merges them in constant time

    Correct answer
    B

    Each of about log⁡n\log nlogn levels processes all nnn elements during merging

    Explanation

    The list can be divided in half for about log⁡n\log nlogn levels. At each level, merging processes a total of about nnn elements, yielding nlog⁡nn\log nnlogn work.

  52. 52

    What is the best-case running-time order of the merge sort described, even if its input is already sorted?

    1. A

      Ω(nlog⁡n)\Omega(n\log n)Ω(nlogn)

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

    Ω(nlog⁡n)\Omega(n\log n)Ω(nlogn)

    Explanation

    The described merge sort does not stop early for already sorted input; it still divides the list and merges at each level, so its best-case order is nlog⁡nn\log nnlogn.

  53. 53

    How can the running-time bounds of merge sort be summarized using theta notation?

    1. A

      Θ(n2)\Theta(n^2)Θ(n2), because every sorting algorithm compares every pair

    2. B

      Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn), because its upper and lower bounds have the same order

    3. C

      Θ(n)\Theta(n)Θ(n), because each merge processes the list once

    4. D

      Θ(log⁡n)\Theta(\log n)Θ(logn), because the list is repeatedly divided in half

    Correct answer
    B

    Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn), because its upper and lower bounds have the same order

    Explanation

    The lecture gives merge sort both an O(nlog⁡n)O(n\log n)O(nlogn) upper bound and an Ω(nlog⁡n)\Omega(n\log n)Ω(nlogn) lower bound, allowing the concise description Θ(nlog⁡n)\Theta(n\log n)Θ(nlogn).

Question 1 of 53
Question 1 of 53

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent