Merge Sort in Java

Merge Sort in Java

0 Comments

In this post, I will explain the Merge Sort algorithm and how to use it in Java.

Sorting is the process of arranging data in ascending or descending order. Sorting becomes necessary while searching a particular record in database, a particular telephone number in telephone directory, words in a dictionary, and so on.

In computer applications, sorting is a common operation, and there are several efficient algorithms to sort data. One such popular sorting algorithm is Merge Sort.

The Merge Sort Algorithm

Merge sort is basically a Divide and Conquer algorithm to sort a given set of elements recursively and then merge them. This algorithm runs in O(n*log n) time in all cases, where n is the number of comparisons done.

Merge sort uses not-in-place sorting. In this type of sorting, the algorithm requires extra space for comparison and temporary storage of data elements. Therefore, for merge-sort, the required space is more than or equal to the elements being sorted. This is one of the drawback of Merge sort. However, with the cost of resources getting lesser, not-in-place sorting should not be a concern unless you are going for some extremely big data computation in an environment with limited resource.

To sort data, Merge Sort performs the following steps:

    1. It splits the items in the collection into two collections. To do so, it takes the middle of the collection and split the collection into its left and its right part, like this.

Merge Sort Step 1

    1. The resulting collections are again split recursively until they are broke to a single item in each collection.

Merge Sort Step 2
As you can see in this Figure, now we have single item in all the sub collections. At this point, we can say that, since each collection has only one item in it, everything is sorted.

  1. Next, Merge sort starts merging the collections. The step of merging involves comparing two sub collections, and then appending the larger one to the end of the smaller one. This goes on, until we end up with one single, sorted collection.

When we were dividing, we were doubling the number of collections until we reached our base case of one item per collection.

Now, we’re doing the exact opposite in order to build our collection up.

This figure shows the complete process of Merge sort.

Implementing Merge Sort

An example of implementing Merge sort in Java is this.

MergeSortExample.java

In this code, the mergeSort() method takes an array to sort as input. If the input array has a single element, there is no need of sorting and the method just returns back the input array.

If the array has more than one element, the method splits the array into two halves, and sorts the splitted arrays recursively. The code then calls the merge() method to merge the sorted array.

I’ve written a JUnit test to test the sorting function. If you are new to JUnit, I suggest going through my Junit series.

The MergeSortExampleTest class is this.

MergeSortExampleTest.java

The output on running the test in IntelliJ is this.
Test Output Merge Sort

Summary

Merge sort is not the only algorithm for sorting data. Other sorting algorithms includes insertion sort, bubble sort, quicksort, and heap sort.

I won’t go into the details of comparing them in this post. I will leave you with few pointers on when to use Merge sort.

  • Merge sort requires additional storage proportional to the number of records to be sorted. Therefore, if data is in a list-like structure and storage is available, merge sort is a superior algorithm.
  • Merge sort is very efficient for immutable data structures like linked lists since linked list elements are scattered throughout memory. Using Merge sort makes it easy for sorting as it makes only fewer total comparisons. Also, pointers can easily be changed when merging.
  • Merge sort is well suited in multithreaded programming. So if you have to sort very large data sets with possible parallelization, Merge sort is the algorithm to go for.
1

About jt

    You May Also Like

    Leave a Reply