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

Merge Sort

The document describes the merge sort algorithm. Merge sort uses a divide and conquer approach to sort a list. It divides the list into halves, recursively sorts the halves, and then merges the sorted halves back together. The algorithm is demonstrated through an example that shows how it divides, conquers, and combines subproblems to fully sort an initial list of random numbers.

Uploaded by

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

Merge Sort

The document describes the merge sort algorithm. Merge sort uses a divide and conquer approach to sort a list. It divides the list into halves, recursively sorts the halves, and then merges the sorted halves back together. The algorithm is demonstrated through an example that shows how it divides, conquers, and combines subproblems to fully sort an initial list of random numbers.

Uploaded by

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

Data Structures

Topic: Merge Sort


By
Ravi Kant Sahu
Asst. Professor

Lovely Professional University, Punjab


Divide and Conquer
 Recursive in structure
 Divide the problem into sub-problems that are
similar to the original but smaller in size

 Conquer the sub-problems by solving them


recursively. If they are small enough, just solve
them in a straightforward manner.

 Combine the solutions to create a solution to the


original problem

-2 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.

-3 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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

-5 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Work Space

-6 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers

MergeSort (A, p, r) // sort A[p..r] by divide & conquer


1 if p < r
2 then q  (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]

Initial Call: MergeSort(A, 1, n)

-7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Procedure Merge
Merge(A, p, q, r)
1 n1  q – p + 1
2 n2  r – q Input: Array containing
3 for i  1 to n1
4 do L[i]  A[p + i – 1]
sorted subarrays A[p..q]
5 for j  1 to n2 and A[q+1..r].
6 do R[j]  A[q + j]
7 L[n1+1]   Output: Merged sorted
8 R[n2+1]   subarray in A[p..r].
9 i1
10 j  1
11 for k p to r
12 do if L[i]  R[j]
13 then A[k]  L[i]
14 ii+1 Sentinels, to avoid having to
15 else A[k]  R[j] check if either subarray is
16 jj+1
fully copied at each step.

-8 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Work Space

-9 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Merge – Example
A … 61 86 26
8 32 1 32
9 26 9 42 43 …
k k k k k k k k k

L 6 8 26 32  R 1 9 42 43 
i i i i i j j j j j

- 10 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 1

- 11 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 2

- 12 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 3

- 13 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 4

- 14 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 5

- 15 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 6

- 16 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 7

- 17 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 8

- 18 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 9

- 19 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 10

- 20 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 11

- 21 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 12

- 22 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 13

- 23 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 14

- 24 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 15

- 25 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 16

- 26 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 17

- 27 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 18

- 28 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 19

- 29 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 20

- 30 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 21

- 31 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MergeSort (Example) - 22

- 32 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
- 33 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

You might also like