The push_swap project is part of the School 42 curriculum, designed to enhance your understanding of sorting algorithms and stack data structures. The goal of this project is to sort a list of integers using a set of predefined operations on two stacks, with the objective of doing so using the fewest possible moves.
The push_swap program takes a list of integers as input and sorts them using a series of operations on two stacks, A and B. The program outputs the sequence of operations needed to sort the list.
- Language: The project is implemented in C.
- Data Structures:
- Stack A: Initially holds the input integers.
- Stack B: Used as a temporary stack for sorting operations.
- Operations:
- sa: Swap the first two elements at the top of stack
A. - sb: Swap the first two elements at the top of stack
B. - ss:
saandsbat the same time. - pa: Push the top element from stack
Bto stackA. - pb: Push the top element from stack
Ato stackB. - ra: Rotate all elements of stack
Aup by one. - rb: Rotate all elements of stack
Bup by one. - rr:
raandrbat the same time. - rra: Reverse rotate all elements of stack
Adown by one. - rrb: Reverse rotate all elements of stack
Bdown by one. - rrr:
rraandrrbat the same time.
- sa: Swap the first two elements at the top of stack
- Headers:
#include <stdlib.h>: For memory management.#include <unistd.h>: For standard input/output.#include <stdio.h>: For debugging and auxiliary output.#include <limits.h>: For integer limit constants.
-
Parsing Input:
- Reads the list of integers from command line arguments.
- Validates input to ensure all elements are integers and within the allowed range.
- Initializes stack
Awith the input integers.
-
Sorting Algorithm:
- Implements a series of sorting algorithms optimized for fewest moves, such as:
- Simple sorting for small sets of integers (e.g., up to 5 elements).
- More complex algorithms like Quicksort or Radix sort for larger sets.
- Determines the optimal sequence of operations to sort the list.
- Implements a series of sorting algorithms optimized for fewest moves, such as:
-
Executing Operations:
- Executes the sequence of operations on stacks
AandB. - Outputs each operation to standard output.
- Executes the sequence of operations on stacks
-
Optimizing Moves:
- Applies techniques to minimize the number of moves.
- Ensures the sequence is efficient and adheres to the project's constraints.
To run the push_swap program, compile the project using the provided Makefile and then execute the program with a list of integers as arguments:
make
./push_swap 3 2 5 1 4The program will output a sequence of operations, for example:
pb
pb
sa
ra
pa
pa
This project provides a practical exercise in implementing and optimizing sorting algorithms, as well as managing and manipulating stack data structures.