Home - pin3dev/42_PushSwap GitHub Wiki

Documentation

This project is part of the 42 curriculum and focuses on algorithmic problem-solving using a sorting algorithm. The goal of this project is to implement a sorting algorithm for a stack of integers using two stacks and a predefined set of operations. The objective is to sort the integers with the fewest possible moves.

Directory Tree

/pushswap
│
├── /inc
│   └── push_swap.h
│
├── /src
│   ├── push_swap.c
│   ├── swap.c
│   ├── push.c
│   ├── rotate.c
│   ├── reverse_rotate.c
|   ...
│
└── Makefile

/inc: This directory contains the project's header file, which defines the necessary function prototypes, macros, and data structures.
/src: This directory contains the source code files for the core functionality, including sorting logic, stack operations, and input validation.

Logical Groups

Main Operations Algorithm Markers Wrapped Refactored Utils
push_swap swap, push, rotate, reverse_rotate sort_3, big_sort, sort_cases, return_dest_place, find_best_case, mv_node_to_top_a, mv_node_to_top_ab, mv_node_to_top_b, fit_between, rotate_to_finish, back_to_a find_place_n_def_total_mov, put_index_n_def_mov_n_orient, put_mov_and_orientation, put_total_mov sa, sb, ss, ra, rb, rr, rra, rrb, rrr, pa, pb, ft_stackadd_back, ft_stacklast, ft_stacksize ft_stacknew, _atoi, skip_spaces ft_max, ft_min, ck_new_min_max, create_node, args_splited, args_to_node, ck_doubles, ck_sorted, ft_error, free_stack, free_split

Main Functions

The Main section includes the primary entry point of the program, push_swap. This function initializes the necessary stacks and determines the optimal sorting algorithm based on the size and complexity of the input. It orchestrates the sorting process, ensuring the correct steps are followed for efficient stack sorting, whether it's a small or large set of integers.

Operations Functions

The Operations section manages the core stack operations essential to the sorting algorithm. These functions perform the basic manipulations of the two stacks (A and B) using operations like swap, push, rotate, and reverse_rotate.

Algorithm Functions

Implements various sorting strategies for different scenarios. Functions like sort_3, big_sort, and sort_cases are responsible for handling different stack sizes and complexities. These functions optimize stack movements by calculating the best case scenario for each element in the stack and performing operations to achieve the fewest moves possible. They also include helper functions like find_best_case and mv_node_to_top_ab for managing element placement and stack rotation.

Markers Functions

Involves calculating the necessary moves and rotations for each stack element. Functions like find_place_n_def_total_mov and put_total_mov are designed to track the movements needed to place each element in the correct position within the stack. These functions also mark and define the best total movements and orientations for sorting the stack efficiently.

Wrapped Functions

Contains wrapped versions of pre-existing functions for better readability.

Refactored Functions

Includes pre-existing functions in Libft that needed to be restructured to improve readability, efficiency or modularity.

Utility Functions

Provides a range of helper functions that are used throughout the program to ensure smooth execution. Functions like ft_max, ft_min, and ck_new_min_max are used for comparisons and finding extreme values in the stack. Functions such as args_to_node, ck_doubles, and ck_sorted handle input validation, ensuring the input is sorted correctly and without duplicates. Memory management is handled by functions like free_stack and free_split to avoid memory leaks.

Next ➡️

⚠️ **GitHub.com Fallback** ⚠️