Swaps [mp]
Micro pattern¶
Description¶
Exchange two positions or semantic states in constant time to restore an invariant.
Problems¶
Comparison sorting¶
Exchange elements to place them in appropriate regions.
- Bubble sort (DSA)
- Quick sort (DSA)
- Heap sort (DSA)
- Selection sort (DSA)
Reversal and rotation¶
Swap symmetric positions while moving inward.
- 48. Rotate Image (Top Easy)
- 189. Rotate Array (Top Easy)
- 344. Reverse String (Top Easy)
In-place partitioning¶
Swap values into their target regions or natural positions.
- 75. Sort Colors (Top Medium)
- 41. First Missing Positive (Top Hard)
- 283. Move Zeroes (Top Easy)
Permutation generation¶
Swap remaining values into positions and undo swaps after exploration.
- 46. Permutations (Top Medium)
- 384. Shuffle an Array (Top Easy)
Constant-time unordered deletion¶
Replace a removed array slot with its final element.
- 380. Insert Delete GetRandom O(1) (Top Medium)
Swap semantic extremes¶
Exchange minimum and maximum summaries when their roles reverse.
- 152. Maximum Product Subarray (Top Hard)