Sorting algorithms/Merge sort: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Java}}: Added works with template)
m (Added to <5 category)
Line 1: Line 1:
{{task}}{{Sorting Algorithm}}[[Category:Recursion]]The '''merge sort''' is a recursive sort of order n*log(n) (technically n*lg(n)--lg is log base 2) sort. It is notable for having no worst case. It is ''always'' ''O(n*log(n))''. 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. This is how the algorithm gets is "divide and conquer" description.
[[Category:Less Than 5 Examples]]{{task}}{{Sorting Algorithm}}[[Category:Recursion]]The '''merge sort''' is a recursive sort of order n*log(n) (technically n*lg(n)--lg is log base 2) sort. It is notable for having no worst case. It is ''always'' ''O(n*log(n))''. 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. This is how the algorithm gets is "divide and conquer" description.


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:
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:

Revision as of 15:42, 22 February 2008

Task
Sorting algorithms/Merge sort
You are encouraged to solve this task according to the task description, using any language you may know.

The merge sort is a recursive sort of order n*log(n) (technically n*lg(n)--lg is log base 2) sort. It is notable for having no worst case. It is always O(n*log(n)). 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. This is how the algorithm gets is "divide and conquer" description.

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

Works with: Java version 1.5+
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){
			//change the direction of this comparison to change the direction of the sort
			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;
	}
}