Merging two sorted arrays

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Merge

{

    class Merge

    {

        static void Main(string[] args)

        {

            //Program to merge two sorted arrays

            int[] arrayA = { 11, 22, 33, 44, 55 };

            int[] arrayB = { 10, 11, 12, 13, 14 };

            int i = 0, j = 0;

            for (; (i + j) < (arrayA.Length + arrayB.Length); )

            {

                if (arrayA[i] < arrayB[j])

                {

                    Console.Write(arrayA[i] + " ");

                    i++;

                    if (i == arrayA.Length)

                    {

                        for (; j < arrayB.Length; j++)

                        {

                            Console.Write(arrayB[j] + " ");

                        }

                    }

                }

                else

                {

                    Console.Write(arrayB[j] + " ");

                    j++;

                    if (j == arrayB.Length)

                    {

                        for (; i < arrayA.Length; i++)

                        {

                            Console.Write(arrayA[i] + " ");

                        }

                    }

                }

            }

            Console.WriteLine();

        }

    }

}

 

10 11 11 12 13 14 22 33 44 55
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s