Sie sind auf Seite 1von 22

By –

Aneeq Ul Waseeq
H.B Chaitanya Bharadwaj
Jayaprakash
Naziya
Pratyush
Sowjanya
History of the Merge Sort
 Written around 1945
 Commonly attributed to renown
Hungarian-American mathematician
John von Neumann
 One of the first sorting styles proposed
for computers
 Uses divide and conquer principles
(recursively breaking down a problem
into smaller sub-problems)
So what’s happening exactly?
 The data is recursively broken down into
smallest possible subset (in this case, an
individual number)
 The first item in one subset is compared to
the first in the next subset
 The smaller of the two is placed first in a
new set, then the second smallest etc.
 Increasingly larger sorted sets are formed
until the final, fully sorted list is made by
comparing one sorted half of the data to
the other sorted half
Merge Sorts and Binary Trees
 The merge sort
algorithm can be
visualized using a
binary tree, where:
 Each leaf node is one
piece of data
 Going up one level
involves one merge
process
Image courtesy of Rashid Bin Muhammad at Kent State
University
Memory Requirements
 Array implementation
 Generally requires a second array to store the
merged list
 O(n) extra space
 Linked list implementation
 Can be done in place by changing list pointers
 O(log n) extra space [for recursion]
 Recursive nature of algorithm requires
extra memory compared to a non-recursive
algorithm
When should you use merge sort?
 For linked lists
 When wanting to avoid random access
to data
 If stability is desired
 When using extra space is not a
concern
Divide-and-Conquer
 Divide and Conquer is more than just a military strategy; it is
also a method of algorithm design that has created such
efficient algorithms as Merge Sort.
 In terms or algorithms, this method has three distinct steps:
 Divide: If the input size is too large to deal with in a
straightforward manner, divide the data into two or more
disjoint subsets.
 Recur: Use divide and conquer to solve the sub problems
associated with the data subsets.
 Conquer: Take the solutions to the sub problems and
“merge” these solutions into a solution for the original
problem.
7
Merge-Sort Algorithm
 Divide: If S has at leas two elements (nothing
needs to be done if S has zero or one elements),
remove all the elements from S and put them into
two sequences, S1 and S2, each containing about
half of the elements of S. (i.e. S1 contains the first
n/2 elements and S2 contains the remaining n/2
elements.
 Recur: Recursive sort sequences S1 and S2.
 Conquer: Put back the elements into S by merging
the sorted sequences S1 and S2 into a unique
sorted sequence.
8
Algorithm -
Algorithm mergesort(a[0,….n-1) A[k]B[i]
{ ii+1
if(n>1) then }
copy n[0,…(n/2)] to B[0,…(n/2)] else
copy n[(n/2)+1,...n-1 ] to C[0 …(n/2)] A[k]C[j]
mergesort(B[0…(n/2)]) jj+1
mergesort(C[0…(n/2)]) end if
merge(B,C,A) kk+1
end if end while
if(i<p)
Algorithm merge(B[0…p-1],C[o…q-1],A[0…p+q-1]) copy B[i…p-1] to A[k…p+q-1]
{
else
i0,j0,k0 copy C[j…q-1] to A[k…p+q-1]
while(i<p and j<q) end if
if(B[i]<C[j]) then
{
Merge sort Analysis
 Let T(N) be the running time for an array
of N elements
 Merge sort divides array in half and calls
itself on the two halves. After returning, it
merges both halves using a temporary
array
 Each recursive call takes T(N/2) and
merging takes O(N)

10
Merge sort Recurrence Relation
 The recurrence relation for T(N) is:
 T(1) < a
○ base case: 1 element array  constant time
 T(N) < 2T(N/2) + bN
○ Sorting N elements takes
 the time to sort the left half
 plus the time to sort the right half
 plus an O(N) time to merge the two halves
 T(N) = O(n log n) (see Lecture 5 Slide17)

11
Time Complexity
 Best case: O(n)
 When values are already sorted
 Worst case: O(n*log n)
 n operations / level * log n + 1 levels
 When the data is unsorted/randomly
arranged
 Average case: O(n*log n)
 The only stable O(n*log n ) sorting
algorithm
0, n=1 …….
C(n) = < =2^k*C(2^(k-k)) + K*2^k – 2^(k-1)
C(n/2)+C(n/2)+n+1, n>1 – 2^(k-2) ……. -2^1 – 2^0
=k*2^k – [2^(k-1) – 2^(k-2) …. -2^1
C (n)=2C(n/2)+n-1 ,until C(1)=0 – 2^0]
{Backward substitution moving from n
to 1} Sum of nth term of Geometric series,

Consider n=2^k Sn= (a*r^(n-1))/(r-1)

C(n)=2*C(2^k/2) + 2^k - 1 =k*2^k – ((1*2^k – 1)/(2 – 1))


=2*C(2^(k-1)) + 2^k-1 =k*2^k – (2^k – 1)
=2[2*C(2^(k-2)) + 2(k-1) – 1] + 2^k =2^k(k-1) + 1
-1 ~k*2^k
=2[2[2*C(2^(k-3)) + 2^(k-2) – 1] +
2^(k-1) – 1] + 2^k – 1 Applying log on both sides (n=2^k)
=2^2*C(2^(k-2)) + 2^k – 2 + 2^k – log n = log(2^k) = k
1 C(n) ~ n*log(n)
=2^2*C(2(k-2) + 2*2^k – 2 – 2^0
=2^3*C(2^(k-3)) + 3*2^k – 2^2 – C(n) = θ(n*log2(n))
2^1 – 2^0
Properties of Merge sort
 Not in-place
 Requires an auxiliary array (O(n) extra
space)
 Stable
 Make sure that left is sent to target on equal
values.
 Iterative Merge sort reduces copying.

14
Merge sort Tree

 Take a binary tree T


 Each node of T represents a recursive call of the
merge sort algorithm.
 We associate with each node v of T a the set of
input passed to the invocation v represents.
 The external nodes are associated with individual
elements of S, upon which no recursion is called.
Merge Sort Tree (cont..)

16
Merge Sort Tree (cont.)

17
Merge Sort Tree (cont.)

18
Merge-Sort (cont.)

19
Merging Two Sequences

20
Merging Two Sequences (cont.)

21
Thank You

Das könnte Ihnen auch gefallen