8000 GitHub - kitearuba/push_swap: πŸ“Š Sorting algorithm challenge using stacks and limited operations. Optimized C code to sort integers with the fewest possible moves. Logic meets efficiency.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

πŸ“Š Sorting algorithm challenge using stacks and limited operations. Optimized C code to sort integers with the fewest possible moves. Logic meets efficiency.

Notifications You must be signed in to change notification settings

kitearuba/push_swap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

89 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


🧩 Push_Swap - Sorting with Stacks in C 🧩

Push_Swap C Programming Makefile 42 Network

Welcome to Push_Swap, a modular and efficient sorting program designed to solve a fundamental algorithmic challenge: sorting integers using two stacks (a and b) with a limited set of operations. This project is part of the 42 Network curriculum and is designed to test your understanding of algorithms, data structures, and optimization.


πŸ“‘ Table of Contents

  1. Introduction
  2. Project Structure
  3. Features
  4. Error Handling and Validation
  5. Bonus Functionality
  6. Installation
  7. Usage
  8. Makefile Specifics
  9. Acknowledgements
  10. Author

πŸ“– Introduction

The Push_Swap project aims to implement efficient sorting algorithms with minimal operations. Using stacks (a and b), you must sort a list of integers by performing operations such as:

  • sa: Swap the first two elements of a stack.
  • ra: Rotate a stack (top element moves to the bottom).
  • pb: Push the top element from one stack to the other.

The bonus part introduces a checker program that validates the correctness of sorting operations.


πŸ“‚ Project Structure

.
β”œβ”€β”€ include/             # Header files
β”‚   β”œβ”€β”€ push_swap.h      # Core declarations
β”‚   β”œβ”€β”€ stack.h          # Stack-specific structures and operations
β”œβ”€β”€ src/                 # Main source code
β”‚   β”œβ”€β”€ core/            # Core logic for Push_Swap
β”‚   β”œβ”€β”€ commands/        # Stack manipulation functions
β”‚   β”œβ”€β”€ sorting/         # Sorting algorithms
β”‚   β”œβ”€β”€ validation/      # Input parsing and validation
β”‚   β”œβ”€β”€ utils/           # Utility functions
β”‚   └── checker/         # Bonus checker implementation
β”œβ”€β”€ libft/               # Custom library (libft) for common operations
β”œβ”€β”€ tests/               # Test scripts and sample test cases
β”œβ”€β”€ Makefile             # Build automation
└── README.md            # Project documentation

🌟 Features

Modular Design

  • Clear separation of core logic, commands, sorting algorithms, validation, and utility functions.
  • Reusable codebase for both mandatory and bonus parts.

Dynamic Input Handling

  • Supports both array-based and linked-list-based stacks for flexibility and performance.

Optimized Sorting Algorithms

  • Small Inputs: Handles 3–5 numbers with hardcoded sorting.
  • Large Inputs: Uses chunk-based or radix sorting to optimize moves.

πŸ›‘οΈ Error Handling and Validation

Comprehensive Input Validation

The program ensures that inputs meet the following criteria:

  • No duplicates.
  • Valid integer representation (e.g., 004 is interpreted as 4).
  • Handles invalid input such as -- or ++.

Resource Cleanup

  • Frees all allocated memory, including stacks and auxiliary data structures, in case of errors.

Bonus Validation

  • The bonus checker program reads operations from standard input and ensures:
    • The stack is sorted.
    • All operations are valid.

βž• Bonus Functionality

Checker Program

  • Verifies the correctness of sorting operations.
  • Reads operations like sa, pb, etc., from standard input and applies them to the stacks.

Key Features

  • Uses get_next_line to handle multi-line input.
  • Outputs:
    • OK if the operations result in a sorted stack.
    • KO if the stack is not sorted or operations are invalid.

βš™οΈ Installation

Prerequisites

  • GCC: The GNU Compiler Collection.
  • Make: Build automation tool.

Steps

  1. Clone the repository:
    git clone https://github.com/kitearuba/push_swap.git
  2. Navigate to the project directory:
    cd push_swap
  3. Compile the project:
    make

πŸš€ Usage

Sorting with Push_Swap

To sort a sequence of integers, run:

./push_swap <list_of_numbers>

Example:

./push_swap 4 3 2 1

Output:

pb
pb
ra
pa
pa

Validating with Checker (Bonus)

Pipe the output of Push_Swap to the Checker:

./push_swap 4 3 2 1 | ./checker 4 3 2 1

Expected Output:

  • OK: Stack is sorted.
  • KO: Stack is not sorted or invalid operations.

πŸ› οΈ Makefile Specifics

Relinking Prevention

The Makefile:

  • Avoids unnecessary relinking by checking timestamps of all dependencies.
  • Rebuilds the project only when necessary, ensuring faster builds.

Targets

  • make: Builds the Push_Swap program and the Checker.
  • make clean: Removes object files.
  • make fclean: Removes object files and executables.
  • make re: Rebuilds the entire project.

πŸ™Œ Acknowledgements

  • 42 Network: For designing this challenging and rewarding project.
  • Open-source contributors and the community for sharing insights and tips.

πŸ‘¨β€πŸ’» Author


About

πŸ“Š Sorting algorithm challenge using stacks and limited operations. Optimized C code to sort integers with the fewest possible moves. Logic meets efficiency.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0