A sorting algorithm project developed as part of the curriculum at 42 Heilbronn. This program sorts a stack of integers using a limited set of operations, with the goal of minimizing the number of moves required.
The push_swap project challenges students to implement an efficient sorting algorithm using only two stacks and a restricted set of operations. The program must take a list of integers as input and output a series of operations that will sort the stack in ascending order with the smallest number at the top.
This project deepens understanding of algorithm complexity, data structures, and optimization techniques. Different sorting strategies are employed based on the input size, from hardcoded solutions for small sets to binary radix sort for larger inputs.
The program is organized into several categories of functions:
The following operations can be performed on the stacks:
sa- swap a: swaps the first two elements at the top of stack asb- swap b: swaps the first two elements at the top of stack bss- swap both: performs sa and sb simultaneouslypa- push a: takes the top element from stack b and puts it on top of stack apb- push b: takes the top element from stack a and puts it on top of stack bra- rotate a: shifts all elements of stack a up by one (first becomes last)rb- rotate b: shifts all elements of stack b up by onerr- rotate both: performs ra and rb simultaneouslyrra- reverse rotate a: shifts all elements of stack a down by one (last becomes first)rrb- reverse rotate b: shifts all elements of stack b down by onerrr- reverse rotate both: performs rra and rrb simultaneously
Functions for validating and parsing input:
parse_input- converts command line arguments to integer array- Validates for duplicates and non-numeric input
- Handles both space-separated and individual arguments
- Error checking for integer overflow
The program uses different sorting strategies based on input size:
sort_two- swaps two elements if neededsort_three- hardcoded optimal solution for 3 elementssort_small- sorts 4-5 elements by isolating minimum values
binary_radix_sort- efficient sorting using binary representation- Processes numbers bit by bit from least to most significant
- Uses both stacks to partition numbers based on bit values
- Optimal for larger datasets with O(n * log n) complexity
Utility functions supporting the sorting operations:
index_array- converts values to their sorted indicesis_sorted- checks if stack is already sortedfind_min_position- locates the minimum value in the stackbubble_sort- helper for creating sorted reference arrayinit_stacks- initializes stack data structuresfill_stack- populates stack with parsed values
The program uses a Makefile with the following targets:
make # Compiles the program
make clean # Removes object files
make fclean # Removes object files and the executable
make re # Recompiles the entire programThe compilation produces an executable file push_swap that can be run with integer arguments.
To use the program:
- Clone the repository and compile:
git clone https://github.com/Lizard31/push_swap.git
cd push_swap
make- Run the program with a list of integers:
./push_swap 3 2 1The program will output the operations needed to sort the stack:
sa
rra
Sort three numbers:
./push_swap 2 1 3Sort with space-separated arguments:
./push_swap "4 67 3 87 23"Sort a larger set:
./push_swap 5 8 2 9 1 7 3 6 4You can verify that the operations correctly sort the stack by using a checker program. The checker is typically provided separately as part of the 42 project resources:
ARG="4 67 3 87 23"; ./push_swap $ARG | ./checker $ARGNote: The checker program must be obtained separately and is not included in this repository.
The push_swap implementation uses different strategies based on input size:
Simply swap if they are in the wrong order.
Use hardcoded comparisons to determine the optimal sequence of moves (maximum 2 operations).
Push the smallest values to stack b, sort the remaining 3 elements, then push back from stack b.
Implement binary radix sort:
- Convert all numbers to their indices in the sorted array (0 to n-1)
- For each bit position (from least to most significant):
- Push numbers with 0 at current bit position to stack b
- Rotate numbers with 1 at current bit position in stack a
- Push all numbers back from stack b to stack a
- Repeat until all bits are processed
This approach guarantees efficient sorting with a predictable number of operations.
- All code follows the 42 Norm coding standards
- The program is written in C and compiles with
-Wall -Wextra -Werrorflags - Memory management is handled carefully to prevent leaks
- Error handling outputs "Error\n" to stderr for invalid input
- The program handles edge cases including:
- Already sorted input (no operations needed)
- Duplicate values (error)
- Non-numeric input (error)
- Integer overflow (error)
- Empty input (no output, clean exit)