Merge two array and store them into another array in sorted form






Merge two array and store them into another array in sorted form




public static void mergeArra(int[] arr, int[] arr2)
        {
            int[] NewArr = new int[arr.Length * 2];
            int k = 0;
                for (int i = 0; i <= 1; i++)
                {
                    if (i > 0)
                    {
                     arr = arr2;
                    }
                  
                    for (int j = 0; j <= arr.Length-1; j++)
                    {
                    NewArr[k] = arr[j];
                    k++;

                 }
            }
            Array.Sort(NewArr);
                foreach (int arr1 in NewArr)
                {
                    Console.Write(arr1 + "\t");
                }
               
        }
      
        static void Main(string[] args)
        {
            Console.WriteLine("Merge two sorted arrays into one");

            Console.WriteLine("Enter array Size");
            int arraysize = Convert.ToInt32(Console.ReadLine());
            int[] arr = new int[arraysize];
            int[] arr2 = new int[arraysize];

            Console.WriteLine("Enter the 1 array Value");
            for (int i = 0; i <= arraysize - 1; i++)
            {
                arr[i]= Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Enter the 2 array Value");
            for (int i = 0; i <= arraysize - 1; i++)
            {
                arr2[i] = Convert.ToInt32(Console.ReadLine());
            }
            Array.Sort(arr);
            Array.Sort(arr2);

            Program.mergeArra(arr,arr2);
            Console.ReadLine();
           
        }

Share:

No comments:

Post a Comment

Popular

Tags

Mobile

Recent Posts