Help Save Code2care! 😢

I've lost 99% of my revenue to AdBlockers & AI. Your support could be the lifeline that keeps this passion project alive!

Buy Code2care a Coffee QR Code

Scan to Buy Me A Coffee and help me continue coding for you!

Implementing Merge Sort Algorithm in Java Program


Java Merge Sort Algo Example

Merge sort is considered to be highly reliable and efficient sorting algorithm. It is one of the most popular choices for many applications that need sorting logic.

Merge sort is also called stable sort as it preserves the relative order of equal elements in the input array.

package org.code2care.java.sorting.algo;

import java.util.Arrays;

public class MergeSortJavaExample {

    public static void main(String[] args) {

        //Unsorted Array
        int[] unsortedArray = {4, 99, 2, 11, 34, 67, 54, 12, 45, 245, 234, 12, 200};
        MergeSortJavaExample mergeSort = new MergeSortJavaExample();

        int[] sortedArray = mergeSort.mergeSortAlgorithm(unsortedArray);

        System.out.println(Arrays.toString(sortedArray));
    }

    public int[] mergeSortAlgorithm(int[] arrayOfNumbers) {

        int arrayLength = arrayOfNumbers.length;

        if (arrayLength == 1) {
            return arrayOfNumbers;
        }

        //Divide the array into two halves
        int midIndex = arrayLength / 2;
        int[] leftArray = Arrays.copyOfRange(arrayOfNumbers, 0, midIndex);
        int[] rightArray = Arrays.copyOfRange(arrayOfNumbers, midIndex, arrayLength);


        leftArray = mergeSortAlgorithm(leftArray);
        rightArray = mergeSortAlgorithm(rightArray);


        return mergeArrays(leftArray, rightArray);
    }

    public int[] mergeArrays(int[] leftArray, int[] rightArray) {

        int leftLength = leftArray.length;
        int rightLength = rightArray.length;
        int[] mergedArray = new int[leftLength + rightLength];

        int leftIndex = 0, rightIndex = 0, mergedIndex = 0;


        while (leftIndex < leftLength && rightIndex < rightLength) {
            if (leftArray[leftIndex] <= rightArray[rightIndex]) {
                mergedArray[mergedIndex] = leftArray[leftIndex];
                leftIndex++;
            } else {
                mergedArray[mergedIndex] = rightArray[rightIndex];
                rightIndex++;
            }
            mergedIndex++;
        }


        while (leftIndex < leftLength) {
            mergedArray[mergedIndex] = leftArray[leftIndex];
            leftIndex++;
            mergedIndex++;
        }
        
        while (rightIndex < rightLength) {
            mergedArray[mergedIndex] = rightArray[rightIndex];
            rightIndex++;
            mergedIndex++;
        }

        return mergedArray;
    }
}

Output:

[2, 4, 11, 12, 12, 34, 45, 54, 67, 99, 200, 234, 245]


Time Complexity
Best CaseAverage CaseWorst CaseSpace Complexity
Merge SortΩ(n log n)Θ(n log n)O(n log n)O(n)

Though merge sort does require additional memory compared to a few other sorting algorithms, it is still worth it as on the other hand it increases the speed and stability of the sort.

Merge sort is a good choice for many sorting applications.

You can download this article in various formats for your convenience. Choose from the options below:

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Code2care © 2024 | Privacy Policy | About Us | Contact Us | Search