Implementing Merge Sort Algorithm in Java

A merge sort works as follows:

  • Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
  • Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
Merge Sort Example
Merge Sort Example

An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged. (Via Wikipedia)

class Msa
static protected int a[];
public static void main(String args[])throws IOException
BufferedReader obj= new BufferedReader (new InputStreamReader (;
int n,low,mid,high;
System.out.println(“Enter number of elements:”);
a=new int[n];
System.out.println(“Enter elements”);
for (int i=0;i

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.