C# program to merge two sorted arrays into one

Merging two sorted arrays in C# involves combining the elements of both arrays into a single array while maintaining the sorted order. This is a fundamental operation used in algorithms like merge sort and when combining datasets.

Approaches to Merge Sorted Arrays

There are two main approaches to merge sorted arrays −

  • Simple Concatenation: Append all elements from both arrays and then sort the result.

  • Merge Algorithm: Compare elements from both arrays and merge them in sorted order without additional sorting.

Using Simple Concatenation

The simplest approach is to combine both arrays and sort the merged result −

using System;

class Program {
    static void Main() {
        int[] arr1 = {5, 15, 25, 30, 47};
        int[] arr2 = {10, 20, 35, 40, 50};
        
        int[] merged = new int[arr1.Length + arr2.Length];
        int j = 0;
        
        // Copy first array
        for (int i = 0; i < arr1.Length; i++) {
            merged[j++] = arr1[i];
        }
        
        // Copy second array
        for (int i = 0; i < arr2.Length; i++) {
            merged[j++] = arr2[i];
        }
        
        // Sort the merged array
        Array.Sort(merged);
        
        Console.WriteLine("Merged sorted array:");
        foreach (int element in merged) {
            Console.Write(element + " ");
        }
    }
}

The output of the above code is −

Merged sorted array:
5 10 15 20 25 30 35 40 47 50 

Using Efficient Merge Algorithm

A more efficient approach compares elements from both arrays and merges them in sorted order without additional sorting −

Two-Pointer Merge Process 5 15 25 30 47 Array 1 10 20 35 40 50 Array 2 i j 5 10 15 20 25 30 35 40 47 50 Merged Array Compare arr1[i] and arr2[j] Add smaller element to merged array

using System;

class Program {
    static void Main() {
        int[] arr1 = {5, 15, 25, 30, 47};
        int[] arr2 = {10, 20, 35, 40, 50};
        
        int[] merged = MergeSortedArrays(arr1, arr2);
        
        Console.WriteLine("Merged sorted array:");
        foreach (int element in merged) {
            Console.Write(element + " ");
        }
    }
    
    static int[] MergeSortedArrays(int[] arr1, int[] arr2) {
        int[] merged = new int[arr1.Length + arr2.Length];
        int i = 0, j = 0, k = 0;
        
        // Compare and merge elements
        while (i < arr1.Length && j < arr2.Length) {
            if (arr1[i] <= arr2[j]) {
                merged[k++] = arr1[i++];
            } else {
                merged[k++] = arr2[j++];
            }
        }
        
        // Add remaining elements from arr1
        while (i < arr1.Length) {
            merged[k++] = arr1[i++];
        }
        
        // Add remaining elements from arr2
        while (j < arr2.Length) {
            merged[k++] = arr2[j++];
        }
        
        return merged;
    }
}

The output of the above code is −

Merged sorted array:
5 10 15 20 25 30 35 40 47 50 

Using LINQ for Merging

C# LINQ provides a concise way to merge and sort arrays −

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] arr1 = {5, 15, 25, 30, 47};
        int[] arr2 = {10, 20, 35, 40, 50};
        
        int[] merged = arr1.Concat(arr2).OrderBy(x => x).ToArray();
        
        Console.WriteLine("Merged sorted array using LINQ:");
        Console.WriteLine(string.Join(", ", merged));
    }
}

The output of the above code is −

Merged sorted array using LINQ:
5, 10, 15, 20, 25, 30, 35, 40, 47, 50

Comparison

Approach Time Complexity Space Complexity Best For
Simple Concatenation O((m+n) log(m+n)) O(m+n) Small arrays or when simplicity is preferred
Merge Algorithm O(m+n) O(m+n) Large arrays where efficiency matters
LINQ O((m+n) log(m+n)) O(m+n) Readable code and small to medium arrays

Conclusion

Merging sorted arrays can be accomplished through simple concatenation, efficient merge algorithms, or LINQ methods. The merge algorithm approach provides optimal O(m+n) time complexity, making it ideal for large datasets where performance is critical.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements