Merge Sort

πŸ“˜ Data Structure and Algorithm πŸ‘ 151 views πŸ“… Nov 05, 2025
⏱ Estimated reading time: 1 min

Merge Sort Algorithm

Merge Sort is a divide-and-conquer algorithm that splits an array into halves, sorts them recursively, and merges them back.

Algorithm Steps

  1. Divide the array into two halves.
  2. Recursively sort each half.
  3. Merge the sorted halves.

Example (C++)

void merge(int arr[], int l, int m, int r) {
    int n1 = m - l + 1, n2 = r - m;
    int L[n1], R[n2];
    for (int i = 0; i < n1; i++) L[i] = arr[l + i];
    for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2)
        arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
    while (i < n1) arr[k++] = L[i++];
    while (j < n2) arr[k++] = R[j++];
}

Time Complexity

  • All Cases: O(n log n)

Advantages

  • Stable and efficient for large data.

Disadvantages

  • Requires extra space for merging.

Conclusion

Merge Sort is efficient and consistent, especially suitable for large datasets that need stable sorting.


πŸ”’ Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes

πŸ€– AI Quizer Assistant

πŸ“ Quiz
πŸ“š Categories
πŸ† Leaderboard
πŸ“Š My Score
❓ Help
πŸ‘‹ Hi! I'm your AI quiz assistant for Quizer.in!

I can help you with:
β€’ πŸ“ Finding quizzes
β€’ πŸ† Checking leaderboard
β€’ πŸ“Š Your performance stats

Type 'help' to get started! πŸš€
AI is thinking...