[c# code] Mergesoft Algorithm - Sorting two sorted set!



The below example is the main technique of mergesort algorithm ( ie sorting two sorted set! )

Given, two sorted lists as below, write the resulting sorted list using a MERGE SORT technique.

List 1  :  {3,4,8,10,14,15,29,31}
List 2  : {5,7,9,11,12,13}
 
My result:


     class Program
    {
        static void Main(string[] args)
        {
            int[] x ={3,4,8,10,14,15,29,31};
            int[] y= {5,7,9,11,12,13};
            int[] r=pre_mergesoft(x,y,x.Length,y.Length);
            foreach (int c in r)
                System.Console.WriteLine(c);

            Console.ReadKey();
        }

        static int[] pre_mergesoft(int[] a, int[] b, int lengtha, int lengthb)
        {
            int index1= 0;
            int index2 = 0;
            int total=lengtha+lengthb;
            int count=0;
            int[] result=new int[total];
            while((index1<lengtha) && index2<lengthb)
            {
              
                if ((a[index1] <= b[index2]))
                {
                 
                    result[count] = a[index1];
                    index1 = index1 + 1;
                  
                  
                }
                else
                {
                 
                    result[count] = b[index2];
                    index2 = index2 + 1;
                  
                  
                }
                count = count + 1;
              


            }

            if (index1 == (lengtha))
                for (int i = index2; i < lengthb; i++)
                {
                    result[count] = b[i];
                    count = count + 1;
                }
            if(index2==(lengthb))
                for (int i = index1; i < lengtha; i++)
                {
                    result[count] = a[i];
                    count = count + 1;
                }


                    return result;

        }
    }
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.

1 comments:

  1. Find the best games for gambling | DrmCD
    Learn 의왕 출장마사지 the best casino 진주 출장샵 games 의정부 출장샵 for your gambling, from slots to table games 의정부 출장마사지 to bingo. DrmCD provides comprehensive information on all casino games, 천안 출장샵 including the

    ReplyDelete