0

How do I make the function return the result of the sorted arrays?

class quiksort
{
    public static char[] qsort(char[] items)
    {
        return qs(items, 0, items.Length - 1);
    }

    // A recursive version of Quicksort for characters. 
    static char[] qs(char[] items, int left, int right)
    {
        int i, j;
        char x, y;

        i = left; j = right;
        x = items[(left + right) / 2];

        do
        {
            while ((items[i] < x) && (i < right)) i++;
            while ((x < items[j]) && (j > left)) j--;

            if (i <= j)
            {
                y = items[i];
                items[i] = items[j];
                items[j] = y;
                i++; j--;
            }
        } while (i <= j);

        if (left < j)
        {
            return qs(items, left, j);
        }
        if (i < right)
        {
            return qs(items, i, right);
        }
    } 
}

the errors says that not all code paths return a value? what does that mean

3
  • You have two if conditions, but what if none is met, you're not returning anything in that case. Commented Jan 18, 2014 at 1:54
  • yeah i just saw it now. i just returned a value of null. hmm but there's no output showing when i run the code Commented Jan 18, 2014 at 1:56
  • ... and now that you have a sorting algorithm, consider using it for not only chars. Make it generic. :-) Your method signature then might look like static T[] qs<T>(T[] items, int left, int right) where T : IComparable<T>. You would only have to replace char with T and the < operator with the CompareTo() method from IComparable. Commented Jan 18, 2014 at 11:05

2 Answers 2

2

The problem is in your second method. As the error states all code paths must return a value. You have your return statements within if statements. Even if one of those will always execute the compiler does not care, as far as it is concerned there is the potential that your method won't return anything. You need to add a return as the last line of the function.

    static char[] qs(char[] items, int left, int right)
    {
        int i, j;
        char x, y;

        i = left; j = right;
        x = items[(left + right) / 2];

        do
        {
            while ((items[i] < x) && (i < right)) i++;
            while ((x < items[j]) && (j > left)) j--;

            if (i <= j)
            {
                y = items[i];
                items[i] = items[j];
                items[j] = y;
                i++; j--;
            }
        } while (i <= j);

        if (left < j)
        {
            return qs(items, left, j);
        }
        if (i < right)
        {
            return qs(items, i, right);
        }
        return //whatever is most appropriate in the case that you arrive here
    }

I'm not sure what value you actually want to return there so I'll leave that to you. If you expect to only get there in the case of an error then I would probably use null.

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

1 Comment

Instead of returning null in case of an error you can also throw an appropriate exception (InvalidOperationException, ArgumentException). In this case however it's actually better to not return anything (void), cause you are altering the input as well.
1

evanmcdonnal's answer is correct as to the error.

More generally though, it's confusing to have a method take a char[] parameter, change that char[] and then return a char[] (the same char[], but that isn't obvious from just the signature.

If you're going to do an in-place replacement like this, then your code will be clearer if you just return void, making it obvious that you alter the char[] passed as a parameter.

Conversely, if you're going to return a sorted array, then return a new array, and leave the one you are passed unaltered.

1 Comment

Good general advice about how to structure your code and probably better than just adding the line I say is required to make the compiler error go away.

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.