Mergesort in Java
2014-04-11 17:09
295 查看
1. Mergesort
1.1. Overview
The Mergesort algorithm can be used to sort a collection of objects. Mergesort is so calleddivideand conquer algorithm. Divide and conquer algorithms divide the original data into smaller sets of data to solve the problem.
Mergesort sorts in worst case in O(n log n) time. Due to the required copying of the collection Mergesort is in the average case
slower than Quicksort.
http://www.vogella.com/tutorials/JavaAlgorithmsQuicksort/article.html
The
sortoperation in Arrays.sort() uses a slightly optimized merge sort algorithm that is fast and stable:
Fast: It is guaranteed to run in
n log(n)time and runs substantially faster on nearly sorted lists. Empirical tests showed it to be as fast as a highly optimized quicksort. A quicksort is generally considered to be faster
than a merge sort but isn't stable and doesn't guarantee
n log(n)performance.
Stable: It doesn't reorder equal elements. This is important if you sort the same list repeatedly on different attributes. If a user of a mail program sorts the inbox by mailing date and then sorts it by sender, the user naturally expects
that the now-contiguous list of messages from a given sender will (still) be sorted by mailing date. This is guaranteed only if the second sort was stable.
1.2. Mergesort
During the Mergesort process the object in the collection are divided into two collections. To split a collection, Mergesort willtake the middle of the collection and split the collection into its left and its right part.
The resulting collections are again recursively sorted via the Mergesort algorithm.
Once the sorting process of the two collections is finished, the result of the two collections is combined. To combine both collections Mergesort start
at each collection at the beginning. It pick the object which is smaller and inserts this object into the new collection. For this collection it now selects the next elements and selects the smaller element from both collection.
Once all elements from both collections have been inserted in the new collection, Mergesorthas successfully sorted the collection.
To avoid the creation of too many collections, typically one new collection is created and the left and right side are treated as different collections.
1.3. Comparison with Quicksort
In comparison tohttp://www.vogella.com/tutorials/JavaAlgorithmsQuicksort/article.html Quicksort thedivide part is simple in Mergesort while the merging take is complex.
Quicksort can sort "inline" of an existing collection, e.g. it does not have to create a copy of the collection while Mergesort requires
a copy.
2. Mergesort in Java
2.1. Implementation
Create a Java project called de.vogella.algorithms.sort.mergesort.Create the following program.
package de.vogella.algorithms.sort.mergesort; public class Mergesort { private int[] numbers; private int[] helper; private int number; public void sort(int[] values) { this.numbers = values; number = values.length; this.helper = new int[number]; mergesort(0, number - 1); } private void mergesort(int low, int high) { // check if low is smaller then high, if not then the array is sorted if (low < high) { // Get the index of the element which is in the middle int middle = low + (high - low) / 2; // Sort the left side of the array mergesort(low, middle); // Sort the right side of the array mergesort(middle + 1, high); // Combine them both merge(low, middle, high); } } private void merge(int low, int middle, int high) { // Copy both parts into the helper array for (int i = low; i <= high; i++) { helper[i] = numbers[i]; } int i = low; int j = middle + 1; int k = low; // Copy the smallest values from either the left or the right side back // to the original array while (i <= middle && j <= high) { if (helper[i] <= helper[j]) { numbers[k] = helper[i]; i++; } else { numbers[k] = helper[j]; j++; } k++; } // Copy the rest of the left side of the array into the target array while (i <= middle) { numbers[k] = helper[i]; k++; i++; } } }
相关文章推荐
- Program work 12. Merge Sort in Java
- 归并排序(Mergesort)之Java实现
- 【Leetcode】Sort Colors in JAVA
- An Implementation of Merge Sort in C
- How to merge cells ( or apply colspan) using XWPFTable in POI in Java?
- mergesort in python
- Java 归并排序(MergeSort)
- Java实现归并排序(Merge-Sort)算法
- How Developers Sort in Java?
- Program work 17. Selection Sort in Java
- in place merge sort (分析)
- [算法]MergeSort的Java实现
- Some sort in java
- mergeSort java实现
- In-place Merge Sort (原地归并排序)
- 原地归并排序(In-place merge sort)
- bubble sort in linked list....(java version)
- In-place Merge Sort (原地归并排序)
- MergeSort(归并排序)算法Java实现
- Java 归并排序(MergeSort)