1. Home
  2. Explore
  3. Computer Science
  4. Comparison-Based Sorting

Study methods

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

Comparison-Based Sorting

  • 61Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Sorting Goals
A comparison-based sorting problem begins with an array of
items whose values can be compared consistently.

61 questions

  1. 1
    A comparison-based sorting problem begins with an array of
    _____
    items whose values can be compared consistently.
    Hint

    This variable represents the size of the input array.

    Answer

    n

    Explanation

    The input is an array of n items, with a consistent way to compare their values.

  2. 2
    If the input is ordered by a consistent comparison, a sorting algorithm can arrange it from
    _____
    to greatest.
    Hint

    This is the direction used for the main sorting goal in the content.

    Answer

    least

    Explanation

    The target output is an array with all n items sorted from least to greatest.

  3. 3
    A sort is stable when items with equal values retain their
    _____
    order.
    Hint

    Think about the order tied items had before this sorting operation.

    Answer

    original

    Explanation

    Stable sorting preserves the original order of items that have equal values.

  4. 4
    If a spreadsheet is sorted by last name after being sorted by first name, a stable sort preserves the first-name order among people with the same
    _____
    .
    Hint

    These are the values that tie in the later sort.

    Answer

    last name

    Explanation

    A stable last-name sort does not jumble the existing first-name order of people with the same last name.

  5. 5
    An algorithm that uses only the existing array for its operations is described as operating
    _____
    .
    Hint

    This property concerns whether extra memory is needed.

    Answer

    in-place

    Explanation

    An in-place algorithm does not need extra memory for its main sorting operations.

  6. 6
    Insertion sort considers each item and compares it with items that appear
    _____
    it to find where it belongs.
    Hint

    The algorithm looks toward earlier positions in the array.

    Answer

    before

    Explanation

    For each item, insertion sort checks the items before it until it finds the correct insertion position.

  7. 7
    When insertion sort places an item, it shifts larger items slightly to the
    _____
    to make room.
    Hint

    The array is being sorted from least to greatest.

    Answer

    right

    Explanation

    Larger items are moved to the right until the current item can be inserted in its correct location.

  8. 8
    Insertion sort is stable because it can preserve the order of items with
    _____
    values.
    Hint

    Stable sorting is about how ties are handled.

    Answer

    equal

    Explanation

    Insertion sort is stable: items with equal values can retain their original order.

  9. 9
    Insertion sort can be performed in-place by shifting items rather than using
    _____
    memory.
    Hint

    The algorithm works within the array it is sorting.

    Answer

    extra

    Explanation

    Insertion sort can be done in-place because it shifts array items and does not require extra memory.

  10. 10
    Selection sort repeatedly finds the
    _____
    item among the unsorted positions and swaps it into place.
    Hint

    For ascending order, it selects the smallest remaining value.

    Answer

    minimum

    Explanation

    At each position, selection sort finds the minimum among the unsorted items and swaps it into that position.

  11. 11
    After selection sort places the first minimum, it can ignore index
    _____
    .
    Hint

    Array indexing in the example begins with the first position at zero.

    Answer

    0

    Explanation

    Once the first minimum is placed, index 0 is sorted and no longer needs to be considered.

  12. 12
    Selection sort can be made stable by shifting items in a way that preserves their
    _____
    order.
    Hint

    Stable sorting preserves how tied values were arranged before the sort.

    Answer

    original

    Explanation

    Selection sort can be made stable if items are shifted carefully to maintain tie-breaking order.

  13. 13
    Selection sort can be performed
    _____
    because it can rearrange items within the array.
    Hint

    This means the sort does not require extra memory for its operations.

    Answer

    in-place

    Explanation

    Selection sort can be implemented in-place, even though its time performance is slow.

  14. 14
    Bubble sort compares neighboring unsorted items and swaps them when they are
    _____
    .
    Hint

    A swap is needed when the pair is not arranged correctly for the target order.

    Answer

    out of order

    Explanation

    Bubble sort scans through unsorted pairs and swaps a pair when its items are out of order.

  15. 15
    Bubble sort can stop when a complete pass through the array makes no
    _____
    .
    Hint

    If a pass makes none, the array is already in order.

    Answer

    swaps

    Explanation

    A pass with no swaps shows that no adjacent items need to be reordered, so bubble sort can quit.

  16. 16
    In each iteration of bubble sort, the largest remaining items tend to move toward the array's furthest
    _____
    positions.
    Hint

    For ascending order, the largest values belong at the end.

    Answer

    right

    Explanation

    The largest items bubble up toward the furthest-out indices, which are at the right end of the array.

  17. 17
    Bubble sort is stable and can be done
    _____
    with a low memory footprint.
    Hint

    It rearranges items directly in the array.

    Answer

    in-place

    Explanation

    Bubble sort is stable and in-place, so it has a low memory footprint.

  18. 18
    Merge sort is a
    _____
    algorithm that divides an array into smaller portions and combines sorted portions.
    Hint

    The strategy solves smaller parts of a problem before combining their results.

    Answer

    divide-and-conquer

    Explanation

    Merge sort divides the array recursively, sorts the smaller portions, and merges them into a sorted result.

  19. 19
    Merge sort treats a one-item array as already
    _____
    .
    Hint

    A single item cannot be out of order with another item in that array.

    Answer

    sorted

    Explanation

    A one-item array is the base case because it cannot be out of order with any other item in that array.

  20. 20
    After recursively sorting the halves, merge sort combines their items in a step called the
    _____
    .
    Hint

    This operation brings the sorted portions back together.

    Answer

    merge

    Explanation

    Merge sort merges the recursively sorted portions into one sorted array.

  21. 21
    When merging two sorted arrays, the next item to output is the smaller of the two current
    _____
    .
    Hint

    Only the first remaining item in each sorted portion needs to be compared.

    Answer

    items

    Explanation

    The merge step compares the current first items of the two portions and selects the smaller one next.

  22. 22
    Because merge sort compares the current leading items of sorted portions rather than every item with every other item, a merge of the array takes
    _____
    time.
    Hint

    The work at a merge level grows linearly with the number of items.

    Answer

    O(N)O(N)O(N)

    Explanation

    At each merge level, each item is processed as the sorted portions are combined, so a merge takes O(N)O(N)O(N) time.

  23. 23
    Merge sort is
    _____
    because it preserves the order of equal-valued items.
    Hint

    This property concerns preserving tie-breaking order.

    Answer

    stable

    Explanation

    Merge sort is stable, so equal-valued items can retain their original relative order.

  24. 24
    The simplest, fastest-writing version of merge sort copies portions of the array, so it is generally not
    _____
    .
    Hint

    This version uses copied portions during merging.

    Answer

    in-place

    Explanation

    Merge sort is easiest to write when portions are copied and merged into a full-sized array; an in-place version is possible but more complicated.

  25. 25
    The time complexity of merge sort is
    _____
    .
    Hint

    It has logarithmically many levels, with linear work at each level.

    Answer

    O(Nlog⁡N)O(N\log N)O(NlogN)

    Explanation

    Merge sort has log⁡N\log NlogN levels of recursion and each level performs O(N)O(N)O(N) merging work, giving O(Nlog⁡N)O(N\log N)O(NlogN) time.

  26. 26
    Merge sort is easy to parallelize because different recursive calls can process different
    _____
    of the array.
    Hint

    The recursive work is divided among separate parts of the input.

    Answer

    portions

    Explanation

    Different threads or processes can handle separate portions of the array during the recursive sorting stage.

  27. 27
    A heap-based sort can use a max-heap to repeatedly extract the
    _____
    value.
    Hint

    A max-heap makes the largest value available at the top.

    Answer

    maximum

    Explanation

    In a max-heap, the maximum can be obtained and placed at the end of the array.

  28. 28
    After extracting the maximum in heapsort, the algorithm places it at the end and treats that position as no longer part of the
    _____
    .
    Hint

    The active structure shrinks as sorted items accumulate at the end.

    Answer

    heap

    Explanation

    Heapsort reduces the heap portion of the array after placing each extracted maximum in its final position.

  29. 29
    Heapsort is not stable because heaps do not guarantee preservation of the original ordering between
    _____
    items.
    Hint

    These are items with equal values.

    Answer

    tied

    Explanation

    Heap operations may change the relative order of equal-valued items, so heapsort is not stable.

  30. 30
    Heapsort can be done in-place because the heap is represented as an
    _____
    .
    Hint

    The heap operations rearrange values within this structure.

    Answer

    array

    Explanation

    A heap can be stored in the array being sorted, allowing heapsort to operate in-place.

  31. 31
    Heapsort extracts the maximum NNN times, with each extraction taking O(log⁡N)O(\log N)O(logN) time, so its worst-case performance is
    _____
    .
    Hint

    Multiply the number of extractions by the cost of maintaining the heap for each one.

    Answer

    O(Nlog⁡N)O(N\log N)O(NlogN)

    Explanation

    There are NNN maximum extractions, each requiring O(log⁡N)O(\log N)O(logN) work, giving worst-case time O(Nlog⁡N)O(N\log N)O(NlogN).

  32. 32
    Quicksort divides values around a selected value called the
    _____
    .
    Hint

    This value determines which items go to the left and right partitions.

    Answer

    pivot

    Explanation

    Quicksort chooses a pivot and partitions the other values according to whether they are lower or higher than it.

  33. 33
    After partitioning, a quicksort pivot is in its exact
    _____
    location in the sorted array.
    Hint

    No later recursive call needs to move the pivot again.

    Answer

    final

    Explanation

    Partitioning puts the pivot into its correct final location, which can then be excluded from recursive calls.

  34. 34
    Quicksort recursively sorts the values lower than the pivot on the
    _____
    and the values higher than the pivot on the right.
    Hint

    This is the side assigned to values smaller than the pivot.

    Answer

    left

    Explanation

    The left partition contains values lower than the pivot, while the right partition contains values higher than it.

  35. 35
    Quicksort often chooses its pivot at
    _____
    because the best pivot is not known in advance.
    Hint

    A randomized choice helps avoid consistently poor pivot selections.

    Answer

    random

    Explanation

    The pivot is usually chosen at random because it is not possible to know beforehand which value will split the data well.

  36. 36
    During partitioning, quicksort swaps values that are on the wrong side of the
    _____
    .
    Hint

    The partition is defined by comparing values with this selected value.

    Answer

    pivot

    Explanation

    Quicksort scans the array for values that should be on the other side of the pivot and swaps them.

  37. 37
    Quicksort is not stable because partitioning may change the relative order of items moved across the
    _____
    .
    Hint

    The reordering happens as items are separated into lower and higher groups.

    Answer

    pivot

    Explanation

    Partitioning can move equal-valued items relative to each other, so quicksort does not necessarily preserve their original order.

  38. 38
    Quicksort can be performed in-place by recursively operating on index
    _____
    of the same array.
    Hint

    The recursive calls restrict which section of the array they modify.

    Answer

    ranges

    Explanation

    An in-place quicksort can use ranges of indices and modify only items within those ranges, without a merge step.

  39. 39
    Quicksort has worst-case time O(N2)O(N^2)O(N2) when pivots repeatedly produce a partition with one empty side and one side containing nearly all the items; this can happen if the array is already sorted and the
    _____
    pivot is repeatedly chosen.
    Hint

    Choosing an endpoint value for an already sorted array creates a highly unbalanced split.

    Answer

    worst

    Explanation

    Repeatedly choosing the minimum or maximum on sorted input creates highly unbalanced partitions and leads to O(N2)O(N^2)O(N2) worst-case time.

  40. 40
    The average time complexity of quicksort is
    _____
    .
    Hint

    On average, pivots divide the work more effectively than in the worst case.

    Answer

    O(Nlog⁡N)O(N\log N)O(NlogN)

    Explanation

    Although quicksort has a quadratic worst case, its average behavior is O(Nlog⁡N)O(N\log N)O(NlogN).

  41. 41
    With randomly selected pivots, quicksort is often practically faster than other algorithms in the O(Nlog⁡N)O(N\log N)O(NlogN) category, despite its worse
    _____
    case.
    Hint

    The contrast is between typical practical speed and the most unfavorable possible behavior.

    Answer

    worst

    Explanation

    Random-pivot quicksort is often fast in practice, even though its worst-case complexity is O(N2)O(N^2)O(N2).

  42. 42
    For a few thousand items, insertion sort can be effectively
    _____
    to a human observer.
    Hint

    At this input size, its quadratic growth may still be imperceptible.

    Answer

    instant

    Explanation

    The content notes that insertion sort is functionally instant for a few thousand items, despite its O(N2)O(N^2)O(N2) complexity.

  43. 43
    For a million items, insertion sort may take hours on a typical user's computer, while merge sort remains essentially
    _____
    at sizes up to the millions.
    Hint

    The more efficient algorithm remains very fast at this scale.

    Answer

    instant

    Explanation

    The examples contrast insertion sort's poor scaling with merge sort's near-instant performance up through millions of items.

  44. 44
    For a billion items, insertion sort may take weeks on a supercomputer and
    _____
    on an average home computer.
    Hint

    The home-computer estimate is far longer than weeks.

    Answer

    years

    Explanation

    The content gives an estimate of weeks on a supercomputer and years on an average home computer for insertion sort on a billion items.

  45. 45
    For billions of items, merge sort may take minutes on a
    _____
    .
    Hint

    The stated estimate applies to an ordinary personal computer.

    Answer

    home computer

    Explanation

    The content estimates that merge sort can handle billions of items in the minutes category on a home computer.

  46. 46
    Quicksort can also be in the minutes category for sorting
    _____
    of items in the practical-performance comparison.
    Hint

    This is the largest scale discussed in the comparison.

    Answer

    billions

    Explanation

    Despite its quadratic worst case, quicksort's practical behavior with randomized pivots is described as minutes even for billions of items.

  47. 47
    A decision tree for comparison-based sorting represents each comparison outcome as a choice among possible
    _____
    .
    Hint

    The branches track which arrangements remain possible after comparisons.

    Answer

    orderings

    Explanation

    Each node represents possible orderings, and comparison results narrow down which ordering can be correct.

  48. 48
    Before any comparisons are made, the root of the decision tree contains
    _____
    possible orderings.
    Hint

    No information has yet ruled out any arrangement.

    Answer

    all

    Explanation

    With no comparisons performed, every ordering of the input values is still possible.

  49. 49
    Each comparison in the decision tree branches according to whether one value is less than another, thereby eliminating some possible
    _____
    .
    Hint

    The comparison reduces the set of arrangements consistent with the results so far.

    Answer

    orderings

    Explanation

    A comparison narrows the possible worlds by ruling out orderings inconsistent with its true or false outcome.

  50. 50
    A run of a comparison-based sorting algorithm corresponds to a root-to-
    _____
    path in its decision tree.
    Hint

    The path ends when the algorithm has identified an outcome.

    Answer

    leaf

    Explanation

    Each possible run follows a path from the root through comparison outcomes to a leaf.

  51. 51
    For three values, the example decision tree has leaves at depths of at least two and at most
    _____
    comparisons.
    Hint

    The longest paths in the example contain one more comparison than the shortest paths.

    Answer

    three

    Explanation

    The three-value example has a minimum depth of two and a maximum path length of three comparisons.

  52. 52
    For NNN distinct values, the decision tree must have N!N!N! leaves because there are N!N!N! possible
    _____
    .
    Hint

    Each leaf must account for one possible arrangement of the input values.

    Answer

    permutations

    Explanation

    There are N!N!N! ways to arrange NNN distinct values, so a sorting decision tree must distinguish among N!N!N! possible permutations.

  53. 53
    Because each comparison can distinguish between two outcomes, the number of possible orderings remaining can shrink by about a factor of
    _____
    at each step.
    Hint

    This is the branching factor of a yes-or-no comparison.

    Answer

    two

    Explanation

    A comparison has two outcomes, so it can eliminate roughly half of the orderings remaining at its parent node.

  54. 54
    The required height of the decision tree is at least on the order of log⁡(N!)\log(N!)log(N!), since the tree must distinguish among
    _____
    leaves.
    Hint

    The number of leaves equals the number of possible input permutations.

    Answer

    N!N!N!

    Explanation

    Distinguishing N!N!N! possible permutations with binary comparisons requires a tree whose height is at least log⁡(N!)\log(N!)log(N!).

  55. 55
    Expanding the factorial inside the logarithm gives log⁡(N!)=log⁡N+log⁡(N−1)+⋯+log⁡1\log(N!) = \log N + \log(N-1) + \cdots + \log 1log(N!)=logN+log(N−1)+⋯+log1 by turning a product into a sum of
    _____
    .
    Hint

    Use the logarithm rule for the logarithm of a product.

    Answer

    logarithms

    Explanation

    Since N!=N(N−1)⋯1N! = N(N-1)\cdots 1N!=N(N−1)⋯1, the logarithm of the product is the sum of the logarithms of its factors.

  56. 56
    The partial sum from log⁡N\log NlogN down to log⁡(N/2)\log(N/2)log(N/2) contains about
    _____
    terms.
    Hint

    Count the integers in the upper half of the range.

    Answer

    N/2N/2N/2

    Explanation

    There are about N/2N/2N/2 terms between NNN and N/2N/2N/2, and these terms provide a lower-bound estimate.

  57. 57
    Using those roughly N/2N/2N/2 terms, each at least log⁡(N/2)\log(N/2)log(N/2), gives a lower-bound expression of about
    _____
    .
    Hint

    Multiply the approximate number of terms by the smallest logarithm in the selected range.

    Answer

    N2log⁡(N2)\frac{N}{2}\log\left(\frac{N}{2}\right)2N​log(2N​)

    Explanation

    The selected terms are each at least log⁡(N/2)\log(N/2)log(N/2), and there are about N/2N/2N/2 of them, yielding N2log⁡(N2)\frac{N}{2}\log(\frac{N}{2})2N​log(2N​).

  58. 58
    The decision-tree argument proves that every comparison-based sorting algorithm requires at least Ω(Nlog⁡N)\Omega(N\log N)Ω(NlogN) time in the
    _____
    .
    Hint

    The lower bound concerns the minimum time any comparison sort must guarantee across its possible inputs.

    Answer

    worst case

    Explanation

    The proof establishes a lower bound on worst-case behavior: any comparison-based sort must take at least Ω(Nlog⁡N)\Omega(N\log N)Ω(NlogN) time.

  59. 59
    The Ω(Nlog⁡N)\Omega(N\log N)Ω(NlogN) lower bound applies to sorting algorithms that use
    _____
    , not necessarily to every possible sorting method.
    Hint

    The proof models its decisions as comparisons in a tree.

    Answer

    comparisons

    Explanation

    The lower-bound proof is based on a decision tree of comparisons; sorting without comparisons is not covered by this result.

  60. 60
    Sorting without comparisons may allow algorithms to do better than the comparison-based lower bound, so comparison-based sorting is not the only possible
    _____
    .
    Hint

    The content points toward a different class of sorting methods.

    Answer

    category

    Explanation

    The Ω(Nlog⁡N)\Omega(N\log N)Ω(NlogN) result applies to comparison-based sorts; algorithms that do not use comparisons may belong to another category and can potentially do better.

  61. 61
    Among the comparison-based algorithms discussed, the efficient methods provide O(Nlog⁡N)O(N\log N)O(NlogN) behavior, but the best algorithms do not combine that performance with both stability and being
    _____
    .
    Hint

    The two desirable features are stability and avoiding extra memory.

    Answer

    in-place

    Explanation

    The content notes that the best comparison-based algorithms provide O(Nlog⁡N)O(N\log N)O(NlogN) behavior, but do not combine stability and in-place operation.

1 of 61
Question 1 of 61

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent