0% found this document useful (0 votes)
18 views

Merge Sort

Merge sort is a divide and conquer algorithm that works by recursively splitting an array into halves, sorting each half, and then merging the sorted halves back together. It involves dividing the array, recursively sorting the subarrays, and merging the sorted subarrays back into one sorted array.

Uploaded by

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

Merge Sort

Merge sort is a divide and conquer algorithm that works by recursively splitting an array into halves, sorting each half, and then merging the sorted halves back together. It involves dividing the array, recursively sorting the subarrays, and merging the sorted subarrays back into one sorted array.

Uploaded by

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

Merge

Merge Sort
Sort
Divide-and-Conquer

• Divide and Conquer is 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 subproblems
associated with the data subsets.
 Conquer: Take the solutions to the subproblems and “merge”
these solutions into a solution for the original problem.
An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into non-
decreasing order.

• Divide: Divide the n-element sequence to be sorted into


two subsequences of n/2 elements each

• Conquer: Sort the two subsequences recursively using


merge sort.

• Combine: Merge the two sorted subsequences to produce


the sorted answer.
Divide-and-Conquer Technique (cont.)

a problem of size n
(instance)

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to the It leads to a recursive


original problem algorithm!
Merging

Problem statement:
Given two arrays A[0..N-1] and B[0..M-1]
sorted in place. Merge them to an external array
C[0..N+M-1]
Such that C[] is also sorted
1 2 3 4 10 20 5 16 22 43 50 67

A[0..5] B[0..5]
1 2 3 4 5 10 16 20 22 43 50 67

C[0..11]
Mergesort
• Split array A[0..n-1] into about equal halves and make copies
of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
 Repeat the following until no elements remain in one of the
arrays:
• compare the first elements in the remaining unprocessed
portions of the arrays
• copy the smaller of the two into A, while incrementing
the index indicating the unprocessed portion of that
array
 Once all elements in one of the arrays are processed, copy
the remaining unprocessed elements from the other array
into A.
Merging

i=0 J=0

1 2 3 4 10 20 5 16 22 43 50 67

K=0

1
Merging

i=1 J=0

1 2 3 4 10 20 5 16 22 43 50 67

K=1

1 2
Merging

i=2 J=0

1 2 3 4 10 20 5 16 22 43 50 67

K=2

1 2 3
Merging

i=3 J=0

1 2 3 4 10 20 5 16 22 43 50 67

K=3

1 2 3 4
Merging

i=4 J=0

1 2 3 4 10 20 5 16 22 43 50 67

K=4

1 2 3 4 5
Merging

i=4 J=1

1 2 3 4 10 20 5 16 22 43 50 67

K=5

1 2 3 4 5 10
Merging

i=5 J=1

1 2 3 4 10 20 5 16 22 43 50 67

K=6

1 2 3 4 5 10 16
Merging

i=5 J=2

1 2 3 4 10 20 5 16 22 43 50 67

K=7

1 2 3 4 5 10 16 20
Merging

i=6 J=2

1 2 3 4 10 20 5 16 22 43 50 67

K=6

1 2 3 4 5 10 16 20 22 43 50 67
Merging

Merge(A,B,n,m,C) :
Select A[i] and B[j] starting from i=0 and j=0 and k = 0
If A[i] < B[j]
C[k]= A[i]
increment k and i by 1
Else
C[k] = B[j]
increment k and j
If i=n and j<m
copy all remaining elements of B into C
If j=m and i<n
copy all remaining elements of A into C
Mergesort

Mergesort(C, low, high)


{
If(low < high) then
{
Mid = [ low+high] / 2
Mergesort (C,low,mid)
Mergesort( C,mid+1, high)
Merge ( C,low, mid, high)
}
}
Merge Sort – Example
Original Sequence Sorted Sequence
18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43

18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43

18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9

18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1
Merge Sort Example
•Mergesort (divide-and-conquer)
Divide array into two halves.

A L G O R I T H M S

A L G O R I T H M S divide
Merge Sort

•Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.

A L G O R I T H M S

A L G O R I T H M S divide

A G L O R H I M S T sort
Mergesort

•Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
Merge two halves to make sorted whole.
A L G O R I T H M S

A L G O R I T H M S divide

A G L O R H I M S T sort

A G H I L M O R S T merge
Merging

•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest

A G L O R H I M S T

A auxiliary array
Merging

•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest

A G L O R H I M S T

A G auxiliary array
Merging

•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest

A G L O R H I M S T

A G H auxiliary array
Merging

•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest

A G L O R H I M S T

A G H I auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M O auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M O R auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S auxiliary array
Merging
•Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S T auxiliary array
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers

Mergesort( low, high)


// A[low:high] is a global array to be sorted.
{
If(low < high) then
{
Mid = [ low+high] / 2
Mergesort (low,mid)
Mergesort( mid+1, high)
Merge ( low, mid, high)
}
}
Merge-Sort (A, p, r)
Merge(low, mid, high)
{
h= low; i=low;j=mid+1;
while ( ( h<=mid) and (j<=high)) do
{
If ( a[h] <= a[j]) then
b[i]= a[h]
h=h+1
else
b[i] =a[j]; j=j+1
i=i +1
}
If( h>mid) then
For k= j to high do
b[i] = a[k]
i = i+1
for k = h to mid do
b[i] = a[k]
i = i+1
for k = low to high do
a[k] = b[k]
}
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn
• Each level has total cost cn.
• Each time we go down one level,
the number of subproblems doubles,
cn/2 cn/2 but the cost per subproblem halves
 cost per level remains the same.
• There are lg n + 1 levels, height is
cn/4 cn/4 cn/4 cn/4 lg n. (Assuming n is a power of 2.)
 Can be proved by induction.
• Total cost = sum of costs at each
c c c c c c level = (lg n + 1)cn = cnlgn + cn =
(n lgn).
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn cn

cn/2 cn/2 cn

lg n

cn/4 cn/4 cn/4 cn/4 cn

c c c c c c cn

Total : cnlgn+cn
Example

3, 8, 1, 5, 9, 2, 0, 7, 4, 1, 7
Sorting Algorithms so far

• Insertion sort
 Worst-case running time O(n2)

• Merge sort
 Worst-case running time O(n log n), but requires
additional memory O(n)

You might also like