Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Mergesort in Java

Lars Vogel

Version 1.1

08.02.2011

Revision History
Revision 0.117.05.2009Lars Vogel
Created
Revision 0.2 - 1.106.09.2009 - 2011Lars Vogel
bugfixes and enhancements

Mergesort with Java

This article describes how to implement Mergesort with Java.


Table of Contents

1. Mergesort
2. Mergesort in Java
2.1. Implementation
2.2. Test
3. Complexity Analysis
4. Thank you
5. Questions and Discussion
6. Links and Literature
6.1. Source Code
6.2. General

1. Mergesort

Mergesort is a divide and conquer algorithm. The sorting elements are stored in a collection. This collection is divided into two collections and these are again sorted via mergesort. Once the two collections are sorted then the result is combined

Mergesort will take the middle of the collection and takes then the two collection for the next iteration of mergesort. In the merging part mergesort runs through the both collections and selects the lowest of the both to insert it into a new collection.

In comparison to quicksort the divide part is simple in mergesort while the merging take is complex. In addition quicksort can work "inline", e.g. it does not have to create a copy of the collection while mergesort requires a copy.