> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/core-curriculum/rank-02/push-swap.md).

# Push Swap

## Push Swap

### Overview

The **Push Swap** project is part of the 42 School curriculum and challenges students to sort integers using two stacks (A and B) with a limited set of operations. The goal is to design an efficient algorithm that sorts the stack in the minimum number of moves, demonstrating algorithmic thinking and optimization skills.

### Table of Contents

* Overview
* Objective
* Allowed Operations
* Project Structure
* Algorithm Strategies
* Testing and Evaluation
* Resources
* Author

### Objective

The main objective is to sort the integers in **stack A** in ascending order while minimizing the number of operations required. You must handle various edge cases, including:

* Already sorted stacks.
* Reverse-sorted stacks.
* Stacks with only a few elements (e.g., 3 or 5).

### Allowed Operations

| Command | Description                                                                                      |
| ------- | ------------------------------------------------------------------------------------------------ |
| `sa`    | Swap the first two elements of stack A.                                                          |
| `sb`    | Swap the first two elements of stack B.                                                          |
| `ss`    | Perform `sa` and `sb` simultaneously.                                                            |
| `pa`    | Push the top element of stack B onto stack A.                                                    |
| `pb`    | Push the top element of stack A onto stack B.                                                    |
| `ra`    | Rotate stack A (shift all elements up, moving the first element to the last position).           |
| `rb`    | Rotate stack B.                                                                                  |
| `rr`    | Perform `ra` and `rb` simultaneously.                                                            |
| `rra`   | Reverse rotate stack A (shift all elements down, moving the last element to the first position). |
| `rrb`   | Reverse rotate stack B.                                                                          |
| `rrr`   | Perform `rra` and `rrb` simultaneously.                                                          |

### Project Structure

* **Input Handling**: Validates input to ensure all values are integers and that there are no duplicates.
* **Sorting Logic**: Implements the sorting algorithm using only the allowed operations.
* **Output**: Outputs the sequence of operations required to sort the stack.
* **Error Handling**: Gracefully manages invalid inputs.

### Algorithm Strategies

* **Small Stacks**: Use hardcoded logic for optimal sorting with minimal moves (e.g., for stacks with 3 or 5 elements).
* **Larger Stacks**: For large stacks (e.g., 100 or 500 elements), implemented in my code **Radix Sort.**
  * Process each bit of the binary representation.
  * Push or rotate elements based on the current bit.
  * Reassemble the sorted stack.

### Testing and Evaluation

* **Checker Program**: Use the provided checker program to verify the output:

  ```bash
  ./push_swap [list_of_integers] | ./checker [list_of_integers]
  ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/core-curriculum/rank-02/push-swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
