Recursive Merge Sort Implementation in Java

JavaJavaBeginner
Practice Now

Introduction

Merge Sort is an efficient and stable sorting algorithm that uses the Divide and Conquer Technique for sorting an array. In this lab, you will be learning how to implement Merge Sort in Java using a recursive approach.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/BasicSyntaxGroup -.-> java/while_loop("`While Loop`") java/DataStructuresGroup -.-> java/arrays_methods("`Arrays Methods`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/classes_objects -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/class_methods -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/modifiers -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/oop -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/packages_api -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/identifier -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/arrays -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/comments -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/data_types -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/for_loop -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/if_else -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/operators -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/output -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/strings -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/variables -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/while_loop -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/arrays_methods -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/object_methods -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} java/system_methods -.-> lab-117972{{"`Recursive Merge Sort Implementation in Java`"}} end

Create a Merge Sort method

Create a public static method named mergeSort() with three integer parameters, int arrToSort[], startIdx, endIdx. You need to check if the array length is less than 2, then return. Otherwise, calculate the middle index of the array and split the array into the left and right halves. The left and right halves should be recursively divided too. After dividing them, you have to merge them together to form a sorted array.

public static void mergeSort(int[] arrToSort, int startIdx, int endIdx) {
    if (startIdx >= endIdx) {
        return;
    }

    int midIdx = startIdx + (endIdx - startIdx) / 2;
    mergeSort(arrToSort, startIdx, midIdx);
    mergeSort(arrToSort, midIdx + 1, endIdx);
    merge(arrToSort, startIdx, midIdx, endIdx);
}

Create a Merge method

Create another public static method named merge() with four parameters: int arrToSort[], startIdx, midIdx, and endIdx. You need to initialize the left and right arrays. Then, you need to merge both arrays by comparing the elements of each array and merging them into a single array.

public static void merge(int[] arrToSort, int startIdx, int midIdx, int endIdx) {
    int[] leftArr = new int[midIdx - startIdx + 1];
    int[] rightArr = new int[endIdx - midIdx];

    for (int i = 0; i < leftArr.length; i++) {
        leftArr[i] = arrToSort[startIdx + i];
    }
    for (int i = 0; i < rightArr.length; i++) {
        rightArr[i] = arrToSort[midIdx + i + 1];
    }

    int leftArrIdx = 0, rightArrIdx = 0, sortedArrIdx = startIdx;
    while ((leftArrIdx < leftArr.length) && (rightArrIdx < rightArr.length)) {
        if (leftArr[leftArrIdx] < rightArr[rightArrIdx]) {
            arrToSort[sortedArrIdx] = leftArr[leftArrIdx];
            leftArrIdx += 1;
        } else {
            arrToSort[sortedArrIdx] = rightArr[rightArrIdx];
            rightArrIdx += 1;
        }
        sortedArrIdx += 1;
    }

    while (leftArrIdx < leftArr.length) {
        arrToSort[sortedArrIdx] = leftArr[leftArrIdx];
        leftArrIdx += 1;
        sortedArrIdx += 1;
    }

    while (rightArrIdx < rightArr.length) {
        arrToSort[sortedArrIdx] = rightArr[rightArrIdx];
        rightArrIdx += 1;
        sortedArrIdx += 1;
    }
}

Test Merge Sort

Let's test the mergeSort() method by writing code to sort an input array. Create a main method. Then, declare an integer array with a few unsorted values. Call the mergeSort() method and pass the unsorted array to it. Finally, print the sorted array to the console. The code block for this step will look like this:

public static void main(String[] args) {
    int[] arrToSort = { 4, 2, 8, 0, 1, 6 };
    mergeSort(arrToSort, 0, arrToSort.length - 1);
    System.out.println(Arrays.toString(arrToSort));
}

After running the program, the output should be: [0, 1, 2, 4, 6, 8].

Test with Empty Array

Create another test for an empty array. Declare an empty integer array. Call the mergeSort() method and pass it to the empty array. Finally, print the sorted array to the console. The code block for this step will look like this:

int[] emptyArr = {};
mergeSort(emptyArr, 0, emptyArr.length - 1);
System.out.println(Arrays.toString(emptyArr));

The output should be same as input.

Test with Single Element Array

Create one more test with a single element array. Declare a single-element integer array. Call the mergeSort() method and pass it to the single-element array. Finally, print the sorted array to the console. The code block for this step will look like this:

int[] singleElementArr = {7};
mergeSort(singleElementArr, 0, singleElementArr.length - 1);
System.out.println(Arrays.toString(singleElementArr));

The output should be same as input.

Test with Negative Values

Create another test for an array consisting of negative integers. Declare an integer array that includes some negative integers. Call the mergeSort() method and pass the array as an argument. Finally, print the sorted array to the console. The code block for this step will look like this:

int[] arrWithNegatives = {-7, 2, -9, 0, -1, 6, 8};
mergeSort(arrWithNegatives, 0, arrWithNegatives.length - 1);
System.out.println(Arrays.toString(arrWithNegatives));

After running the program, the output should be: [-9, -7, -1, 0, 2, 6, 8].

Test with Non-numeric Elements

Create another test by sorting an array of non-numerical elements. Declare an array of strings. Call the mergeSort() method and pass the array as an argument. Finally, print the sorted array to the console. The code block for this step will look like this:

String[] strsToSort = {"John", "David", "Emily", "Bob", "Christine"};
mergeSort(strsToSort, 0, strsToSort.length - 1);
System.out.println(Arrays.toString(strsToSort));

After running the program, the output should be: ['Bob', 'Christine', 'David', 'Emily', 'John'].

Final source code

Here is the final source code for Merge Sort:

import java.util.Arrays;

public class MergeSort {
   public static void mergeSort(int[] arrToSort, int startIdx, int endIdx) {
       if (startIdx >= endIdx) {
           return;
       }

       int midIdx = startIdx + (endIdx - startIdx) / 2;
       mergeSort(arrToSort, startIdx, midIdx);
       mergeSort(arrToSort, midIdx + 1, endIdx);
       merge(arrToSort, startIdx, midIdx, endIdx);
   }

   public static void merge(int[] arrToSort, int startIdx, int midIdx, int endIdx) {
       int[] leftArr = new int[midIdx - startIdx + 1];
       int[] rightArr = new int[endIdx - midIdx];

       for (int i = 0; i < leftArr.length; i++) {
           leftArr[i] = arrToSort[startIdx + i];
       }

       for (int i = 0; i < rightArr.length; i++) {
           rightArr[i] = arrToSort[midIdx + i + 1];
       }

       int leftArrIdx = 0, rightArrIdx = 0, sortedArrIdx = startIdx;
       while ((leftArrIdx < leftArr.length) && (rightArrIdx < rightArr.length)) {
           if (leftArr[leftArrIdx] < rightArr[rightArrIdx]) {
               arrToSort[sortedArrIdx] = leftArr[leftArrIdx];
               leftArrIdx += 1;
           } else {
               arrToSort[sortedArrIdx] = rightArr[rightArrIdx];
               rightArrIdx += 1;
           }
           sortedArrIdx += 1;
       }

       while (leftArrIdx < leftArr.length) {
           arrToSort[sortedArrIdx] = leftArr[leftArrIdx];
           leftArrIdx += 1;
           sortedArrIdx += 1;
       }

       while (rightArrIdx < rightArr.length) {
           arrToSort[sortedArrIdx] = rightArr[rightArrIdx];
           rightArrIdx += 1;
           sortedArrIdx += 1;
       }
   }

   public static void main(String[] args) {
       //create an unsorted integer array
       int[] arrToSort = { 4, 2, 8, 0, 1, 6 };
       //sort the integer array using Merge Sort
       mergeSort(arrToSort, 0, arrToSort.length - 1);
       //print the sorted array
       System.out.println(Arrays.toString(arrToSort));

       //create an empty integer array
       int[] emptyArr = {};
       //sort the empty array
       mergeSort(emptyArr, 0, emptyArr.length - 1);
       //print the sorted array
       System.out.println(Arrays.toString(emptyArr));

       //create a single-element array
       int[] singleElementArr = {7};
       //sort the single-element array
       mergeSort(singleElementArr, 0, singleElementArr.length - 1);
       //print the sorted array
       System.out.println(Arrays.toString(singleElementArr));

       //create a negative integer array
       int[] arrWithNegatives = {-7, 2, -9, 0, -1, 6, 8};
       //sort the negative integer array
       mergeSort(arrWithNegatives, 0, arrWithNegatives.length - 1);
       //print the sorted array
       System.out.println(Arrays.toString(arrWithNegatives));

       //create a string array
       String[] strsToSort = {"John", "David", "Emily", "Bob", "Christine"};
       //sort the string array
       mergeSort(strsToSort, 0, strsToSort.length - 1);
       //print the sorted array
       System.out.println(Arrays.toString(strsToSort));
   }
}

Run the program

Compile and run the program using the terminal:

javac MergeSort.java && java MergeSort

Summary

In this lab, you learned what is Merge Sort, how it works and how to implement it in Java. You created two public static methods, mergeSort() and merge() to divide and merge the input array respectively. After the implementation, you tested it on various inputs, discussed the time and space complexity, and verified its functionality. Congratulations! You have successfully completed the Merge Sort lab in Java.

Other Java Tutorials you may like