Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Of course Im working on some code. I was given this Quicksort to use in my code by my instructor. Unfortunately he isnt available so I thought I would ask you...
In this code, which method do I call to main to complete the quicksort?
I have an easier one and have been comparing to find out and I figured it out in my other code but I cant in this one. I think its qs(T[] values). I thought it would be sort() but I run into problems when I call that method.


C#
public class QuickSort<T> where T : IComparable
    {
        T[] input;
        public T[] Output
        {
            get
            {
                return input;
            }
        }
        public void qs(T[] values)
        {
            input = new T[values.Length];
            for (int i = 0; i < values.Length; i++)
            {
                input[i] = values[i];
                Console.WriteLine(input[i]);
                Console.WriteLine();
            }
        }

        // Set up a call to the actual Quicksort method.
        public void Sort()
        {
            Sorting(0, input.Length - 1);
        }
        public int getPivotPoint(int begPoint, int endPoint)
        {
            int pivot = begPoint;
            int m = begPoint + 1;
            int n = endPoint;
            while ((m < endPoint) &&
                   (input[pivot].CompareTo(input[m]) >= 0))
            {
                m++;
            }
            while ((n > begPoint) &&
                   (input[pivot].CompareTo(input[n]) <= 0))
            {
                n--;
            }
            while (m < n)
            {
                T temp = input[m];
                input[m] = input[n];
                input[n] = temp;
                while ((m < endPoint) &&
                       (input[pivot].CompareTo(input[m]) >= 0))
                {
                    m++;
                }
                while ((n > begPoint) &&
                       (input[pivot].CompareTo(input[n]) <= 0))
                {
                    n--;
                }
            }
            if (pivot != n)
            {
                T temp2 = input[n];
                input[n] = input[pivot];
                input[pivot] = temp2;
            }
            return n;
        }
        public void Sorting(int beg, int end)
        {
            if (end == beg)
            {
                return;
            }
            else
            {
                int pivot = getPivotPoint(beg, end);
                if (pivot > beg)
                    Sorting(beg, pivot - 1);
                if (pivot < end)
                    Sorting(pivot + 1, end);
            }
        }
    }
}


Any help will be appreciated. Thanks
Posted

Hi,

you first have to call "qs" to fill the object with the array you want to be sorted. Then you call "Sort" to do the sorting and then you can call "Output" to get the sorted values.
 
Share this answer
 
You use this class like;
C#
QuickSort<int> sortObject = new QuickSort<int>();
sortObject.qs(new int[] {20,56,45,12 });
sortObject.Sort();
int[] test = sortObject.Output;
foreach (int value in test)
{
  Console.WriteLine(value);
}

the qs function loads the array of values then calling Sort actually sorts the array and you get the sorted array out with the Output property.
 
Share this answer
 
Comments
Tanacia 25-Nov-10 11:55am    
Rod Kemp, I would have never thought of that. I have another questions. Why not call everything in one method wouldn't that be simpler? This seems to be too complicated.
Rod Kemp 25-Nov-10 19:01pm    
Sure it would be simpler to put a call to Sorting(0, input.Length - 1); in the qs method, why it was done like it is can only be explained by your instructor.
Thanks guys. That was the last piece to my puzzle. It worked! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900