The push_swap project is part of the 42 school curriculum, aiming to sort a stack of integers using a limited set of operations while minimizing the number of steps.
The program generates a sequence of instructions that, when applied to the input stack, result in it being sorted in ascending order.
Project Description
The push_swap program takes a list of integers as arguments and outputs a sequence of instructions required to sort them.
Two stacks are used: a (containing the input data) and b (initially empty).
Available operations include:
sa, sb, ss – swap the first two elements at the top of a stack
pa, pb – push the top element from one stack to the other
ra, rb, rr – rotate a stack upwards
rra, rrb, rrr – rotate a stack downwards
The goal is to sort stack a with the fewest possible operations.
Compilation and Execution
To compile the project, use the command:
make
Then run the push_swap program with a list of integers as arguments:
./push_swap 3 2 1 6 5 4
To verify the correctness using checker, use:
./push_swap 3 2 1 | ./checker 3 2 1
The push_swap project provides an excellent introduction to sorting algorithms, data structures (stacks), and code optimization for performance.
It teaches algorithmic complexity analysis and efficient memory management in the C programming language.
Projekt push_swap to zadanie realizowane w ramach programu szkoły 42, którego celem jest posortowanie stosu liczb całkowitych przy użyciu ograniczonego zestawu operacji, minimalizując liczbę wykonanych kroków.
Program generuje sekwencję instrukcji, które po zastosowaniu na stosie wejściowym prowadzą do jego posortowania w porządku rosnącym.
Opis projektu
Program push_swap przyjmuje jako argumenty listę liczb całkowitych i wyświetla na standardowym wyjściu sekwencję instrukcji niezbędnych do ich posortowania.
Do dyspozycji są dwa stosy: a (zawierający dane wejściowe) oraz b (początkowo pusty).
Dostępne operacje to:
sa, sb, ss – zamiana miejscami dwóch pierwszych elementów stosu
pa, pb – przeniesienie elementu z jednego stosu na drugi
ra, rb, rr – rotacja stosu w górę
rra, rrb, rrr – rotacja stosu w dół
Celem jest uzyskanie posortowanego stosu a przy możliwie najmniejszej liczbie operacji.
Kompilacja i uruchomienie
Aby skompilować projekt, użyj polecenia:
make
Następnie uruchom program push_swap z listą liczb jako argumentami:
./push_swap 3 2 1 6 5 4
Aby sprawdzić poprawność działania za pomocą checker, użyj:
./push_swap 3 2 1 | ./checker 3 2 1
Projekt push_swap stanowi doskonałe wprowadzenie do algorytmów sortowania, struktur danych (stosów) oraz optymalizacji kodu pod kątem wydajności.
Uczy analizy złożoności algorytmicznej oraz efektywnego zarządzania pamięcią w języku C.