Comparison-Based Sorting
- 58Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What assumptions define the comparison-based sorting problem described in the content?
58 questions
- 1
What assumptions define the comparison-based sorting problem described in the content?
Model answer
The input is an array of N items with a consistent ordering that allows items to be compared, and the goal is to output the items in sorted order.
Explanation
Comparison-based sorting determines item order by comparing values in the input.
- 2
Which output direction does the content focus on, and could the sorting goal use the opposite direction?
Model answer
It focuses on sorting from least to greatest, but the same goal could instead be defined as sorting from greatest to least.
Explanation
The direction of the requested ordering can be chosen either way.
- 3
What does it mean for a sorting algorithm to be stable?
Model answer
A stable sort preserves the original relative order of items that have equal sort keys.
Explanation
Stability retains the order of tied items, including an order established by an earlier sort.
- 4
How does stability help when sorting a spreadsheet first by first name and then by last name?
Model answer
If the last-name sort is stable, people with the same last name remain in their previous first-name order.
Explanation
The second sort does not disturb the first-name ordering among tied last names.
- 5
What does it mean for a sorting algorithm to be in-place in this content?
Model answer
It sorts by rearranging items within the input array and does not require extra memory for a separate result array.
Explanation
Shifting or swapping items in the original array can achieve an in-place sort.
- 6
Why might stability matter even when equal items have the same value for the current sort?
Model answer
Equal items may carry meaningful earlier ordering, such as a previous sort order or the order in which a user entered them, that should be preserved.
Explanation
The values may tie on the current key while differing in information represented by their prior order.
- 7
How does insertion sort place an item as it processes the array?
Model answer
It compares the item with earlier items and shifts larger items right until the item can be inserted in its correct position.
Explanation
Each item is inserted into the already processed portion of the array.
- 8
Why is insertion sort stable?
Model answer
It inserts items without changing the relative order of equal-valued items.
Explanation
Its shifting and insertion behavior can preserve the order of ties.
- 9
What is insertion sort’s additional-memory characteristic and time complexity as described?
Model answer
It is in-place and has O(N2) time complexity.
Explanation
It shifts items within the array, but its repeated comparisons and movements yield quadratic performance.
- 10
How does selection sort choose the item for each next sorted position?
Model answer
For each position, it finds the minimum among the items in the unsorted portion beginning at that position, then puts that minimum there.
Explanation
After placing each successive minimum, that position is treated as sorted.
- 11
After selection sort places the minimum at a position, how does that affect later passes?
Model answer
That position is excluded from later searches; the unsorted portion begins at the next position.
Explanation
The algorithm successively fixes the first, second, third, and later minimum values.
- 12
What are selection sort’s stability, space, and time characteristics in the content?
Model answer
It can be in-place and can be made stable by shifting items carefully, but it has O(N2) time complexity.
Explanation
A simple swap may disrupt ties, whereas careful shifting can preserve their order.
- 13
How does bubble sort process neighboring items?
Model answer
It scans unsorted neighboring pairs and swaps a pair when the items are out of order.
Explanation
Repeated passes move items toward their correct positions through adjacent swaps.
- 14
Why can bubble sort stop early?
Model answer
It can stop after a complete pass with no swaps, because that indicates the array is sorted.
Explanation
If no adjacent out-of-order pair was found, another pass is unnecessary.
- 15
What does it mean that large items bubble toward the end in bubble sort?
Model answer
During a pass, swaps move large items toward the far-right indices, and the largest item reaches its correct position after one full pass.
Explanation
The pass moves larger values along through successive neighboring comparisons.
- 16
How can bubble sort avoid rechecking some items on later passes?
Model answer
It can stop each pass at the last item swapped in the preceding pass, since items beyond that point are already in their correct positions.
Explanation
This uses the boundary established by the previous pass to reduce unnecessary comparisons.
- 17
What are bubble sort’s stability, space, and time characteristics?
Model answer
It is stable and in-place, has O(N2) time complexity, and performs best when the array is nearly sorted.
Explanation
Adjacent swaps can preserve the order of equal items, and a nearly sorted array may require few swaps and allow early stopping.
- 18
What is the basic divide-and-conquer strategy used by merge sort?
Model answer
It recursively divides the array into smaller portions, sorts those portions, and merges them into a sorted array.
Explanation
The recursive division reduces the task to small subarrays that can then be combined.
- 19
Why can a one-item array serve as a base case for merge sort?
Model answer
A one-item array is already sorted because its item cannot be out of order relative to another item within that array.
Explanation
No comparison with another item is needed to establish its sorted order.
- 20
How does merge sort handle a two-item portion?
Model answer
It orders the two items, which can be understood as merging two sorted single-item portions.
Explanation
The singletons are sorted base cases, and merging them produces an ordered pair.
- 21
How does the merge step combine two sorted arrays?
Model answer
It compares the first remaining item of each array, moves the smaller one to the result, and repeats until all items are included.
Explanation
Because each input portion is sorted, its first remaining item is its smallest remaining item.
- 22
If the sorted portions are [27,38] and [16], what sequence does merging produce?
Model answer
The merged sequence is [16,27,38].
Explanation
The smallest available items are selected in order: 16, then 27, then 38.
- 23
If the sorted portions are [12,27,38] and [16,27,39], what sequence does merging produce?
Model answer
The merged sequence is [12,16,27,27,38,39].
Explanation
The merge repeatedly selects the smallest front item; the two equal values both appear in the result.
- 24
Why is merge sort stable?
Model answer
Its merge step can preserve the relative order of items with equal keys when combining the sorted portions.
Explanation
Maintaining the order of ties during merging preserves stability throughout the sort.
- 25
Why is merge sort easiest to implement with extra memory rather than in-place?
Model answer
It is simplest to copy portions of the array and merge them into a full-sized result; an in-place version is possible but more complicated and requires more checks.
Explanation
The separate result array makes combining the sorted portions straightforward.
- 26
What are merge sort’s time complexity and parallelization characteristics?
Model answer
It runs in O(NlogN) time and is easy to parallelize by sorting different portions in separate threads or processes.
Explanation
The recursive subproblems operate on separate portions, while each level’s merging takes O(N) work.
- 27
Why does merge sort’s recursion and merging lead to O(NlogN) time?
Model answer
There are O(logN) levels of splitting and merging, and the merge work across each level is O(N).
Explanation
Multiplying the linear work per level by the logarithmic number of levels gives O(NlogN).
- 28
How does heapsort use a max-heap to sort an array?
Model answer
It repeatedly removes the maximum from the heap, places it at the end of the array, and then continues with the smaller heap portion.
Explanation
Each extracted maximum belongs in the last available position of the sorted output.
- 29
What happens to the heap region during heapsort?
Model answer
After a maximum is placed at the end, that position is no longer part of the heap, so the heap region becomes smaller.
Explanation
Subsequent heap operations apply only to the remaining unsorted portion.
- 30
Why can heapsort find each next maximum efficiently?
Model answer
The heap structure supports maximum retrieval and heap maintenance in O(logN) time.
Explanation
Maintaining the heap property avoids searching every remaining item for the maximum.
- 31
What are heapsort’s time, space, and stability characteristics in the content?
Model answer
It has worst-case O(NlogN) time, can be done in-place, and is not stable.
Explanation
It performs N maximum extractions at O(logN) each, but heap operations do not preserve the relative order of tied items.
- 32
What role does the pivot play in quicksort?
Model answer
The pivot partitions items into those lower than it and those higher than it, and quicksort recursively sorts the two partitions.
Explanation
A well-chosen pivot can divide the problem into balanced subproblems.
- 33
Why is a pivot that divides the data roughly in half desirable?
Model answer
It creates balanced recursive subproblems and supports quicksort’s O(NlogN) average behavior.
Explanation
Balanced partitions limit the number of recursive levels compared with repeatedly producing highly uneven partitions.
- 34
How is the pivot commonly selected, and why?
Model answer
It is usually selected at random because its suitability is not known until the array has been partitioned around it.
Explanation
Random selection helps avoid repeatedly choosing an unfavorable pivot based on the input’s arrangement.
- 35
How does quicksort partition items around a pivot?
Model answer
It scans from both ends for items on the wrong sides of the pivot, swaps those items, and places the pivot in the partition’s middle position.
Explanation
Partitioning groups lower values on one side and higher values on the other.
- 36
What important property does the pivot have after quicksort partitions around it?
Model answer
The pivot is in its final sorted position and can be excluded from subsequent recursive calls.
Explanation
The recursive calls need only sort the values on either side of the pivot.
- 37
Why is quicksort in-place according to the content?
Model answer
It can operate on index ranges within the original array and modify only those ranges, without a merge step that creates a separate result array.
Explanation
The recursive calls work on portions of the same array.
- 38
Why is quicksort not stable?
Model answer
Partitioning may move items across the pivot in ways that change the relative order of equal-valued items.
Explanation
The partitioning swaps do not necessarily preserve the ordering of ties.
- 39
What causes quicksort’s worst-case O(N2) behavior?
Model answer
Repeatedly choosing a worst-case pivot, such as the minimum of an already sorted array, creates one empty partition and one nearly full partition at each step.
Explanation
These highly unbalanced recursive calls lead to much more work than balanced partitions.
- 40
What are quicksort’s average time complexity and practical performance characteristics?
Model answer
Its average time complexity is O(NlogN), and with randomly selected pivots it is often faster in practice than other O(NlogN) algorithms.
Explanation
Its average performance can be strong even though its worst case is O(N2).
- 41
How does the approximate performance of insertion sort change from thousands to millions and billions of items in the examples?
Model answer
It is effectively instant for a few thousand items, may take hours for a million items on a typical home computer, and may take years for a billion items on one.
Explanation
The examples contrast these times with much faster performance on a supercomputer, illustrating the effect of quadratic growth.
- 42
How does the content characterize merge sort’s performance on very large inputs?
Model answer
Merge sort is described as effectively instant for up to millions of items and as taking minutes for billions of items on a home computer.
Explanation
Its O(NlogN) growth is substantially more favorable than the quadratic growth of insertion sort for large inputs.
- 43
How does the content characterize quicksort’s performance on billions of items?
Model answer
With its typical average behavior, quicksort is described as taking minutes even for billions of items.
Explanation
The example highlights its practical performance while its worst case remains O(N2).
- 44
Why is worst-case complexity not always enough to describe an algorithm’s practical performance?
Model answer
Worst-case complexity describes the slowest situation, but it may not represent typical behavior; quicksort, for example, averages O(NlogN) despite a worst case of O(N2).
Explanation
Both the possible worst case and the algorithm’s average or practical behavior can matter when choosing a sorting method.
- 45
What trade-offs among stability, extra memory, and speed appear in the sorting algorithms described?
Model answer
Some algorithms are stable or in-place, but the fastest comparison-based algorithms described provide O(NlogN) behavior without combining both stability and in-place operation.
Explanation
Merge sort is stable but easiest with extra memory, while in-place heapsort and quicksort are not stable.
- 46
Why does the content ask whether comparison-based sorting can be faster than O(NlogN)?
Model answer
The algorithms discussed provide O(NlogN) upper bounds, but those bounds do not establish whether a faster comparison-based sort is possible; a lower-bound argument is needed.
Explanation
An upper bound shows that an algorithm can perform within a limit, not that no algorithm can do better.
- 47
What is a decision tree in the analysis of comparison-based sorting?
Model answer
It represents comparison outcomes as branches, with each node describing the orderings still possible after the comparisons made so far.
Explanation
Each comparison narrows the set of possible correct orderings.
- 48
What does the root of a sorting decision tree represent before any comparisons are made?
Model answer
It represents all possible orderings of the input items, since none has yet been ruled out.
Explanation
With no comparison results, every permutation may be the sorted order for some input arrangement.
- 49
How does a comparison affect the possible orderings represented in a decision tree?
Model answer
Its outcome determines a branch and removes orderings inconsistent with that outcome.
Explanation
The comparison distinguishes which items must precede others, progressively narrowing the possibilities.
- 50
How does an execution of a comparison-based sorting algorithm correspond to a path in its decision tree?
Model answer
The sequence of comparison outcomes for that execution forms a root-to-leaf path.
Explanation
Each branch records the result of the next comparison made by the algorithm.
- 51
What does a leaf in a sorting decision tree represent?
Model answer
It represents a resolved ordering outcome after enough comparisons to determine the sorted order.
Explanation
The algorithm must distinguish the possible correct orderings before it can return a sorted result.
- 52
What does the decision tree for sorting three distinct values illustrate about the number of comparisons?
Model answer
Some outcomes require at least two comparisons, while the longest paths in the described tree require three comparisons.
Explanation
The tree’s path lengths show that different inputs can require different numbers of comparisons.
- 53
Why must a decision tree for comparison-sorting N distinct items have at least N! leaves?
Model answer
There are N! possible input orderings, and the algorithm must distinguish all of them to determine the correct sorted order.
Explanation
Each possible ordering requires a distinguishable outcome in the decision process.
- 54
How does the number of leaves imply a lower bound on the height of a decision tree?
Model answer
A binary decision tree of height h can have at most 2h leaves, so distinguishing N! orderings requires height at least log2(N!).
Explanation
Each comparison has two possible outcomes, limiting how many possibilities can be separated in a given number of comparisons.
- 55
How can the expression log(N!) be rewritten using the factorial expansion and logarithm rules?
Model answer
It becomes logN+log(N−1)+cdots+log1.
Explanation
Expanding N! into a product and applying the logarithm product rule turns the product into a sum.
- 56
What asymptotic lower bound follows from the decision-tree argument for comparison-based sorting?
Model answer
The worst-case time of any comparison-based sorting algorithm is at least Omega(NlogN).
Explanation
The required decision-tree height is at least log(N!), which grows on the order of NlogN.
- 57
What does the comparison-sorting lower bound imply about seeking a faster comparison-based algorithm?
Model answer
No comparison-based sorting algorithm can have worst-case performance asymptotically better than NlogN.
Explanation
The lower bound applies to the entire class of comparison-based sorts, not just the algorithms discussed.
- 58
How could a sorting algorithm potentially go beyond the comparison-based lower bound?
Model answer
It would need to sort without relying only on comparisons; the content notes that non-comparison sorting algorithms can be considered separately.
Explanation
The lower-bound proof applies specifically to algorithms whose ordering decisions are based on comparisons.

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