0% found this document useful (0 votes)
11 views9 pages

Merge Sort

Uploaded by

JET JET
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)
11 views9 pages

Merge Sort

Uploaded by

JET JET
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/ 9

Merge Sort

Introduction
• Merge sort is a famous sorting algorithm.
• It uses a divide and conquer paradigm for sorting.
• It divides the problem into sub problems and solves them individually.
• It then combines the results of sub problems to get the solution of the
original problem.
Divide and Conquer Strategy
Suppose we had to sort an array A. A subproblem would be to sort a sub-section
of this array starting at index p and ending at index r, denoted as A[p..r].
• Divide - If q is the half-way point between p and r, then we can split the
subarray A[p..r] into two arrays A[p..q] and A[q+1, r].
• Conquer - In the conquer step, we try to sort both the
subarrays A[p..q] and A[q+1, r]. If we haven't yet reached the base case, we
again divide both these subarrays and try to sort them.
• Combine - When the conquer step reaches the base step and we get two sorted
subarrays A[p..q] and A[q+1, r] for array A[p..r], we combine the results by
creating a sorted array A[p..r] from two sorted subarrays A[p..q] and A[q+1,
r].
MergeSort Algorithm

• The MergeSort function repeatedly


divides the array into two halves
until we reach a stage where we try
to perform MergeSort on a subarray
of size 1 i.e. p == r.
• After that, the merge function comes
into play and combines the sorted
arrays into larger arrays until the
whole array is merged.
Merge( ) Function
Merge Sort Complexity
• Time Complexity
• Best Case Complexity: O(n*log n)
• Worst Case Complexity: O(n*log n)
• Average Case Complexity: O(n*log n)

• Space Complexity
• The space complexity of merge sort is O(n).
Example-1
• Consider the following elements have to be sorted in ascending order-
6, 2, 11, 7, 5, 4

You might also like