GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - DiegoVa6/push_swap: 42 push_swap project in C, focused on stack-based sorting, algorithm design, input parsing and operation optimization.

BranchesTags
Open more actions menu

Latest commit

 

History

30 Commits
30 Commits

Folders and files

NameName
Last commit message
Last commit date
include
include
 
 
libft
libft
 
 
src
src
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 

Repository files navigation

push_swap

push_swap is a 42 project written in C focused on sorting a stack using a limited set of operations.
The program takes a list of integers as input and prints the sequence of instructions required to sort them in ascending order, with the smallest number at the top of stack a.

The main challenge of the project is not only to sort correctly, but to do so with as few operations as possible.

Overview

This implementation combines different strategies depending on the input size:

  • dedicated sorting logic for very small cases (2, 3, and 5 numbers)
  • an indexing step to normalize values
  • a K-sort / chunk-based approach for larger stacks

The project also includes input validation, duplicate detection, error handling, and a modular organization of stack operations and sorting logic.

Allowed Operations

The program is restricted to the operations defined in the subject:

  • sa / sb / ss — swap the first two elements
  • pa / pb — push from one stack to the other
  • ra / rb / rr — rotate upwards
  • rra / rrb / rrr — reverse rotate

The output consists exclusively of these instructions, one per line.

Algorithm Approach

Small inputs

For very small stacks, the program uses dedicated hardcoded strategies:

  • 2 elements → simple swap if needed
  • 3 elements → minimal-case sorting
  • 4 or 5 elements → push the smallest values to stack b, sort the remaining stack, then push back

Large inputs

For bigger stacks, the program first indexes all values so that the smallest value becomes 0, the next becomes 1, and so on.
This simplifies comparisons and makes the sorting logic independent of the original numeric range.

After indexing, the program applies a K-sort inspired strategy:

  1. push values from stack a to stack b according to a dynamic range
  2. rotate b when appropriate to keep values positioned efficiently
  3. move values back to a in descending index order

This approach gives solid results for larger inputs such as 100 or 500 numbers.

Project Structure

.
├── include/
│   └── push_swap.h
├── libft/
├── src/
│   ├── main.c
│   ├── operations/
│   │   ├── operations_core.c
│   │   ├── operations_swap_push.c
│   │   ├── operations_rotate.c
│   │   └── operations_reverse_rotate.c
│   ├── parsing/
│   │   ├── parsing.c
│   │   └── validation.c
│   ├── sort/
│   │   ├── small_sort.c
│   │   ├── big_sort.c
│   │   ├── sort_utils.c
│   │   ├── indexing.c
│   │   └── k_sort.c
│   └── utils/
│       ├── memory_utils.c
│       └── debug.c
└── Makefile

Compilation

Compile the project with:

make

Available Makefile rules:

make
make clean
make fclean
make re

Usage

Basic example:

./push_swap 4 63 455 -35 1 9

Arguments can also be passed as quoted strings:

./push_swap "2 1 3" 6 5 8

If the input is valid, the program prints the sequence of sorting instructions. If the input is invalid, the program prints:

Error

If the input is already sorted, the program produces no output.

Validation

This project handles:

  • invalid numeric input
  • duplicates
  • integer overflow
  • already sorted input
  • multiple argument formats The program was tested with the official checker and additional manual cases.

What I Learned

Through this project, I improved my understanding of:

  • stack-based problem solving
  • linked list manipulation in C
  • input parsing and validation
  • algorithm design under strict constraints
  • optimizing operations rather than only focusing on correctness
  • organizing a C project into modular components

Notes

This project was developed as part of the 42 curriculum and reflects my approach to combining correctness, efficiency, and clean project structure in C.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

42 push_swap project in C, focused on stack-based sorting, algorithm design, input parsing and operation optimization.

Topics

42-madrid42-schoolalgorithmscdata-structurespush-swapsortingstack

Resources

Readme
MIT license
Activity

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.