Click here to Skip to main content
15,064,140 members
Articles / General Programming / Algorithms
Posted 11 Mar 2012


11 bookmarked

Simo Sort: A Sorting Algorithm for Elements with Low Variance

Rate me:
Please Sign up or sign in to vote.
4.63/5 (8 votes)
12 Mar 2012CPOL2 min read
Simo Sort a New Sorting algorithm C++ Binary Value Numbers Sort Elements with Low Variance


I was writing an article on determining the fastest algorithm for sorting and while I was writing I got influenced by the many sorting algorithms and I devised an algorithm for sorting. It's called Simo because that's the name of someone who is very special to my heart :)

How it works

It is a recursive sorting algorithm that repeats the following steps:

  1. Get the average value of numbers in the array
  2. Divide the array into two arrays (array 1 and array 2) and the average value will be chosen as the pivot.
  3. Any value smaller than the pivot will be in first array and any value larger than the pivot will be in the second array.
  4. Repeat the same algorithm on array 1 and array 2 till you reach the ending condition

For optimization purposes:

  1. The ending condition is not a normal ending for a recursive function but it uses some conditions that I devised through try and error, these conditions raise the performance alot.
  2. If the array size is less than 15 an Insertion Sort is used to decrease overhead, 15 has been found empirically as the optimal cutoff value in 1996.

The algorithm

This is how the algorithm works:

low is the index of the first element and high is the index of last element.

template <class T>
void simoSort(T a[],int low,int high)
    //Optimization Condition
        double average=0;    
        int n = (high - low + 1);
        for (int i = low; i <= high; i++)    

        int k=low;

        for(int i = low;i<=high; i++)
            int tempValue = a[i];
            a[i] = a[k];
            a[k] = tempValue;


        //Optimized Stop Condition
        if(low<k-1 && k-1!=high)

        if(high>k && low!=k)
        //Insertion Sort to reduce Overhead when recursion is not deep enough
        for (int i = low + 1; i <= high; i++) {
            int value = a[i];
            int j;
            for (j = i - 1; j >= low && a[j] > value; j--) {
              a[j + 1] = a[j];
            a[j + 1] = value;


"Example 1" shows how the algorithm works in a normal case.

Image 1

"Example 2" shows how the algorithm works in its best case scenario where elements of the array have little variance between them. In the case where there where only 2 numbers "1" and "0" the algorithm had a complexity of O(n)

Image 2

Note: When the algorithm stops in the shown figures it means a leaf has reached an ending condition.


The Search is Stable and has an upper bound space Complexity of O(1)

After doing the time complexity analysis calculations.

  • The Average and Worst case scenarios are of (5/6)*nlogn complexity which maps to O(nlogn)
  • The Best Case is O(n) as shown in Example 2.

If any one can prove these values wrong please tell me and I will modify them, thanks.

The algorithm is currently faster than quick sort and bucket sort when the variance of the elements is low(not more than 5 different elements) according to the I benchmark that I made, and I'm currently writing an article that will compare all sorting algorithms including this one. 


The current version of the algorithm only works on integers. it does not work on chars or double values.


1.0 (10 March 2012)

  • Initial release.

I hope that this article would at least slightly help those who are interested in this issue. Feel free to tell me any comments on the algorithm :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mahmoud Hesham El-Magdoub
Software Developer (Senior) Free Lancer
Egypt Egypt
BSc Computer Engineering, Cairo University, Egypt.

I love teaching and learning and I'm constantly changing Smile | :)
Feel free to discuss anything with me.

A Freelance UX designer, Code is my tool brush, I design for experience !

My Website

Comments and Discussions

QuestionIterators instead of T[] Pin
Werner Salomon17-Mar-12 5:32
MemberWerner Salomon17-Mar-12 5:32 
QuestionImpact of calculating the mean Pin
mounte@sandvik11-Mar-12 22:40
Membermounte@sandvik11-Mar-12 22:40 
AnswerRe: Impact of calculating the mean Pin
Mahmoud Hesham El-Magdoub11-Mar-12 22:53
MemberMahmoud Hesham El-Magdoub11-Mar-12 22:53 
QuestionDifference to Quicksort? Pin
nv311-Mar-12 12:38
Membernv311-Mar-12 12:38 
AnswerRe: Difference to Quicksort? Pin
Mahmoud Hesham El-Magdoub11-Mar-12 22:30
MemberMahmoud Hesham El-Magdoub11-Mar-12 22:30 
GeneralRe: Difference to Quicksort? Pin
melimani12-Mar-12 0:37
Membermelimani12-Mar-12 0:37 
GeneralRe: Difference to Quicksort? Pin
Mahmoud Hesham El-Magdoub12-Mar-12 0:53
MemberMahmoud Hesham El-Magdoub12-Mar-12 0:53 
GeneralRe: Difference to Quicksort? Pin
melimani12-Mar-12 2:02
Membermelimani12-Mar-12 2:02 
GeneralRe: Difference to Quicksort? Pin
Mahmoud Hesham El-Magdoub12-Mar-12 2:09
MemberMahmoud Hesham El-Magdoub12-Mar-12 2:09 
GeneralRe: Difference to Quicksort? Pin
nv312-Mar-12 8:48
Membernv312-Mar-12 8:48 
Yes Mahmoud, I understand your point. All I wanted to suggest is that you explain in your article here, why picking the pivot element by calculating the average will increase the performance, at least in a certain class of cases. You can do that by either a theoretical analysis or by experiment.

What I can't see yet is, why in cases of low variance sets your algorithm is particularly useful. I think that deserves some explanation. Do you mean by low variance that many elements in the sets have the same value, or in the statitical sense that the standard deviation is low -- and what is low?

And of course you are right, just because a field has been researched for 50 years by some really great geniuses of information science, that is no reason why there should not be any improvement possible. However, I would be extremely suspicious if I found such an improvement already after my first steps in that field.

So try to show that you have found something real. And even if you can prove the opposite, namely that the average calculation is not worth the cpu cycles, that would also be a valuable result. Don't be discouraged, but be self-critical with the results you achieve.
GeneralRe: Difference to Quicksort? Pin
Mahmoud Hesham El-Magdoub12-Mar-12 8:57
MemberMahmoud Hesham El-Magdoub12-Mar-12 8:57 
GeneralRe: Difference to Quicksort? Pin
Member 851795515-Mar-12 0:10
MemberMember 851795515-Mar-12 0:10 
GeneralRe: Difference to Quicksort? Pin
Mahmoud Hesham El-Magdoub15-Mar-12 10:27
MemberMahmoud Hesham El-Magdoub15-Mar-12 10:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.