3
template<class T> void sSort(T *A, int first, int last) 
{
    if(A[first]>A[last])
        swap(A[first],A[last]);

    if(first+1>=last)
    return;
    double  k = floor((last-first+1)/3);


    sSort(A,first,last-k);
    sSort(A,first+k,last);
    sSort(A,first,last-k);
}

I perfectly understood the mergeSort, bubbleSort complexities but i'm so confused in this one. What is the complexity for this algorithm. Can anyone explain?

1
  • You could just try running it on a few arrays of different sizes and see how the time compares to the array size... Commented Nov 11, 2011 at 0:01

2 Answers 2

10

This is the Stooge sort. It's an algorithm constructed to show that amateurs really shouldn't implement their own algorithms without properly analyzing them first. Its running time is approximately O(n^3).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this link help me to reach further documentation.
2

It's not too hard to do the calc.

  • Every time this algorithm do 3 calls to itself splitting into 3 (equal) part the portion of the input of the current step. Note: The first call and the third are the same.
  • The local complexity is just a O(1) (which means constant) since it will do just a swap, an if and the calculation of k

3 Comments

I am rusty on this kind of thing, but I think it's a bit more involved as the three recursive calls operate on overlapping value ranges.
I actually stuck with 3 calls, i can't calculate them, i'm going kinda crazy :)
actually algorithm works, but so slowly, you can try to run it :). there aren't any mistyping in the algorithm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.