Strategy Overview
Push Swap uses different sorting strategies based on the size of the input:- 2 elements: Single swap operation
- 3 elements: Specialized
sort_three()algorithm - 4+ elements: Advanced
sort_big_stacks()algorithm using cost analysis
Strategy Selection
The main sorting function determines which strategy to use (push_swap.c:59-70):Sorting Three Elements
For exactly 3 elements, Push Swap uses a specialized algorithm that handles all possible permutations efficiently.Algorithm
Location: push_swap.c:15-42Case Analysis
The algorithm handles all 6 possible permutations of 3 elements:Case 1: [3, 2, 1] - Reverse Sorted
Case 1: [3, 2, 1] - Reverse Sorted
Condition:
val1 > val2 && val2 > val3Operations: sa → rraSteps:- Swap first two: [2, 3, 1]
- Reverse rotate: [1, 2, 3]
Case 2: [3, 1, 2] - Max at Top
Case 2: [3, 1, 2] - Max at Top
Condition:
val1 > val2 && val2 < val3 && val1 > val3Operations: raSteps:- Rotate: [1, 2, 3]
Case 3: [2, 1, 3] - Small Swap
Case 3: [2, 1, 3] - Small Swap
Condition:
val1 > val3 && val2 < val3 (simplified)Operations: raNote: This case overlaps with Case 2 logicCost: 1 operationCase 4: [2, 3, 1] - Min at Bottom
Case 4: [2, 3, 1] - Min at Bottom
Condition:
val1 < val2 && val2 > val3 && val1 > val3Operations: rraSteps:- Reverse rotate: [1, 2, 3]
Case 5: [1, 3, 2] - Middle Swap
Case 5: [1, 3, 2] - Middle Swap
Condition:
val1 < val2 && val2 > val3 && val1 < val3Operations: sa → raSteps:- Swap first two: [3, 1, 2]
- Rotate: [1, 2, 3]
Case 6: [3, 2, 1] (alternate) - Simple Swap
Case 6: [3, 2, 1] (alternate) - Simple Swap
Condition:
val1 > val2 && val2 < val3 && val1 < val3Operations: saSteps:- Swap first two: [1, 2, 3]
Optimality
Thesort_three() algorithm is optimal - it never uses more than 2 operations for any 3-element permutation, which is the theoretical minimum for some cases.
Sorting Large Stacks
For 4 or more elements, Push Swap uses the Turk Algorithm - a sophisticated approach based on cost analysis.Algorithm Overview
Location: sort_big_stacks.c:53-75Phase 1: Move A to B
The algorithm first moves all elements except 3 from Stack A to Stack B, using cost analysis to determine the optimal order.Initial Pushes
- The first 2 elements are pushed to B without cost analysis
- This provides initial elements in B for target node calculation
- Optimization: Skip if stack becomes sorted
Cost-Based Movement
Location: sort_big_stacks.c:37-51- Find the cheapest node to move (lowest push_cost)
- Optimize by rotating both stacks simultaneously if possible
- Position the node at the top of Stack A
- Position its target at the top of Stack B
- Push to Stack B
Phase 2: Sort Remaining Three
Once only 3 elements remain in Stack A:Phase 3: Move B to A
Move all elements from Stack B back to Stack A in sorted order. Location: sort_big_stacks.c:15-19init_nodes_b()calculates target positions in A- Position target node at top of A
- Push from B to A
Phase 4: Final Rotation
Rotation Optimizations
Rotate Both (rr)
Location: sort_big_stacks.c:21-27Reverse Rotate Both (rrr)
Location: sort_big_stacks.c:29-35Prep for Push
Location: stack_utils.c:43-62- If node is above median: Use forward rotation (ra/rb)
- If node is below median: Use reverse rotation (rra/rrb)
Performance Characteristics
| Input Size | Strategy | Typical Operations | Worst Case |
|---|---|---|---|
| 2 elements | Single swap | 1 | 1 |
| 3 elements | sort_three() | 1-2 | 2 |
| 5 elements | sort_big_stacks() | ~7-12 | ~12 |
| 100 elements | sort_big_stacks() | ~500-700 | ~700 |
| 500 elements | sort_big_stacks() | ~5000-5500 | ~5500 |
The algorithm’s efficiency comes from always choosing the cheapest move and optimizing rotations when both stacks can rotate in the same direction.