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

merge sort (2)

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

merge sort (2)

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Merge sort Algorithm

Is based on the divide and conquer approach


Divides the list into two sublists of sizes as nearly equal as
possible
Sorts the two sublists separately by using merge sort
Merges the sorted sublists into one single list
Merge-Sort: Merge Example

A:
3 5 15 28 30 6 10 14

L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6

i=0 j=0
Merge-Sort: Merge Example

A:
3
1 5 15 28 30 6 10 14

k=0

L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6

i=0 j=0
Merge-Sort: Merge Example

A:
1 25 15 28 30 6 10 14

k=1

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=0 j=1
Merge-Sort: Merge Example

A:
1 2 3 28 30
15 6 10 14

k=2

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=1 j=1
Merge-Sort: Merge Example

A:
1 2 3 4 6 10 14

k=3

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=1
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 10 14

k=4

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=2
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 10 14

k=5

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=3
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 14

k=6

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=4
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 8
14

k=7

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=3 j=4
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 8

k=8

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=4 j=4
Algorithm:- Merge ( A [n], low, mid, high )
{
//A is an array of n elements, low is lower bound
of an array and high is upper bound.
1. i = low
2. j = mid + 1
3. k = low
4. Do
4.1 If( A [i] <= A [j])
4.1.1 B [k] = A [i]
4.1.2 i = i + 1
4.1.3 k = k +1
4.2 else
4.2.1 B [k] = A [j]
4.2.2 j = j +1
4.2.3 k = k +1
while (i <= mid && j <= high);
6. While ( j < = high)
6.1 B [k] = A [j]
6.2 j = j + 1
6.3 k = k +1

7. While( i <= mid )


7.1 B [k] = A [i]
7.2 i = i + 1
7.3 k = k +1

8. For (i=0 to n-1)


8.1 A [i] = B [i]

}
Implementing Merge Sort Algorithm

To understand the implementation of merge sort algorithm,


consider an unsorted list of numbers stored in an array.

0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)

Let us sort this unsorted list.

0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)

The first step to sort data by using merge sort is to split the list
into two parts.

0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)

The first step to sort data by using merge sort is to split the list
into two parts.

0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)

The list has odd number of elements, therefore, the left sublist is
longer than the right sublist by one entry.

0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)

Further divide the two sublists into nearly equal parts.

0 1 2 2 3 3 4 4 5 5 6 6
arr 53 10
10 3030 7676 3 3 5757 24 24
Implementing Merge Sort Algorithm (Contd.)

Further divide the sublists.

0 11 22 3 3 4 45 56 6
arr 53 10
1010 3030 30
7676 76 3 357 24
57 24
Implementing Merge Sort Algorithm (Contd.)

There is a single element left in each sublist.


Sublists with one element require no sorting.

0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)

Start merging the sublists to obtain a sorted list.

0 11 2 2 3 3 4 5
4 65 6
arr 10
53 53 10 30 30 76 76 3 357 57
24
57 24
Implementing Merge Sort Algorithm (Contd.)

Further merge the sublists.

0 1 2 2 3 3 4 4 55 6 6
arr 10 30
53 5330 7676 3 3 2457 57 57
24
Implementing Merge Sort Algorithm (Contd.)

Again, merge the sublists.

00 11 22 3 44 55 6 6
arr 10
3 30
10 53
24 76
30 533 5724 76 57
Implementing Merge Sort Algorithm (Contd.)

The list is now sorted.

0 1 2 3 4 5 6
arr 3 10 24 30 53 57 76
Write an algorithm to implement Merge sort:
Merge_sort ( A[n], low, high )
{
// A is an array of n elements,low is lower bound
of an array and high is upper bound.
1. if (low = = high)
1.1 Return Recursive Call
2. else
2.1 mid = (low + high)/2
2.2 Merge_sort ( A[], low , mid )
2.3 Merge_sort (A[], mid+1 , high )
2.4 Merge ( A[], low, mid, high )
Analysis of Merge Sort
• Divide the List into two sub-list
• Sort each sub-list
• Merge the two sub-list

The efficiency of merge sort is equal to O(n log


n)
Example Walkthrough
For an array A with indices from 0 to 6, here's how the recursive calls would
unfold:
Call MergeSort(A, 0, 6)
mid = 3
Call MergeSort(A, 0, 3) (left side)
Call MergeSort(A, 0, 3)
mid = 1
Call MergeSort(A, 0, 1) (left side again)
Call MergeSort(A, 0, 1)
mid = 0
Call MergeSort(A, 0, 0) (base case)
Return from MergeSort(A, 0, 0) (array is of size 1, so it's sorted)
Now call MergeSort(A, 1, 1) (base case)
Return from MergeSort(A, 1, 1) (array is of size 1, so it's sorted)
Merge both sorted subarrays from MergeSort(A, 0, 1).
Now return to MergeSort(A, 0, 3) and call MergeSort(A, 2, 3) (right side).
Continue this process until the entire array is sorted.
Merge-Sort Analysis
n cn

n/2 n/2 2 × cn/2 = cn

log n levels
n/4 n/4 n/4 n/4 4 × cn/4 = cn

n/2 × 2c = cn
2 2 2

Total: cn log n

• Total running time: O (n logn)


You might also like