-2

Errors:

  • An object reference is required for the non-static field, method, or property 'Program.Quicksort(int[], int, int)'In Place Quicksort.

  • An object reference is required for the non-static field, method, or property 'Program.ShowMyArray(string)'In Place Quicksort.

Lines: Quicksort(MyArray, LBoundInteger, UBoundInteger);

ShowMyArray("Sorted Array");

        LBoundInteger = MyArray.GetLowerBound(0);
        UBoundInteger = MyArray.GetUpperBound(0);

        outString = showLBoundStr + LBoundInteger.ToString() + Environment.NewLine + showUBoundStr + UBoundInteger.ToString();
        Console.WriteLine(outString);
        QSCallCnt = 0;
        QSSwaps = 0;
        Quicksort(MyArray, LBoundInteger, UBoundInteger);
        ShowMyArray("Sorted Array");
    }

    private void Quicksort(int[] list, int min, int max)
    {
        int[] MyArray = { 65, 25, 75, 15, 85, 35, 95, 67, 48, 66 };
        int pivot = 0;
        int p = 0;
        int temp = 0;
        int QSCallCnt = 0;
        int QSSwaps;
        string round = "round";
        string showMinOutstr = "min=";
        string showMaxOutStr = "max=";
        string showPivotOutStr = "pivot=";
        string beginSortRecursion = "Begin Sort -Recursion ";
        string pivotEnd = "Pivot to the end";
        string QsCallcntStr = "QSCallCnt = ";
        string QsSwapStr = "  QSSwaps = ";
        string outString;

        Random randomNumber = new Random();

        QSCallCnt += 1;
        QSSwaps = 0;
        outString = showMinOutstr + min.ToString() + showMaxOutStr + max.ToString();
        Console.WriteLine(outString, round, QSCallCnt);

        if (min >= max)
        {
            return;
        }

        ShowMyArray(beginSortRecursion + QSCallCnt);

        p = randomNumber.Next(min, max + 1);
        pivot = list[p];

        outString = showMinOutstr + min.ToString() + Environment.NewLine + showMaxOutStr + max.ToString() + Environment.NewLine + showPivotOutStr + pivot.ToString();
        Console.WriteLine(outString);

        temp = list[max];
        list[max] = list[p];
        list[p] = temp;
        ShowMyArray(pivotEnd);

        for (int i = min; i <= max; i++)
        {
            if (list[i] > pivot)
            {
                for (int j = i + 1; j <= max; j++)
                {
                    if (list[j] <= pivot)
                    {
                        temp = list[i];
                        list[i] = list[j];
                        list[j] = temp;
                        QSSwaps += 1;
                        ShowMyArray(QsCallcntStr + QSCallCnt + QsCallcntStr + QSSwaps);
                        break;
                    }
                }
            }
        }

        for (int i = min; i <= max; i++)
        {
            if (list[i] == pivot)
            {
                p = i;
                break;
            }
        }

        if (p - min < 2)
        {
            Quicksort(MyArray, p + 1, max);
        }
        else if (max - p < 2)
        {
            Quicksort(MyArray, min, p - 1);
        }
        else
        {
            Quicksort(MyArray, min, p - 1);
            Quicksort(MyArray, p + 1, max);
        }

    }

    private void ShowMyArray(string msg)
    {
        string outString;
        int[] MyArray = { 65, 25, 75, 15, 85, 35, 95, 67, 48, 66 };

        outString = "      {";
        for (int i = 0; i < MyArray.Length; i++)
        {
            outString += MyArray[i].ToString();
            if (i == MyArray.Length - 1)
            {
                outString += "}   ";
            }
            else
            {
                outString += ",   ";
            }
        }
        allOutString = allOutString + Environment.NewLine + outString;
        Console.WriteLine(outString, msg);
    }
}
}
C Sharp Guy
  • 1
  • 1
  • 2

1 Answers1

3

Although you don't show enough code to be sure, the line

Quicksort(MyArray, LBoundInteger, UBoundInteger);

is almost certainly within a static method.

You cannot call an instance method from a static method. You must create an instance of the object, and call the instance method using that object instance.

You can probably fix this by making Quicksort (and ShowMyArray, which has the same issue) static

private static void Quicksort(int[] list, int min, int max)
{
    // ...
}

private static void ShowMyArray(string msg)
{
    // ...
}

Alternatively, you could make the code that calls Quicksort an instance method.

Eric J.
  • 147,927
  • 63
  • 340
  • 553