0

I am working on a projekt (for school) and one of the requirements is to sort an array. So I decided to use the quicksort. (I chose it because it is an algorithm that I don't know by now) I managed to understand how it works and I also found an option of its implementation in c++.

void quicksort(size_t v[], size_t start_pos, size_t end_pos)
{
    size_t i, j, di, dj;
    if (start_pos < end_pos)
    {
        i = start_pos, j = end_pos;
        di = 0, dj = 1;
        do
        {
            while (i < j && (v[j] > v[i] || v[j] == v[i])) i += di, j -= dj;
            if (i < j)
                std::swap(v[i], v[j]), di = (di + 1) % 2, dj = (dj + 1) % 2;
        } while (i < j);
        if (i - start_pos > 1)
            sort(v, start_pos, i - 1);
        if (end_pos - i > 1)
            sort(v, i + 1, end_pos);
    }
}

What I don't understand is... why in the last 2 if statements is used ">1"? I hope someone can clarify this for me. Thank you! :)

5
  • 7
    So you were asked to write a sort function for your project, and instead of actually writing one, you just randomly found one online, and now you want us to explain to you how it works? Did that about sum it up? Commented Jun 24, 2015 at 11:27
  • 5
    You should understand how it works not by the code but by looking into the algorithm specification. Otherwise, your question not about the algorithm but about the code you have found somewhere. Commented Jun 24, 2015 at 11:28
  • 3
    There are already functions to sort arrays. Commented Jun 24, 2015 at 11:30
  • 5
    This is a really weird implementation of quicksort. At the very least - it's not even recursive? What is sort? Commented Jun 24, 2015 at 11:31
  • 1
    What I don't like in the posted code: array of size_t (!?), abuse of the comma operator, delta variables di,dj which have values 0 or 1 and make the logic more complex, the code to flip the deltas di = (di+1)%2 (when a xor or a ! suffices), the weird condition (v[j] > v[i] || v[j] == v[i]) (why not >=?). Commented Jun 24, 2015 at 12:29

2 Answers 2

3

Both calculations provides the size of the left and right subdivision respectively.

If the size is 1 or zero, that part is already sorted and doesn't need any further processing.

    if (i - start_pos > 1)  
        sort(v, start_pos, i - 1);  

The sort call is only made if the range is two or more elements. As Barry points out, this should probably be

    if (i - start_pos > 1)  
        quicksort(v, start_pos, i - 1);        

Victors comment is also on point.

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

Comments

0

If you look carefully, the upper portion of the code does the arranging the array corresponding to pivot element which is indexed at i and the last two if's sort the remaining portion i.e. to the right and left of the pivot element but in case your pivot index has already reached end_pos you don't need to sort elements to right of the pivot element thus there is an if condition whether i < end_pos.

Comments

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.