Comparison-Based Sorting
- 50Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is the basic goal of sorting an array of n items?
Return all n items in a specified order, such as least to greatest.
Or swipe the card
50 cards
What is the basic goal of sorting an array of n items?
Return all n items in a specified order, such as least to greatest.
What must be true of the data for comparison-based sorting?
The items must have a consistent ordering that lets the algorithm compare them, such as with less-than and greater-than relations.
What does it mean for a sorting algorithm to be stable?
Items with equal values retain their original relative order in the sorted output.
Why might stability matter when sorting a spreadsheet by multiple fields?
A stable later sort preserves the ordering established by an earlier sort for items tied on the later field.
What does it mean for a sorting algorithm to be in-place?
It sorts using the existing array with little or no additional memory, rather than requiring a separate full-sized result array.
How does insertion sort place each item?
It compares the item with earlier items, shifts larger items right, and inserts the item in its correct position among the items already processed.
Is insertion sort stable and in-place, and what is its time complexity?
It is stable and in-place, but its time complexity is O(n2).
How does selection sort choose the next item for the sorted portion?
At each position, it finds the minimum in the remaining unsorted suffix, including that position, and swaps the minimum into place.
Why can selection sort stop considering earlier positions as it proceeds?
Once a minimum is placed at a position, that position is considered sorted and is excluded from later searches.
What are selection sort's in-place and stability properties?
It is in-place. Standard swapping can disrupt the order of equal items, but it can be made stable by shifting items carefully instead.
What is selection sort's time complexity?
It takes O(n2) time.
How does bubble sort process the array?
It scans adjacent unsorted pairs, swapping a pair when it is out of order, and repeats passes until no swaps are needed.
Why is bubble sort said to make large items “bubble up”?
During a pass, large items move toward the far-right positions; the largest unsorted item reaches its correct position after a pass.
How can bubble sort avoid rechecking positions already settled?
It can limit later passes to the portion before the last position swapped in the previous pass.
What are bubble sort's stability, memory, and time properties?
It is stable and in-place, with O(n2) time in the general case.
When can bubble sort perform especially well?
When the array is nearly sorted, because it can finish after a pass with no swaps.
What is the central divide-and-conquer idea in merge sort?
Recursively divide the array into smaller parts until they are single items, then merge the sorted parts back together.
Why is a one-item array a base case for merge sort?
A single item is already sorted because it cannot be out of order relative to another item in that array.
How does merge sort combine two sorted portions?
Repeatedly compare the first remaining item of each portion, move the smaller one into the output, and continue until all items are merged.
Why is merging two sorted portions an O(n) operation?
Each item is examined and moved into the merged output a limited number of times; the algorithm does not compare every item with every other item.
How does merge sort achieve stability?
During merging, it can preserve the relative order of items that have equal values.
What are merge sort's time complexity and parallelization advantage?
It runs in O(nlogn) time and is easy to parallelize because separate portions can be sorted independently.
Is merge sort usually in-place?
Its simplest implementation uses extra memory to copy portions and merge them into a result; in-place versions exist but are more complicated.
In merge sort, how would the sorted portions [27] and [16,38] be merged?
Repeatedly choose the smallest first remaining item: 16, then 27, then 38, producing [16,27,38].
How does heapsort use a max-heap to sort an array?
Repeatedly remove the heap's maximum, place it at the array's end, and shrink the portion still treated as the heap.
Why can heapsort efficiently select the next maximum?
A max-heap exposes the maximum, and maintaining the heap after removal takes O(logn) time.
What is heapsort's worst-case time complexity?
It selects the maximum n times at O(logn) per selection, giving O(nlogn) time.
Is heapsort stable or in-place?
It is in-place because the heap can be represented in the array, but it is not stable because heap operations do not preserve the original order of tied items.
How does quicksort use a pivot?
It partitions items around a pivot, placing lower values on one side and higher values on the other, then recursively sorts the partitions.
What is the key benefit of placing a pivot during quicksort partitioning?
After partitioning, the pivot is in its final sorted position and can be excluded from subsequent recursive calls.
How does quicksort commonly partition an array around a pivot?
It scans from both ends for items on the wrong sides of the pivot, swaps them, and places the pivot in its final position when the scans meet.
Why does quicksort aim for a pivot near the middle of the values?
A balanced split keeps the recursive partitions small and supports O(nlogn) behavior.
What are quicksort's average and worst-case time complexities?
Its average time is O(nlogn), while its worst-case time is O(n2).
What causes quicksort's worst-case O(n2) behavior?
Repeatedly choosing an extreme pivot, such as the minimum of an already sorted array, creates highly unbalanced partitions.
Why are quicksort pivots often chosen randomly?
The algorithm generally cannot know beforehand which value will produce a good split, so random selection reduces the chance of consistently poor choices.
Is quicksort stable or in-place?
It is generally not stable because partition swaps can change the relative order of tied items. It can be in-place by modifying index ranges in the same array.
Why can randomized quicksort be practically faster than other O(nlogn) sorting algorithms?
Despite its O(n2) worst case, randomized pivots usually produce good partitions, and quicksort is often faster in practice.
What does a comparison-based sorting lower bound describe?
The minimum worst-case time that any sorting algorithm relying only on comparisons must take.
What is a decision tree for a comparison-based sorting algorithm?
A tree in which internal nodes represent comparisons and branches represent their possible outcomes; each root-to-leaf path represents a possible execution.
Why must a comparison decision tree have at least n! leaves for sorting n distinct items?
There are n! possible input orderings, and a correct sort must distinguish all of them to determine the correct sorted result.
Why does a comparison decision tree have at least [31m\log_2(n!)[0m depth in the worst case?
Each comparison has at most two outcomes. A binary tree of height h has at most 2h leaves, so distinguishing n! orderings requires h≥log2(n!).
What does a decision tree illustrate for sorting three values?
Different comparison outcomes lead to leaves representing the possible sorted orders; the example requires at least two comparisons on some paths and can require three on others.
How can log(n!) be rewritten as a sum?
Using the product rule for logarithms, log(n!)=logn+log(n−1)+⋯+log1.
What asymptotic lower bound follows from the comparison-tree argument?
Comparison-based sorting requires at least Ω(nlogn) comparisons in the worst case.
Can a comparison-based sorting algorithm have worst-case time better than O(nlogn)?
No. The lower bound is Ω(nlogn), so comparison-based sorting cannot asymptotically beat that worst-case category.
Does the comparison-based sorting lower bound apply to sorting algorithms that do not use comparisons?
No. It applies to comparison-based algorithms; non-comparison sorting methods can fall outside this bound.
How do the described performance estimates compare for insertion sort on a few thousand items versus a million?
A few thousand items are effectively instant to sort, while a million may take hours on a typical computer, though about a second on a supercomputer.
What scale of time was described for insertion sort on a billion items?
It could take weeks on a supercomputer and years on an average home computer.
How do the described estimates compare for merge sort on millions versus billions of items?
Millions of items are essentially instant, while billions can take minutes on a home computer.
What practical performance was described for quicksort on billions of items?
With its typical average-case behavior, quicksort can sort billions of items in the minutes category.

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