Sorting (Merge Sort)
Sorting (Merge Sort)
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
} 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)