DEV Community

Khadija Ashraf
Khadija Ashraf

Posted on

Data Structure & Algorithm: Merge Sort

Recursive Algorithm:

  • Step 1: find the mid index of the array
  • Step 2: go for sorting left half
  • Step 3: go for sorting right half
  • Step 4: merge the left and right half, sorting is done during merging

Source Code Repository: https://github.com/khadija-ashraf/data-structure-algorithm/blob/main/src/com/sorting/MergeSort.java

If we analyze the order of the steps in this recursive algorithm then, we see that, we keep slicing the array until no more slicing can be done, follow steps 1, 2, 3. No sorting is done in this slicing period. Only when the sliced array reaches to zero or 1 elements then we return from slicing and starting merging in the step 4.

Since the the Step 2 executes before the Step 3 therefore the left part of the array will be sliced until it reaches to 1-element arrays, then the sort-and-merge will execute on the left side of the recursive tree, upon return from the left subtree the right subtree sort-and-merge will be executed.


public class MergeSort {
    public void sort(int[] arr, int left, int right) {
        if(left < right) {
            int midIndex = left + (right - left) / 2;

            sort(arr, left, midIndex);
            sort(arr, midIndex+1, right);

            merge(arr, left, midIndex, right);
        }
    }

    public void merge(int[] arr, int left, int midIndex, int right) {
        int leftArraySize = midIndex - left + 1;
        int rightArraySize = right - midIndex;

        int[] leftTempArr = new int[leftArraySize];
        int[] rightTempArr = new int[rightArraySize];

        for(int i = 0; i < leftArraySize; i++) {
            leftTempArr[i] = arr[left + i];
        }

        for(int i = 0; i < rightArraySize; i++) {
            rightTempArr[i] = arr[midIndex + 1 + i];
        }

        // merge

        int i = 0; 
        int j = 0; 
        int k = left;

        while(i < leftArraySize && j < rightArraySize) {
            if(leftTempArr[i] <= rightTempArr[j]) {
                arr[k] = leftTempArr[i];
                i++;
            } else {
                arr[k] = rightTempArr[j];
                j++;
            }
            k++;
        }

        while(i < leftArraySize) {
            arr[k] = leftTempArr[i];
            i++;
            k++;
        }

        while(j < rightArraySize) {
            arr[k] = rightTempArr[j];
            j++;
            k++;
        }

    }

    public void print(int[] arr) {
        for(int n: arr) {
            System.out.print(n + ", ");
        }
    }

    public static void main(String a[]) {
        MergeSort ob = new MergeSort();

        int[] arr = {4,7,1,8,3,8,9};

        ob.sort(arr, 0, arr.length - 1);

        ob.print(arr);
    }
}
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay