A custom integer sorting algorithm that leverages a multi-dimensional array structure (the "shelf") to categorize numbers by digit length, handle duplicates via insertion sort, and merge them back into a single sorted sequence -- achieving O(n) time when there are no duplicates.
- Algorithm Overview
- Complexity
- Architecture
- Getting Started
- Example
- Documentation
- Contributing
- License
- Author
ShelfSort operates in three distinct phases:
Each element is placed into a slot within a two-dimensional array (the "shelf") based on its digit length. Numbers with 1-2 digits share one row, while numbers with 3+ digits are placed into rows corresponding to their length. The column index is derived from the number's value, which means each number is placed directly into its sorted position within its row.
When a number is encountered that already occupies its designated slot, it is identified as a duplicate. Duplicates are collected into a separate array and kept in sorted order using an insertion sort approach as they are added.
The algorithm walks through each row of the shelf in order, pulling out non-empty values. As it reconstructs the original array, it checks the duplicate list and inserts any matching duplicates at the correct position. The result is a fully sorted array.
| Metric | Complexity |
|---|---|
| Time (Best Case) | O(n) -- no duplicates; each element is placed and retrieved in constant time |
| Time (Average Case) | O(n + k^2) -- where k is the number of duplicate elements |
| Time (Worst Case) | O(n^2) -- all elements are duplicates; insertion sort dominates |
| Space | O(n * d) -- where d is the number of distinct digit lengths |
Shelf-Sorting-Algorithm/
├── pom.xml
├── LICENSE
├── README.md
├── .editorconfig
├── .gitattributes
├── .gitignore
├── docs/
│ └── ALGORITHM.md
└── src/
├── main/
│ └── java/
│ └── com/
│ └── shelfproject/
│ ├── ShelfSort.java # Core algorithm (sort + merge)
│ ├── ShelfSortDemo.java # Demo with example usage
│ └── util/
│ └── ArrayUtils.java # Array formatting and generation helpers
└── test/
└── java/
└── com/
└── shelfproject/
├── ShelfSortTest.java # Unit tests
└── ShelfSortBenchmark.java # Performance benchmarks
- Java 8 or higher
- Apache Maven 3.x
git clone https://github.com/R-Alothaim/ShelfSort.git
cd ShelfSort
mvn compilemvn exec:java -Dexec.mainClass="com.shelfproject.ShelfSortDemo"mvn testmvn test -Dtest=ShelfSortBenchmarkInput:
[3, 4, 3, 11, 23, 4421, 2, 11, 2]
Output:
[2, 2, 3, 3, 4, 11, 11, 23, 4421]
For a detailed explanation of the algorithm, including phase-by-phase breakdown, complexity analysis, and limitations, see docs/ALGORITHM.md.
Contributions are welcome. Please open an issue first to discuss what you would like to change, then submit a pull request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/my-feature) - Commit your changes (
git commit -m 'Add my feature') - Push to the branch (
git push origin feature/my-feature) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.
R-Alothaim -- GitHub