Comparison-Based Sorting
- 61Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
61 questions
- 1A 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.
- 2If 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.
- 3A 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.
- 4If 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.
- 5An 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.
- 6Insertion 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.
- 7When 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.
- 8Insertion 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.
- 9Insertion 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.
- 10Selection 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.
- 11After 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.
- 12Selection 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.
- 13Selection 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.
- 14Bubble 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.
- 15Bubble 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.
- 16In 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.
- 17Bubble 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.
- 18Merge 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.
- 19Merge 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.
- 20After 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.
- 21When 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.
- 22Because 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)
Explanation
At each merge level, each item is processed as the sorted portions are combined, so a merge takes O(N) time.
- 23Merge 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.
- 24The 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.
- 25The time complexity of merge sort is_____.
Hint
It has logarithmically many levels, with linear work at each level.
Answer
O(NlogN)
Explanation
Merge sort has logN levels of recursion and each level performs O(N) merging work, giving O(NlogN) time.
- 26Merge 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.
- 27A 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.
- 28After 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.
- 29Heapsort 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.
- 30Heapsort 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.
- 31Heapsort extracts the maximum N times, with each extraction taking 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(NlogN)
Explanation
There are N maximum extractions, each requiring O(logN) work, giving worst-case time O(NlogN).
- 32Quicksort 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.
- 33After 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.
- 34Quicksort 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.
- 35Quicksort 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.
- 36During 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.
- 37Quicksort 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.
- 38Quicksort 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.
- 39Quicksort has worst-case time 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) worst-case time.
- 40The average time complexity of quicksort is_____.
Hint
On average, pivots divide the work more effectively than in the worst case.
Answer
O(NlogN)
Explanation
Although quicksort has a quadratic worst case, its average behavior is O(NlogN).
- 41With randomly selected pivots, quicksort is often practically faster than other algorithms in the 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).
- 42For 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) complexity.
- 43For 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.
- 44For 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.
- 45For 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.
- 46Quicksort 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.
- 47A 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.
- 48Before 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.
- 49Each 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.
- 50A 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.
- 51For 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.
- 52For N distinct values, the decision tree must have N! leaves because there are N! possible_____.
Hint
Each leaf must account for one possible arrangement of the input values.
Answer
permutations
Explanation
There are N! ways to arrange N distinct values, so a sorting decision tree must distinguish among N! possible permutations.
- 53Because 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.
- 54The required height of the decision tree is at least on the order of log(N!), since the tree must distinguish among_____leaves.
Hint
The number of leaves equals the number of possible input permutations.
Answer
N!
Explanation
Distinguishing N! possible permutations with binary comparisons requires a tree whose height is at least log(N!).
- 55Expanding the factorial inside the logarithm gives log(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)⋯1, the logarithm of the product is the sum of the logarithms of its factors.
- 56The partial sum from logN down to log(N/2) contains about_____terms.
Hint
Count the integers in the upper half of the range.
Answer
N/2
Explanation
There are about N/2 terms between N and N/2, and these terms provide a lower-bound estimate.
- 57Using those roughly N/2 terms, each at least 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
2Nlog(2N)
Explanation
The selected terms are each at least log(N/2), and there are about N/2 of them, yielding 2Nlog(2N).
- 58The decision-tree argument proves that every comparison-based sorting algorithm requires at least Ω(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 Ω(NlogN) time.
- 59The Ω(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.
- 60Sorting 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 Ω(NlogN) result applies to comparison-based sorts; algorithms that do not use comparisons may belong to another category and can potentially do better.
- 61Among the comparison-based algorithms discussed, the efficient methods provide 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(NlogN) behavior, but do not combine stability and in-place operation.

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