Question

I created a class to quicksort an array of ints and now im trying to call that class from the program but i keep getting an error, the class is as follows :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuicksortSimpel
{
    class Class1
    {
        public static void Quicksort(IComparable[] elements,int left, int right)
        {

            //Define i and j and pick an pivot to compare other numbers
            int i = left, j = right;
            IComparable pivot = elements[left + (left + right) / 2];

            //Compare numbers and then compare to pivot
            while (i <= j)
            {
                while (elements[i].CompareTo(pivot) < 0)
                {
                    i++;
                }

                while (elements[j].CompareTo(pivot) > 0)
                {
                    j--;
                }

                if (i <= j)
                {
                    // Switch
                    IComparable tmp = elements[i];
                    elements[i] = elements[j];
                    elements[j] = tmp;

                    i++;
                    j--;
                }
            }
            // repeat
            if (left < j)
            {
                Quicksort(elements, left, j);
            }

            if (i < right)
            {
                Quicksort(elements, i, right);
            }
        }
    }
}

I try calling it like this :

numbers = Class1.Quicksort(numbers);

The error that i am getting is this :

No overload for method quicksort takes 1 arguments.

Was it helpful?

Solution

This is because Class1.Quicksort(); takes three arguments and you are passing 1. And also it does not return anything so doing numbers = Class1.Quicksort(); will not work.

You could overload or set optional/default parameters (thanks Silo ).

public static void Quicksort(IComparable[] elements,int left = 0, int right = 0)

or

public static void Quicksort(IComparable[] elements)
{
    QuickSort(elements, 0,0);
}

OTHER TIPS

You are getting this complier error because class method QuickStart() is expecting three params meanwhile you are providing it with only one param.

Your class is not static so why you are accessing it with Class name ?? You need to create an object of Class1 then call sort method on that object. I also noted that you are assigning returned value from this method to some variable but in function definition you don't returning anything ??

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top