π§ My fifth project in 42 Common Core β good god give me patience π¦Ή Β΄ α― ` π¦Ή
push_swap is a sorting algorithm project with a twist:
- You have two stacks:
A
(initial) andB
(helper). - Your goal is to sort
A
using a limited set of operations. - You're graded by how few operations you use β optimization is π.
Operation | Description |
---|---|
sa/sb |
Swap top two elements of A or B |
ss |
sa and sb at the same time |
pa/pb |
Push top element between stacks |
ra/rb |
Rotate: top β bottom |
rr |
ra and rb together |
rra/rrb |
Reverse rotate: bottom β top |
rrr |
rra and rrb together |
- π» Sorting 100 numbers: β€ 700 operations
- π» Sorting 500 numbers: β€ 5500 operations
- β No standard sort functions allowed
- β Must follow 42βs Norm coding style
typedef struct s_node {
int value;
struct s_node *next;
} t_node;
typedef struct s_stack {
t_node *top;
int size;
} t_stack;
Each stack is a singly linked list, allowing efficient rotation and push operations.
sa
,sb
,ss
(swap operations)pa
,pb
(push)ra
,rb
,rr
(rotate)rra
,rrb
,rrr
(reverse rotate)
- Greedy sorting
- Chunk-based push strategy
- Radix sort fallback
- Split stack A into
chunks
- Push elements from A to B using optimal moves
- Rebuild A by pushing back in sorted order
void split_and_push_chunks(t_stack *a, t_stack *b, int chunks);
void optimized_chunk_push_b(t_stack *a, t_stack *b, int min, int max);
void push_back_to_a(t_stack *a, t_stack *b);
void radix_sort(t_stack *a, t_stack *b, int chunks);
Function | Purpose |
---|---|
find_target_index() |
Finds index of number in chunk range |
greedy_rotate_a() |
Minimizes rotations to bring number to top |
optimized_chunk_push_b |
Pushes number to B using best rotation path |
push_back_to_a() |
Rebuilds sorted A by pushing back max values |
radix_sort() |
Efficient final sorting by binary bits |
int main() {
t_stack *a = init_stack();
t_stack *b = init_stack();
push(a, 3); push(a, 2); push(a, 1);
print_stack(a, 'A');
sa(a);
pb(a, b); pb(a, b);
pa(a, b);
return 0;
}
Stack Size | Suggested Method | Goal Moves |
---|---|---|
β€ 3 | Manual sort (sa, ra) | ~2β3 |
β€ 5 | Selective push | ~6β12 |
~100 | Chunk sort (5β7) | β€ 700 |
~500 | Radix or 11 chunks | β€ 5500 |
ARG="3 2 1"; ./push_swap $ARG | ./checker_linux $ARG
- β "OK" if sorted correctly
- β "KO" if not
ARG="3 2 1"; ./push_swap $ARG | wc -l
total=0
for i in {1..50}; do
ARG=$(seq 1 500 | sort -R | tr '\n' ' ');
moves=$(./push_swap $ARG | wc -l);
total=$((total + moves));
echo $moves;
done
echo "Average: $((total / 50))"
checker_linux
orchecker_mac
is a verifier program provided in the project. Pipe your move list into it to check if your output is valid.
- πΊ YouTube: Push_swap Explained
- π Radix Sort Explanation
- π§ͺ push_swap_tester
- π§ͺ Push_swap Visualizer
This project will:
- Teach you low-level optimization techniques
- Stretch your understanding of sorting complexity
- Make you cry a little (or a lot) π
But itβs worth it.
βMake it work, then make it fast, then make it elegant.β
π§ π» Keep calm and rotate on! β¬β«
```