## Concept:

# Merge sort

## Headline

The Merge sort sorting algorithm

## Citation

(http://en.wikipedia.org/wiki/Merge_sort, 21 April 2013)

Conceptually, a merge sort works as follows

- Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
- Repeatedly merge sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list.

## Illustration

See the visualization of Merge sort on Wikipedia:

http://en.wikipedia.org/wiki/Merge_sort

See various illustrations of Merge sort as available on YouTube, e.g.:

### Recursive merge sort in Java

```
public static void mergeSort(int[] a) {
int[] temp = new int[a.length];
mergeSort(a, temp, 0, a.length - 1);
}
public static void mergeSort(int[] a, int[] temp, int min, int max) {
// Base case
if (!(min < max)) return;
// Split array and solve sub-problems recursively
int middle = (min + max) / 2;
mergeSort(a, temp, min, middle);
mergeSort(a, temp, middle + 1, max);
// Merge via temporary array
merge(a, temp, min, middle, max);
}
public static void merge(int[] a, int[] temp, int min, int middle, int max) {
int i = min; // loop over left half
int j = middle + 1; // loop over right half
int k = min; // loop over merged result
while (k <= max)
temp[k++] = (i <= middle && (j > max || a[i] < a[j])) ?
a[i++] // copy from left half
: a[j++]; // copy from right half
// Override array by merged tempory result
for (k = min; k <= max; k++)
a[k] = temp[k];
}
```

### Recursive merge sort in Haskell

```
-- Polymorphic sorting
sort :: Ord a => [a] -> [a]
sort [] = []
sort [x] = [x]
sort xs = merge (sort ys) (sort zs)
where
(ys,zs) = split xs
-- Split a list into halves
split :: [a] -> ([a],[a])
split xs = (take len xs, drop len xs)
where
len = length xs `div` 2
-- Merge sorted sublists
merge :: Ord a => [a] -> [a] -> [a]
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys) =
if x<=y
then x : merge xs (y:ys)
else y : merge (x:xs) ys
```

The main sorting function relies on helpers for splitting the input lists into halves and for merging sorted sublists. An empty list as much as a singleton lists are immediately sorted, as modeled by the first two equations of *sort*. The *split* helper uses list-processing goodies *take* and *drop* to extract two halves (+/- one element for a list of odd length). The *merge* help offers two base cases for a merge to trivially complete, if either of the two operands is an empty list; the recursive case compares the heads of both operands to decide on which of them goes first into the merged result.

The implementation is exercised as follows:

```
main = do
let input = [2,4,3,1,4]
print $ sort input -- [1,2,3,4,4]
```

## Metadata

### There are no revisions for this page.

## User contributions

## User edits

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**

#### 10 most similar pages:

- Concept:Insertion sort - 1.0593417
- Concept:Sorting_problem - 0.47517195
- Concept:Sorting problem - 0.4279449
- Concept:Binary search - 0.37527573
- Concept:Quicksort - 0.36216536
- Concept:Selection sort - 0.3397365
- Concept:Local_scope - 0.3095548
- Concept:Module - 0.26828417
- Concept:Recursion removal - 0.2511241
- Concept:Divide and conquer algorithm - 0.24789682

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert code snippets from
**
@worker.
**