Skip to content

Sorting algorithm project that sorts integer stacks using minimal operations through optimized algorithms and data structures.

Notifications You must be signed in to change notification settings

shmohamm06/push_swap

Repository files navigation

push_swap

Overview

push_swap is a sorting algorithm project that sorts a stack of integers using a limited set of operations. The goal is to sort the stack using the minimum number of operations possible, demonstrating algorithm optimization and efficient data structure manipulation.

Features

  • Stack Operations: Implements push, swap, and rotate operations on stacks
  • Sorting Algorithms: Multiple sorting strategies for different stack sizes
  • Optimization: Minimizes the number of operations required
  • Visualization: Can display the sorting process step by step
  • Performance: Efficient algorithms for various input sizes

Allowed Operations

  • sa - Swap the first two elements of stack A
  • sb - Swap the first two elements of stack B
  • ss - Execute sa and sb simultaneously
  • pa - Push the first element from stack B to stack A
  • pb - Push the first element from stack A to stack B
  • ra - Rotate stack A (first element becomes last)
  • rb - Rotate stack B (first element becomes last)
  • rr - Execute ra and rb simultaneously
  • rra - Reverse rotate stack A (last element becomes first)
  • rrb - Reverse rotate stack B (last element becomes first)
  • rrr - Execute rra and rrb simultaneously

Project Structure

push_swap/
├── push_swap.h              # Header file with function declarations
├── push_swap.c              # Main program logic
├── operations.c             # Stack operation implementations
├── sorting.c                # Sorting algorithm implementations
├── utils.c                  # Utility functions
├── Makefile                 # Build configuration
└── .git/                   # Git repository

Algorithm Strategy

The project typically uses different sorting strategies based on stack size:

  • Small stacks (≤ 3 elements): Simple swap-based sorting
  • Medium stacks (4-5 elements): Optimized insertion sort
  • Large stacks (> 5 elements): Divide and conquer with multiple stacks

Usage

# Compile the program
make

# Sort a list of numbers
./push_swap 2 1 3 6 5 8

# Check if the solution is correct
./push_swap 2 1 3 6 5 8 | ./checker 2 1 3 6 5 8

# Count the number of operations
./push_swap 2 1 3 6 5 8 | wc -l

Requirements

  • GCC compiler
  • Make utility
  • Standard C libraries
  • Libft library (dependency)

Dependencies

This project depends on the Libft library for basic string and memory functions.

Performance Goals

  • 3 numbers: ≤ 3 operations
  • 5 numbers: ≤ 12 operations
  • 100 numbers: ≤ 700 operations
  • 500 numbers: ≤ 5500 operations

Notes

  • Follows 42 coding style (norminette)
  • Implements efficient stack data structures
  • Uses optimized sorting algorithms
  • Handles edge cases and error conditions
  • Memory-safe implementation

Author

shmohamm - 42 Abu Dhabi

About

Sorting algorithm project that sorts integer stacks using minimal operations through optimized algorithms and data structures.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published