Merge Sort
“A Divide and Conquer Sorting Algorithm”
Introduction
 Merge Sort is a robust, comparison-
based sorting algorithm. It
fundamental strategy is to break
down a problem into smaller sub-
problems, solving each recursively.
 It strictly follows the Divide and
Conquer approach.
 Time Complexity: O(n log n) in all
cases (Worst, Average, and Best).
 Space Complexity: O(n) due to the
requirement of temporary arrays for
merging.
Working Principle
 Divide:
Continuously split the array into two
equal halves until they can no longer be
divided.
 Conquer:
Recursively sort the sub-arrays. (Single
elements are naturally sorted).
 Combine:
Merge the two sorted sub-arrays back
into a single sorted array.
Algorithm Steps
 Base Case Check: If the array has 0 or 1 element, it is
considered already sorted. Return immediately.
 Recursive Division: Find the middle index and divide the array
into two halves (Left and Right).
 Sort Halves: Recursively call Merge Sort for the first half and
then for the second half.
 Merge: Compare elements from both sorted halves and merge
them into a new, fully sorted array.
Dry Run of Merge Sort
 Initial Array: [5,2,4,7,1,3,2,6]
 1. Divide:
[5,2,4,7] & [1,3,2,6]
 2. Divide Further:
[5,2], [4,7] ... [1,3], [2,6]
 3. Divide Further:
[5],[2],[4],[7] ... [1],[3],[2],[6]
 4. Sort & Merge:
[2,5],[4,7] and [1,3][2,6]
 5.Merge Sub-Arrays
[2,4,5,7] and [1,2,3,6]
 4. Final Merge:
[1,2,2,3,4,5,6,7]
Pseudo Code:
MergeSort
MERGE-SORT(arr, left,
right)
IF left < right THEN
mid = (left + right) / 2
MERGE-SORT(arr,
left, mid)
MERGE-SORT(arr,
mid + 1, right)
MERGE(arr, left, mid,
right)
Pseudo Code: Merge
MERGE(arr, left, mid, right)
n1 = mid - left + 1
n2 = right - mid
Create arrays L[1..n1], R[1..n2]
Copy elements of arr[left..mid] into L
Copy elements of arr[mid+1..right] into R
i = 0, j = 0, k = left
WHILE i < n1 AND j < n2
IF L[i] <= R[j]
arr[k] = L[i]
i = i + 1
ELSE
arr[k] = R[j]
j = j + 1
k = k + 1
Copy remaining elements of L (if any)
Copy remaining elements of R (if any)
Applications and Conclusion
 Merge Sort is a powerful divide and
conquer algorithm
 Provides consistent O(n log n)
performance
 Preferred when stability and
efficiency are required
 Sorting linked lists
 External sorting (large files)
 Used in databases and file
systems

DSA[1]5655566666666666666666666666666.pptx

  • 1.
    Merge Sort “A Divideand Conquer Sorting Algorithm”
  • 2.
    Introduction  Merge Sortis a robust, comparison- based sorting algorithm. It fundamental strategy is to break down a problem into smaller sub- problems, solving each recursively.  It strictly follows the Divide and Conquer approach.  Time Complexity: O(n log n) in all cases (Worst, Average, and Best).  Space Complexity: O(n) due to the requirement of temporary arrays for merging.
  • 3.
    Working Principle  Divide: Continuouslysplit the array into two equal halves until they can no longer be divided.  Conquer: Recursively sort the sub-arrays. (Single elements are naturally sorted).  Combine: Merge the two sorted sub-arrays back into a single sorted array.
  • 4.
    Algorithm Steps  BaseCase Check: If the array has 0 or 1 element, it is considered already sorted. Return immediately.  Recursive Division: Find the middle index and divide the array into two halves (Left and Right).  Sort Halves: Recursively call Merge Sort for the first half and then for the second half.  Merge: Compare elements from both sorted halves and merge them into a new, fully sorted array.
  • 5.
    Dry Run ofMerge Sort  Initial Array: [5,2,4,7,1,3,2,6]  1. Divide: [5,2,4,7] & [1,3,2,6]  2. Divide Further: [5,2], [4,7] ... [1,3], [2,6]  3. Divide Further: [5],[2],[4],[7] ... [1],[3],[2],[6]  4. Sort & Merge: [2,5],[4,7] and [1,3][2,6]  5.Merge Sub-Arrays [2,4,5,7] and [1,2,3,6]  4. Final Merge: [1,2,2,3,4,5,6,7]
  • 6.
    Pseudo Code: MergeSort MERGE-SORT(arr, left, right) IFleft < right THEN mid = (left + right) / 2 MERGE-SORT(arr, left, mid) MERGE-SORT(arr, mid + 1, right) MERGE(arr, left, mid, right) Pseudo Code: Merge MERGE(arr, left, mid, right) n1 = mid - left + 1 n2 = right - mid Create arrays L[1..n1], R[1..n2] Copy elements of arr[left..mid] into L Copy elements of arr[mid+1..right] into R i = 0, j = 0, k = left WHILE i < n1 AND j < n2 IF L[i] <= R[j] arr[k] = L[i] i = i + 1 ELSE arr[k] = R[j] j = j + 1 k = k + 1 Copy remaining elements of L (if any) Copy remaining elements of R (if any)
  • 7.
    Applications and Conclusion Merge Sort is a powerful divide and conquer algorithm  Provides consistent O(n log n) performance  Preferred when stability and efficiency are required  Sorting linked lists  External sorting (large files)  Used in databases and file systems