An algorithmic engine in pure C solving a constrained dual-stack permutation problem using a restricted 11-instruction set (
The problem requires transforming an arbitrary permutation of
flowchart LR
subgraph Phase 1: Chunk Partitioning
A1["Unsorted Stack A"] -->|Dynamic Range Filtering| B1["Pre-Sorted Buckets in Stack B"]
end
subgraph Phase 2: Greedy Recomposition
B1 -->|Min-Cost Rotation Path (rb vs rrb)| A2["Fully Sorted Stack A"]
end
Raw integer values are first transformed into continuous indices
- Partitions Stack
$A$ into dynamic sliding windows of size$W \approx \sqrt{N} \times \alpha$ . - For each element with normalized index
$i$ :- If
$i \le \text{counter}$ : Push to$B$ (pb) and rotate$B$ (rb) to push smaller values to the bottom. - If
$\text{counter} < i \le \text{counter} + W$ : Push to$B$ (pb) without rotation. - If
$i > \text{counter} + W$ : Rotate$A$ (ra) to evaluate adjacent elements.
- If
- Result: Concentrates maximum elements toward the top and minimums toward the bottom of Stack
$B$ .
- Scans Stack
$B$ from largest to smallest index. - Computes minimum rotation cost vectors between forward circular shift (
rb) and reverse circular shift (rrb). - Atomically pops the maximum element back to Stack
$A$ (pa) until Stack$B$ is fully depleted.
| Set Size | 42 5/5 Target Limit | Engine Performance | Result |
|---|---|---|---|
| 3 Integers |
|
|
Validated |
| 5 Integers |
|
|
Validated |
| 100 Integers |
|
|
Max Score (5/5) |
| 500 Integers |
|
|
Max Score (5/5) |
# Build optimized binary
make
# Clean object files
make clean
# Full purge
make fclean# Generate random sequence and count sorting operations
ARG="4 67 3 87 23 1 90 2"
./push_swap $ARG | wc -l
# Verify sorting correctness with checker
./push_swap $ARG | ./checker_linux $ARGRubén D. Higuita — Systems & Embedded Software Engineer
Madrid, Spain • LinkedIn • GitHub • Portfolio
Engineering Invariant:
Deterministic sorting complexity, optimal operation economy, zero reachable memory leaks.