Overview
This page demonstrates Push Swap behavior across different input sizes, from trivial cases to complex sorting scenarios.Sorting 2 Numbers
The simplest non-trivial case uses a single swap operation.Example: Two Unsorted Numbers
Output
push_swap.c:64-65, which simply swaps the top two elements.
Example: Two Sorted Numbers
Output
Sorting 3 Numbers
Three-number sorting uses the optimizedsort_three() algorithm from push_swap.c:15-42.
Example: Reverse Order
Output
val1 > val2 && val2 > val3 (line 24-28).
Example: Maximum at Top
Output
Example: Two Swaps Needed
Output
val1 < val2 && val2 > val3 && val1 < val3 (line 35-39).
Sorting 5 Numbers
Five numbers trigger thesort_big_stacks() algorithm, which pushes elements to stack B, then sorts optimally.
Example: Random Order
Example Output
The exact output depends on the implementation of
sort_big_stacks(). The algorithm pushes elements to stack B, sorts stack A, then pushes back with optimal positioning.Example: Nearly Sorted
Example Output
Sorting 100 Numbers
For 100 random numbers, the algorithm must be efficient. The evaluation criteria typically requires:- Maximum ~700 operations for a “good” grade
- Maximum ~900 operations for a “passing” grade
Example: Testing with Random Input
Counting Operations
wc -l to count the number of operations output.
Sorting 500 Numbers
The ultimate test of the algorithm’s efficiency:- Maximum ~5500 operations for a “good” grade
- Maximum ~7000 operations for a “passing” grade
Example: Testing Performance
Understanding Good vs Bad Test Cases
Good Test Cases
Effective test cases reveal edge conditions:Bad Test Cases
These don’t provide useful information:Benchmarking Your Implementation
Create a comprehensive test:Test Script
Visualization Tips
To better understand the algorithm:- Use a checker program: Verify that your operations correctly sort the input
- Trace execution: Add debug output to see stack states
- Visualize operations: Create or use a visualizer to see rotations and pushes
Next Steps
- Learn about Error Handling for invalid inputs
- Explore the Turkish Algorithm used for large stacks
- Study Operations to optimize your operation sequences