Mergesort in java

Mergesort is a divide and conquer algorithm. It works by breaking an array into sub-arrays and then recursively sorting the sub-arrays.

Steps for Mergesort

  1. Divide the list with n elements into n sublists with each sublist having 1 element. We will divide list into 2 sublists by dividing in the middle and then recursively dividing the 2 sublists from the middle until we are left with sublists with all of them having 1 element.
  2. Merge all the sublists to produce sorted sublists and then merge the already merged sublists. Do that until only one list is created and no more merging can be performed.

Mergesort example

Here is an animation of the mergesort

Mergesort Animation

Mergesort Animation

Mergesort Java implementation

Here is a java implementation. I have added a lot of logging so that from the output all the steps become clear.
package com.programtalk.mergesort.example;

import java.util.Arrays;

public class MergerSort {
	
	
	public static void main(String[] args) {
		int[] array4Sort = { 8,5,6,7,2,1,3,4};
		System.out.println("To be sorted array: " + Arrays.toString(array4Sort));
		sort(array4Sort);
		System.out.println("Sorted array: " +  Arrays.toString(array4Sort));
	}

	
	
	public static void sort(int[] array4Sort) {
		int[] emptyArray = new int[array4Sort.length];
		int rightEndIndex = array4Sort.length - 1;
		int leftStartIndex = 0;
		recusrsiveMergeSort(array4Sort, emptyArray, leftStartIndex, rightEndIndex);
	}

	private static void recusrsiveMergeSort(int[] array4Sort, int[] tmpArray, int leftIndex, int rightIndex) {
		if (rightIndex <= leftIndex) {
			return;
		}

		int center = (rightIndex + leftIndex) / 2;
		recusrsiveMergeSort(array4Sort, tmpArray, leftIndex, center);
		recusrsiveMergeSort(array4Sort, tmpArray, center + 1, rightIndex);
		mergeLeftAndRight(array4Sort, tmpArray, leftIndex, center + 1, rightIndex);
	}

	private static void mergeLeftAndRight(int[] array2Merge, int[] tmpArrayUsed4Merge, int leftStartIndex, int rightStartIndex,
			int rightEndIndex) {

		System.out.println(
				"left array: " + Arrays.toString(Arrays.copyOfRange(array2Merge, leftStartIndex, rightStartIndex)));
		System.out.println(
				"right array: " + Arrays.toString(Arrays.copyOfRange(array2Merge, rightStartIndex, rightEndIndex + 1)));
		
		int leftEndIndex = rightStartIndex - 1;
		int tmpArray4MergeIndex = leftStartIndex;
		final int startIndexForMergedArray = leftStartIndex;

		for (;leftStartIndex <= leftEndIndex && rightStartIndex <= rightEndIndex; tmpArray4MergeIndex++) {
			// the lower value element goes to the merged array
			if (array2Merge[leftStartIndex] < (array2Merge[rightStartIndex]))
				tmpArrayUsed4Merge[tmpArray4MergeIndex] = array2Merge[leftStartIndex++];
			else
				tmpArrayUsed4Merge[tmpArray4MergeIndex] = array2Merge[rightStartIndex++];
		}
		
		while (leftStartIndex <= leftEndIndex) {
			tmpArrayUsed4Merge[tmpArray4MergeIndex++] = array2Merge[leftStartIndex++];
		}
		while (rightStartIndex <= rightEndIndex) {
			tmpArrayUsed4Merge[tmpArray4MergeIndex++] = array2Merge[rightStartIndex++];
		}

		System.out.println("Temporary array after merging:" + Arrays.toString(tmpArrayUsed4Merge));
		int length = rightEndIndex - startIndexForMergedArray +1;
		System.arraycopy(tmpArrayUsed4Merge, startIndexForMergedArray, array2Merge, startIndexForMergedArray, length);

	}

}

Output:

To be sorted array: [8, 5, 6, 7, 2, 1, 3, 4]
left array: [8]
right array: [5]
Temporary array after merging:[5, 8, 0, 0, 0, 0, 0, 0]
left array: [6]
right array: [7]
Temporary array after merging:[5, 8, 6, 7, 0, 0, 0, 0]
left array: [5, 8]
right array: [6, 7]
Temporary array after merging:[5, 6, 7, 8, 0, 0, 0, 0]
left array: [2]
right array: [1]
Temporary array after merging:[5, 6, 7, 8, 1, 2, 0, 0]
left array: [3]
right array: [4]
Temporary array after merging:[5, 6, 7, 8, 1, 2, 3, 4]
left array: [1, 2]
right array: [3, 4]
Temporary array after merging:[5, 6, 7, 8, 1, 2, 3, 4]
left array: [5, 6, 7, 8]
right array: [1, 2, 3, 4]
Temporary array after merging:[1, 2, 3, 4, 5, 6, 7, 8]
Sorted array: [1, 2, 3, 4, 5, 6, 7, 8]
Like this post? Don’t forget to share it!