0% found this document useful (0 votes)
4 views13 pages

Sorting (Merge Sort)

Merge sort is a sorting algorithm that utilizes the divide and conquer technique by dividing an array into equal halves, sorting each half recursively, and then merging them back together in a sorted manner. The process involves three main steps: divide, conquer, and combine. An example illustrates the algorithm's operation with specific array indices and values.

Uploaded by

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

Sorting (Merge Sort)

Merge sort is a sorting algorithm that utilizes the divide and conquer technique by dividing an array into equal halves, sorting each half recursively, and then merging them back together in a sorted manner. The process involves three main steps: divide, conquer, and combine. An example illustrates the algorithm's operation with specific array indices and values.

Uploaded by

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

Merge Sort

Data Structures and Algorithms


 Merge sort is a sorting technique based on
divide and conquer technique.

 Merge sort first divides the array into equal


halves and then combines them in a sorted
manner.

03/26/2025
Data Structure & Algorithms(Fall 2024)
 Divide: Divide the unsorted list into two sub lists
of about half the size.
 Conquer: Sort each of the two sub lists recursively
until we have list sizes of length 1,in which case the
list itself is returned.
 Combine: Merge the two-sorted sub lists back
into one sorted list.

03/26/2025
Data Structure & Algorithms(Fall 2024)
How it happens cont;

Ex:

Array Name : A

Starting index: p

Ending index: r

Array: A[p..r]

03/26/2025
Data Structure & Algorithms(Fall 2024)
How it happens cont;

Divide
Mid point of p and r : q
the subarrays : A[p..q] and A[q+1, r]
Conquer
Sorting subarrays A[p..q] and A[q+1, r] till the base case is reached
Combine
Combining two sorted subarrays A[p..q] and A[q+1, r]

03/26/2025
Data Structure & Algorithms(Fall 2024)
Algorithm 9

Algorithm MergeSort(l,h){ if(l<h){ 0 1 2 3 4 5 6


7 4 1 3 6
4 2 87 5
mid=(l+h)/2;
MergeSort(l,mid);
MergeSort((mid+1),h); Hig
Merge(l,mid,h); Mi
h

} Lo d

} w

l = low , h =
high

03/26/2025
Data Structure & Algorithms(Fall 2024)
03/26/2025
Data Structure & Algorithms(Fall 2024)
MERGE SORT EXAMPLE :
p q q+1 r
1 5 7 8 2 4 6 9

L 1 5 7 8 infinit
y R 2 4 6 9 infinit
y

i=1 3 4 5 j=1 2 3 4
2 5

K 1 2 4 5 6 7 8 9

03/26/2025
Data Structure & Algorithms(Fall 2024)
03/26/2025
Data Structure & Algorithms(Fall 2024)
03/26/2025
Data Structure & Algorithms(Fall 2024)
03/26/2025
Data Structure & Algorithms(Fall 2024)
03/26/2025
Data Structure & Algorithms(Fall 2024)
03/26/2025
Data Structure & Algorithms(Fall 2024)

You might also like