A highly optimized integer sorting project implementing custom stack-based sorting algorithms through the push_swap
program.
- Overview
- Program Functionality
- Features
- Algorithm Design
- Installation
- Usage
- Examples
- Testing
- Program Structure
- Contributing
- License
- Author
- Support
Super Cool GOAT Stack Sorter presents push_swap
, a custom sorting application that implements efficient stack-based sorting algorithms. Given a set of integers, the program outputs a sequence of operations that sorts the data in the most efficient way possible using only two stacks and a limited set of operations.
The push_swap
program takes a sequence of integers as input and:
- Parses and validates the input data
- Determines the most efficient sorting strategy based on input size
- Executes the appropriate sorting algorithm
- Outputs the sequence of operations needed to sort the data
The program uses two stack (A and B) and performs operations like swapping, pushing, and rotating elements to achieve the sorted state. What makes push_swap
unique is its focus on minimizing the number of operations needed to sort the data.
Operation | Description |
---|---|
sa | Swap the first 2 elements at the top of stack A |
sb | Swap the first 2 elements at the top of stack B |
ss | Execute sa and sb simultaneously |
pa | Take the top element from stack B and push it to stack A |
pb | Take the top element from stack A and push it to stack B |
ra | Rotate stack A upward (first element becomes last) |
rb | Rotate stack B upward (first element becomes last) |
rr | Execute ra and rb simultaneously |
rra | Reverse rotate stack A downward (last element becomes first) |
rrb | Reverse rotate stack B downward (last element becomes first) |
rrr | Execute rra and rrb simultaneously |
- Multi-strategy sorting system that selects the optimal algorithm based on input size
- Memory-efficient implementation that minimizes resource usage
- Robust error handling that validates input and prevents crashes
- Optimized for performance with minimal operation counts
- Clean output format showing the exact sequence of opertaions
- Scalable from small to large datasets with different optimization techniques
The Super Cool GOAT Stack Sorter implements multiple sorting strategies optimized for different data sizes:
For small datasets (2-5 elements), the program uses specialized algorithms that:
- Handle each permutation with the minimum possible operations
- Use pattern recognition to immediately identify the optimal solution
- Execute direct, targeted operations without complex calculations
For larger datasets, the program employs a sophisticated chunking strategy:
- Data is divided into optimal-sized chunks based on the input size
- Elements are strategically distributed between stacks using pivot values
- Partial pre-sorting during distribution reduces later operations
- Final sorting uses position optimization to minimize movements
Clone the repository:
git clone https://github.com/hosu-kim/super_cool_GOAT_stack_sorter.git
cd super_cool_GOAT_stack_sorter
Compile the program:
make
./push_swap [integers]
The program accepts integers as command-line arguments and outputs the sequence of operations to sort them.
- Values must be integers
- Each number must be unique
- Numbers must be within the integer range (-2147483648 to 2147483647)
$ ./push_swap 3 1 2
sa
This performs one swap operation to sort the three numbers.
$ ./push_swap 5 1 4 2 3
pb
ra
pb
sa
ra
pa
ra
pa
The program determines an efficient sequence of 8 operations to sort these 5 number.
$ ./push_swap 10 20 10
Error
The program detects duplicate values and outputs an error message.
You can verify the correctness of your push_swap implementation using the provided checker program located in the test
directory:
ARG="4 67 3 87 23"; ./push_swap $ARG | ./test/checker_linux $ARG
This command:
- Generates a sequence of operations from your push_swap program
- Pipes these operations to the checker program
- The checker verifies if the operations correctly sort the input numbers
The checker will output one of the following:
OK
: The operations correctly sort the input numbersKO
: The operations fail to sort the input numbersError
: An error occurred during execution (invalid operation or other issue)
For thorough testing with random numbers:
# Test with 5 random numbers
ARG=$(shuf -i 1-100 -n 5 | tr "\n" " "); echo $ARG; ./push_swap $ARG | ./test/checker_linux $ARG
# Test with 100 random numbers and count operations
ARG=$(shuf -i 1-1000 -n 100 | tr "\n" " "); ./push_swap $ARG | wc -l
For optimal grading results, aim for:
- 3 numbers: ≤ 2 operations
- 5 numbers: ≤ 12 operations
- 100 numbers: ≤ 700 operations
- 500 numbers: ≤ 5500 operations
Component | Description |
---|---|
Core Engine | Main program logic and algorithm selector |
Stack Implementation | Custom stack data structure with optimized operations |
Operation Modules | Implementation of push, swap, rotate and reverse operations |
Sorting Algorithms | Small-set and large-set sorting strategies |
Utility Functions | Input validation, error handling, and helper functions |
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
hosu-kim ([email protected])
For support, please create an issue in the GitHub repository or contact me directly.