0% found this document useful (0 votes)
38 views2 pages

Mergesort 1 Algorithm: LGN I I lgn1 I

Mergesort is a sorting algorithm that works by dividing an array into halves and recursively sorting the halves. It then merges the sorted halves back together. The best case number of comparisons is n and worst case is 2n-1. Mergesort runs in O(n log n) time because at each level of recursion the work done is n-1 comparisons and there are log n levels of recursion. Practical improvements include using insertion sort for small subarrays and stopping early if the array is already sorted.

Uploaded by

Andrew Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

Mergesort 1 Algorithm: LGN I I lgn1 I

Mergesort is a sorting algorithm that works by dividing an array into halves and recursively sorting the halves. It then merges the sorted halves back together. The best case number of comparisons is n and worst case is 2n-1. Mergesort runs in O(n log n) time because at each level of recursion the work done is n-1 comparisons and there are log n levels of recursion. Practical improvements include using insertion sort for small subarrays and stopping early if the array is already sorted.

Uploaded by

Andrew Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Mergesort

Algorithm
Input: array of n numbers, unsorted.
Assume: all elements distinct
Approach:

def MergeSort (A , p , r ):
if p < r :
q = ( p + r )/2
MergeSort (A , p , q )
MergeSort (A , q +1 , r )
merge (A , p , q , r )

def Merge (A , p , q , r )
C = [0] * ( r - p + 1)
i, j, k = p, q, 1

11

while i < q and j <= r :


if A [ i ] < A [ j ]:
C [ k ++] = A [ i ++]
else
C [ k ++] = A [ j ++]
Append leftovers and copy C -> A

13

15

17

Analysis

For the merge subroutine, the best-case number of comparisons for this
version is n, and the worst case is 2n 1 when given two lists of size n
to merge. There can be some practical improvements made, such as using
a binary search when one side to merge has size 1, but the worst case for
any merge algorithm will always be 2n 1 because there will always exist
at least 1 case such that the algorithm must iterate through all elements to
determine the ordering.
At each level of the algorithm, the merge subroutine merges two lists
of size n/2 each, so the work done at each level of the recurrence is n 1.
Because there are lg n levels in the recursive, tree, the number of comparisons
is (n lg n).
lg n
X
i=0

2i

2i

n 1
lgX
1 =
(n

2i )

(1)

n+1

(2)

i=0

= n lg n

Mergesort
3

Practical Improvements
Use insertion sort for small subarrays, at a cuto of 7. Eliminates
unecessary overhead, improves 20%.
Stop if already sorted: is the biggest item in first half smallest item
in second half?

Bottom-up Mergesort

Basic plan:
1. Pass through array, merging subarrays of size 1
2. Repeat for size 2, 4, 8, 16, etc. . .

You might also like