Have you ever felt like you were drowning in a sea of numbers? Do you want to transform that chaos into clarity with just a sprinkle of coding magic? If you’re eager to learn how to sort arrays like a pro, then Merge Sort is your ticket to success! This blog will guide you through the fascinating world of Merge Sort, breaking it down into bite-sized pieces that are easy to digest.

What Is Merge Sort?

9f4b17a8 9f0a 415b bce7 9b9b035872fe

At its core, Merge Sort is a divide-and-conquer algorithm. It breaks down large problems into smaller, more manageable parts and then combines those solutions. Think of it as organizing a messy room: you start by sorting items into smaller piles before putting everything in its rightful place.

Why Merge Sort Matters

Curious to Know How It Works?

Let’s embark on this sorting journey! Here’s how Merge Sort operates, step by step:

Step 1: Divide

The first step is to split the array into halves until you’re left with subarrays containing a single element. Why single elements? Because they are inherently sorted!

Step 2: Conquer

Once you have these tiny sorted pieces, it’s time to merge them back together. This is where the real magic happens!

Step 3: Combine

Keep merging the sorted subarrays until you rebuild the entire array in the correct order.

Given an array of size n, sort the array using Merge Sort.

Examples:

Example 1:
Input: N=5, arr[]={4,2,1,6,7}
Output: 1,2,4,6,7,


Example 2:
Input: N=7,arr[]={3,2,8,5,1,4,23}
Output: 1,2,3,4,5,8,23

Solution :

Intuition: 

  1. Merge Sort is a divide and conquers algorithm, it divides the given array into equal parts and then merges the 2 sorted parts. 
  2. There are 2 main functions :
    merge(): This function is used to merge the 2 halves of the array. It assumes that both parts of the array are sorted and merges both of them.
    mergeSort(): This function divides the array into 2 parts. low to mid and mid+1 to high where,
 low = leftmost index of the array
 high = rightmost index of the array
 mid = Middle index of the array
  1. We recursively split the array, and go from top-down until all sub-arrays size becomes 1.

Approach : 

Approach:

Pseudocode:

Screenshot 2023 03 05 162634

The pseudo code will look like the following:

Screenshot 2023 03 05 162932

The red number shows the order of the steps in recursion calls.

Code:

Code:C++Java

#include <bits/stdc++.h>
using namespace std;

void merge(vector<int> &arr, int low, int mid, int high) {
    vector<int> temp; // temporary array
    int left = low;      // starting index of left half of arr
    int right = mid + 1;   // starting index of right half of arr

    //storing elements in the temporary array in a sorted manner//

    while (left <= mid && right <= high) {
        if (arr[left] <= arr[right]) {
            temp.push_back(arr[left]);
            left++;
        }
        else {
            temp.push_back(arr[right]);
            right++;
        }
    }

    // if elements on the left half are still left //

    while (left <= mid) {
        temp.push_back(arr[left]);
        left++;
    }

    //  if elements on the right half are still left //
    while (right <= high) {
        temp.push_back(arr[right]);
        right++;
    }

    // transfering all elements from temporary to arr //
    for (int i = low; i <= high; i++) {
        arr[i] = temp[i - low];
    }
}

void mergeSort(vector<int> &arr, int low, int high) {
    if (low >= high) return;
    int mid = (low + high) / 2 ;
    mergeSort(arr, low, mid);  // left half
    mergeSort(arr, mid + 1, high); // right half
    merge(arr, low, mid, high);  // merging sorted halves
}

int main() {

    vector<int> arr = {9, 4, 7, 6, 3, 1, 5}  ;
    int n = 7;

    cout << "Before Sorting Array: " << endl;
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " "  ;
    }
    cout << endl;
    mergeSort(arr, 0, n - 1);
    cout << "After Sorting Array: " << endl;
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " "  ;
    }
    cout << endl;
    return 0 ;
}

Output:

Before Sorting Array:
9 4 7 6 3 1 5
After Sorting Array:
1 3 4 5 6 7 9

Time complexity: O(nlogn) 

Reason: At each step, we divide the whole array, for that logn and we assume n steps are taken to get sorted array, so overall time complexity will be nlogn

Space complexity: O(n)  

Reason: We are using a temporary array to store elements in sorted order.

Auxiliary Space Complexity: O(n)

Code Breakdown

Real-World Applications of Merge Sort

Merge Sort isn’t just an academic exercise; it has real-world applications:

Tips for Implementing Merge Sort

  1. Start Small: Begin with small arrays to get comfortable with the process.
  2. Visualize It: Draw diagrams to understand how arrays are split and merged.
  3. Practice: The more you code, the better you’ll grasp the concepts.

Final Thoughts

Merge Sort might not be the flashiest sorting algorithm, but its efficiency and reliability make it a favorite among developers. By mastering this algorithm, you’ll equip yourself with the skills to handle sorting challenges with ease.

So, are you ready to sort your way to success?

Dive into the code, experiment, and watch the magic of Merge Sort unfold!

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *