finsters
finsters

Reputation: 177

calling merge sort c#

I wrote a merge sort program, but I have problems calling It from another class. I need help. For some reason after I enter the size and the max number a get a black screen in the output. I believe that the solution is pretty easy, but I can't find the solution by myself This is the class where It sorts the numbers

 class MergeSort
        {
            public int[] Sort(int[] unsortedSequence)
            {
                int[] left;
                int[] right;
                int[] result = new int[unsortedSequence.Length];
            if (unsortedSequence.Length <= 1)
                return unsortedSequence;

            int midPoint = unsortedSequence.Length / 2;
            left = new int[midPoint];
            if (unsortedSequence.Length % 2 == 0)
                right = new int[midPoint];
            else
                right = new int[midPoint + 1];
            for (int i = 0; i < midPoint; i++)
                left[i] = unsortedSequence[i];

            int x = 0;

            for (int i = midPoint; i < unsortedSequence.Length; i++)
            {
                right[x] = unsortedSequence[i];
                x++;
            }
            left = Sort(left);
            right = Sort(right);
            result = merge(left, right);
            return result;
        }

        public static int[] merge(int[] left, int[] right)
        {
            int resultLength = right.Length + left.Length;
            int[] result = new int[resultLength];
            int indexLeft = 0, indexRight = 0, indexResult = 0;
            while (indexLeft < left.Length || indexRight < right.Length)
            {
                if (indexLeft < left.Length && indexRight < right.Length)
                {
                    if (left[indexLeft] <= right[indexRight])
                    {
                        result[indexResult] = left[indexLeft];
                        indexLeft++;
                        indexResult++;
                    }
                    else
                    {
                        result[indexResult] = right[indexRight];
                        indexRight++;
                        indexResult++;
                    }
                }
                else if (indexLeft < left.Length)
                {
                    result[indexResult] = left[indexLeft];
                    indexLeft++;
                    indexResult++;
                }
                else if (indexRight < right.Length)
                {
                    result[indexResult] = right[indexRight];
                    indexRight++;
                    indexResult++;
                }
            }
            return result;
        }
    }

And this is the class where I'm trying to call the mergesort

class Program
    {
        static void Main(string[] args)
        {
            Console.Write("How Many Random Numbers Would you like to Generate : ");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.Write("What is the Maximum Random Number Would you like to Generate : ");
            int max = Convert.ToInt32(Console.ReadLine());
            Console.Clear();
            int[] unsortedSequence = generateRandomSequence(n, max);
               MergeSort mergeSortEngine = new MergeSort();
            int[] mergeSortedArray = mergeSortEngine.Sort(unsortedSequence);
            Console.Write("Output for Merge Sort: \n\n");
            OutputSequence(mergeSortedArray);
            Console.WriteLine("\n\nPress Any Key to Continue...");
            Console.ReadKey();
            Console.Clear();

Upvotes: 1

Views: 224

Answers (2)

Gabriel Ichim
Gabriel Ichim

Reputation: 21

It looks like you missing generateRandomSequence(n, max);

It might be like

public static int[] generateRandomSequence(int n, int max)
        {
            var rnd = new Random();
            int[] seq = new int[n];
            for (int ctr = 0; ctr < n; ctr++)
            {
                seq[ctr] = rnd.Next(1, max + 1);
            }
            return seq;
        }

Then, in Program/Test class after Console.Write("Output for Merge Sort: \n\n"); you can iterate with foreach loop to display the sorted array.

foreach (var item in mergeSortedArray)
                {
                    Console.WriteLine("{0}", item);
                }

                //OutputSequence(mergeSortedArray);

Upvotes: 0

ProgrammingLlama
ProgrammingLlama

Reputation: 38880

Because you didn't provide them, I wrote the missing generateRandomSequence() and OutputSequence methods in order to test your code and I can't reproduce your issue. Perhaps you should compare these to your own:

static int[] generateRandomSequence(int count, int max)
{
    Random rn = new Random();
    int[] seq = new int[count];
    for (int i = 0; i < count; ++i)
    {
        seq[i] = rn.Next(0, max + 1);
    }
    return seq;
}

static void OutputSequence(int[] array)
{
    for (int i = 0; i < array.Length; ++i)
    {
        if (i > 0)
        {
            Console.Write(", ");
        }
        Console.Write(array[i]);
    }
    Console.WriteLine();
}

Output from your code using the above methods:

Output

Upvotes: 2

Related Questions