[C# code] Mergesort - Using Mergesort algorithm to sort an array.

 The below code is an example which implements Mergesort algorithm. To understand the code, I putted some console methods to output the variables when the program is running.

final result:



  class Program
    {
        static void Main(string[] args)
        {
            int[] x = { 3, 2, 9, 1, 4, 10 };
            mergesort(x,new int[6],0,5);
            Console.WriteLine("The result:");
            foreach (int c in x)
                Console.WriteLine(c);

            Console.ReadKey();
        }
        static void mergesort(int[] values, int[] scratch, int start, int end)
        {
            if (start == end)
                return;

            int mid = (start + end)/2;

            Console.WriteLine("Begin function 1");

            mergesort(values, scratch, start, mid);
            for (int i = 0; i <= end; i++)
            {
                Console.WriteLine(values[i]);

            }
            Console.WriteLine("End function 1: start: " + start + " end: " + end + " mid: " + mid);

            Console.WriteLine("Begin function 2");

            mergesort(values, scratch, mid + 1, end);

            for (int i = 0; i <= end; i++)
            {
                Console.WriteLine(values[i]);
              
            }

            Console.WriteLine("End function 2 sort: start: " + start + " end: " + end + " mid: " + mid);

                merge(values, scratch,ref start,ref mid,ref end);

        }
        static void merge(int[] ina, int[] scratch,ref int start,ref int mid,ref int end)
        {

            int left = start;
            int right = mid + 1;
            int scratch_index = left;

            Console.WriteLine("begin merging and sorting here");
            Console.WriteLine("left: "+left + " right: "+right+ " scracth_index: "+ scratch_index);

            while ((left <= mid) && (right <= end))
            {
                if (ina[left] <= ina[right])
                {
                    scratch[scratch_index] = ina[left];
                    left = left + 1;
                }
                else
                {
                    scratch[scratch_index] = ina[right];
                    right = right + 1;

                }
                scratch_index = scratch_index + 1;
            }

            for (int i = left; i <= mid; i++)
            {
                scratch[scratch_index] = ina[i];
                scratch_index = scratch_index + 1;
            }
            for (int i = right; i <= end; i++)
            {
                scratch[scratch_index] = ina[i];
                scratch_index = scratch_index + 1;

            }

            for (int i = 0; i <= end; i++)
                ina[i] = scratch[i];

        }
    }
Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment