|
My Project
|
push_swap is a 42 project that implements sorting algorithms with a restricted set of stack operations (push, swap, rotate). The goal is to sort a list of integers in as few moves as possible. I chose to implement radix sort algorithm and worked on a custom
ft_qsort.
๐ View on GitHub ๐ View full documentation
๐ข Custom Stack-Based Sorting Engine Implements a sorting algorithm using only a limited set of allowed operations: sa, sb, pa, pb, ra, rb, rra, rrb, and their combined forms.
โ๏ธ Radix Sort for Large Inputs Efficiently handles large lists using a binary radix sort algorithm, minimizing the total number of operations while maintaining predictable performance.
๐งฎ Custom ft_qsort for Index Assignment To prepare for radix sorting, the program generates a sorted reference array using a handcrafted ft_qsort function. Itโs a lightweight, optimized implementation inspired by quicksort. This approach avoids external dependencies, offers full control over sorting behavior, and aligns with 42's constraint of using only custom code.
๐ง Hardcoded Mini-Sort for 2โ5 Elements Uses optimized, minimal instruction sets for very small stacks to ensure the fewest possible moves.
๐ก๏ธ Strict Input Parsing and Validation Handles both quoted and space-separated arguments, validates integer range and format, checks for duplicates, and safely exits on error.
๐ฆ Modular and Maintainable Codebase Cleanly structured with separate directories for parsing, operations, sorting strategies, and utilities โ with Doxygen-style comments for full documentation.
๐งช Built-In Sorted Check Automatically exits without performing any operations if the input is already sorted.
Radix sort is a strong choice for the push_swap project due to several key advantages:
โ Operation Count Efficiency Radix sort runs in O(n ร k) time, where n is the number of elements and k is the number of bits needed to represent the largest number. This helps keep the total moves low, which is crucial for the push_swap project.
โ Stable and Predictable Behavior Radix sort offers deterministic performance, processing elements in a fixed number of bitwise passes. This consistency ensures a controlled, step-by-step sorting process without relying on input patterns โ ideal for minimizing operations in a constrained environment like push_swap.
โ
Bitwise-Friendly for Stack Logic Radix sort focuses on binary representations, which aligns naturally with low-level stack operations. Leveraging bit shifts (>>) and comparisons at the bit level complements the mechanics of push_swap.
Radix sort in push_swap works by sorting integers based on their binary representation, one bit at a time. This process involves several clear steps:
1. Assigning Sorted Indices First, the integers in Stack A are mapped to their respective indices in a sorted sequence:
[30, 10, 20][10, 20, 30][2, 0, 1]This indexing simplifies sorting by focusing on smaller, normalized values rather than larger integers.
2. Determining Bit Count The algorithm calculates how many binary digits (bits) are required to represent the largest index. The number of bits required corresponds how many times you can shift the maximum index to the right (>> 1) until it becomes zero.
This tells you how many passes of sorting the algorithm will need to do in the bitwise loop.
3. Bitwise Sorting Loop For each bit position (from the least significant bit to the most significant bit):
Why This Works ? By pushing โ0โ elements to Stack B and rotating โ1โ elements within Stack A, each pass of the bit sorting groups elements according to whether they have a 0 or 1 in the current bit position. After enough passes (equal to the total bits needed), all elements end up fully sorted in ascending order of their indices.
4. Restoring Original Values
๐งฎ Radix Sort Flow โ Sequence Diagram
๐ก Why This Works Efficiently This binary approach guarantees sorting in O(n ร k) complexity, where n is the number of elements and k is the number of bits. Unlike traditional sorting algorithms that depend on element comparisons, radix sort operates directly on bits, offering predictable and efficient performance.
Tested on a local machine using randomized input sets with output redirected to /dev/null to exclude printing time.
| Input Size | Execution Time | Approx. Operation Count |
|---|---|---|
| 100 | ~0.004s | 855/857 ops |
| 500 | ~0.012s | 5739/5741 ops |
| 1000 | ~0.037s | 12730/12732 ops |
| 10000 | ~3.9s | 170953/170956 ops |
โฑ๏ธ Command used:
time ./push_swap $(shuf -i 1-10000 -n 10000) > /dev/null
๐ Note: Results may vary slightly based on hardware, compiler flags, and system load. And results are bound to the constraints of the project no multi-threading, pur C logic & stack ops, and no optimization flags.
1. To compile executables: ๐ ๏ธ
Note: This project uses libft as a git submodule.
If you're cloning the repository for the first time, don't forget to initialize and update submodules:
or if you already cloned it:
Run the program: Example 1: basic
Output:
Example 2: use of environment variable and checker_linux that is provided (check if the stack/array is sorted)
Output:
Example 3: use of environment variable and line count to evaluate the number of operation used
Output:
This project is licensed under the [MIT License](LICENSE).
You are free to use, modify, and distribute this code for academic, personal, or professional purposes. Attribution is appreciated but not required.
If you have any questions, suggestions, or feedback, feel free to reach out:
You're also welcome to open an issue or leave a comment on the repository.