This project demonstrates an advanced sorting algorithm called Merge-Insertion Sort (also known as Ford-Johnson Algorithm), designed to minimize the number of comparisons while sorting a list of integers. This implementation is done in C++ using vectors.

- How Merge Sort and Insertion Sort are combined for efficiency.
- The importance of the Jacobsthal sequence in insertion ordering.
- How to perform optimized Binary Search.
- How to handle edge cases like duplicate input and invalid values.




-
Clone the repository to your local machine:
git clone https://github.com/samir-ouaammou/Merge-Insertion-Sort-Algorithm
-
Navigate to the project directory:
cd Merge-Insertion-Sort-Algorithm
-
Compile the source files using
make
:make
-
Clean up compiled files:
make clean
-
To remove all object files and the executable:
make fclean
-
To recompile the project from scratch:
make re
-
Run the program:
./PmergeMe 5 2 9 3 7 1 6
- A hybrid algorithm combining Merge Sort and Binary Insertion.
- Reduces total comparisons especially for small inputs.
- Inspired by the Ford-Johnson algorithm (1959).
- Split input into pairs.
- For each pair: the larger value goes into the
bigs
list, the smaller into thesmalls
list. - Recursively sort the
bigs
list.
- After sorting the
bigs
, thesmalls
are inserted one by one using Binary Search. - The order of insertion follows a Jacobsthal-based sequence to minimize comparisons.
- For each insertion, we use
std::lower_bound()
(which performs binary search internally). - It allows fast location of the correct position to insert an element in a sorted vector.
-
Defines the order in which the "small" elements should be inserted.
-
Formula:
- J(n) = J(n - 1) + 2 * J(n - 2)
- Initial values: J(0) = 0, J(1) = 1
-
Helps to optimize the number of comparisons when inserting elements.
Example sequence: 0, 1, 3, 5, 11, 21, ...
- Ensure all inputs are valid integers.
- No negative values.
- No duplicates.
- Group the input in pairs.
- Save the largest from each pair in
bigs
, the smallest insmalls
. - If the total number of elements is odd, track the leftover.
sortVector()
is called recursively onbigs
.
insertOrder(n)
computes the optimized order.- This ensures efficient insertions with fewer comparisons.
- Insert each element from
smalls
into the sortedbigs
using binary search and the Jacobsthal order.
- If there was an odd number of elements, insert the leftover at the correct position.
- Print the vector before and after sorting.
- Show the execution time.
Before: 5 2 9 3 7 1 6
After: 1 2 3 5 6 7 9
Time to process a range of 7 elements with std::vector : 12.00 us
.
โโโ main.cpp # Implementation file
โโโ README.md # This file
โโโ assets/
โโโ merge-insertion-diagram.png # Suggested image illustrating the algorithm
- Recursion
- Pairing strategy
- Binary search with
lower_bound()
- Custom insertion sequence (Jacobsthal)
- Performance timing with
gettimeofday()
Developed by Samir Ouaammou as part of 1337/42 core curriculum.
This project brings theory and practice together with an elegant algorithm that combines classical ideas with smart optimizations. Merge-Insertion Sort is not just sorting โ it's sorting with strategy.
๐ฅ Happy Hacking!
Want to dive deeper? Here are some great resources to understand Merge-insertion sort better:
Thank you for checking out my (Merge Insertion Sort Algorithm) project! Stay tuned for more exciting challenges. ๐ฅ