Sorting algorithms/Merge sort: Difference between revisions
(Added Java example) |
m (Fixed up the description a bit (allowing for all kinds of collections)) |
||
Line 1: | Line 1: | ||
{{task}}{{Sorting Algorithm}}[[Category:Recursion]]The merge sort is an order n*log(n) (technically n*lg(n)--lg is log base 2) sort. The basic idea is to split the collection into smaller groups by halving it until the groups only have one element (which |
{{task}}{{Sorting Algorithm}}[[Category:Recursion]]The merge sort is an order n*log(n) (technically n*lg(n)--lg is log base 2) sort. The basic idea is to split the collection into smaller groups by halving it until the groups only have one element or no elements (which are both entirely sorted groups). Then merge the groups back together so that their elements are in order. |
||
Write a function to sort |
Write a function to sort a collection of integers using the merge sort. The merge sort algorithm comes in two parts: a sort function and a merge function. The functions in pseudocode look like this: |
||
function mergesort(m) |
function mergesort(m) |
||
var list left, right, result |
var list left, right, result |
Revision as of 19:15, 15 February 2008
You are encouraged to solve this task according to the task description, using any language you may know.
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
Heap sort | Merge sort | Patience sort | Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort
The merge sort is an order n*log(n) (technically n*lg(n)--lg is log base 2) sort. The basic idea is to split the collection into smaller groups by halving it until the groups only have one element or no elements (which are both entirely sorted groups). Then merge the groups back together so that their elements are in order.
Write a function to sort a collection of integers using the merge sort. The merge sort algorithm comes in two parts: a sort function and a merge function. The functions in pseudocode look like this:
function mergesort(m) var list left, right, result if length(m) ≤ 1 return m else var middle = length(m) / 2 for each x in m up to middle - 1 add x to left for each x in m at and after middle add x to right left = mergesort(left) right = mergesort(right) result = merge(left, right) return result
function merge(left,right) var list result while length(left) > 0 and length(right) > 0 if first(left) ≤ first(right) append first(left) to result left = rest(left) else append first(right) to result right = rest(right) if length(left) > 0 append rest(left) to result if length(right) > 0 append rest(right) to result return result
Java
import java.util.List; import java.util.LinkedList; public class Merge{ public static LinkedList<Integer> mergeSort(LinkedList<Integer> m){ LinkedList<Integer> left = new LinkedList<Integer>(); LinkedList<Integer> right = new LinkedList<Integer>(); LinkedList<Integer> result = new LinkedList<Integer>(); if (m.size() <= 1) return m; int middle = m.size() / 2; for(int i = 0; i < middle; i++) left.add(m.get(i)); for(int i = middle; i < m.size(); i++) right.add(m.get(i)); right = mergeSort(right); left = mergeSort(left); result = merge(left, right); return result; } public static LinkedList<Integer> merge(LinkedList<Integer> left, LinkedList<Integer> right){ LinkedList<Integer> result = new LinkedList<Integer>(); while (left.size() > 0 && right.size() > 0) if (left.peek() <= right.peek()) result.add(left.remove()); else result.add(right.remove()); if (left.size() > 0) result.addAll(left); if (right.size() > 0) result.addAll(right); return result; } }