Sorting algorithms/Merge sort: Difference between revisions

From Rosetta Code
Content added Content deleted
(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 is an entirely sorted group). Then merge the groups back together so that their elements are in order.
{{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 an array 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:
function mergesort(m)
function mergesort(m)
var list left, right, result
var list left, right, result

Revision as of 19:15, 15 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 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;
	}
}