MERGE SORT ALGORITHM AND COMPLEXITY ANALYSIS
MERGE SORT ALGORITHM AND COMPLEXITY ANALYSIS
Merge sort is a divide and conquer Merging and Sorting the Halves
algorithm that sorts elements in an
array by dividing it into two halves, After dividing the array, we merge and sort the two
sorting each half, and merging them halves. This process involves comparing elements
back together. from the left and right lists and arranging them in
ascending order.
Understanding the Index We compare the elements one by one.
Variables If an element from the left list is less than or equal to
the element from the right list, we place it in the
merged list.
To perform merge sort, we need to consider the left We repeat this process until all elements are merged.
index (l), right index (r), and middle index (m).
These indices help us divide the array and sort the "Merging and sorting the halves
elements. involves comparing elements from
the left and right lists and arranging
The left index represents the starting index of the
them in ascending order."
array.
The right index represents the ending index of the
array.
The middle index helps us determine the midpoint
Recurrence Relationship
when dividing the array.
Merge sort follows a recurrence relationship, where
"The left index (l), right index (r), and it calls itself on the two halves of the array.
middle index (m) are essential for
dividing and sorting an array using The first call is made on the first half of the array,
from the left index to the middle index.
merge sort." The second call is made on the second half of the
array, from the middle index + 1 to the right index.
Finding the Middle Index
"Merge sort follows a recurrence
When finding the middle index, we use the formula: relationship, calling itself on the two
m = (l + r) / 2. This equation helps us determine the halves of the array."
midpoint of the array.
The left index (l) and right index (r) values are used in
the formula.
The result is the middle index (m), which indicates the
midpoint of the array.
Complexity Analysis of Merge Recurrence Relation
Function The value of t(n)/2 is divided by 2, which is equal to 1
plus log n base 2 times n.
The time complexity of the merge function can be The time complexity equation will have the terms log
analyzed using the substitution method. n and n log n.
The merge function combines two sorted halves of "The time complexity equation will
the array. have the terms log n and n log n."
The time complexity of the merge function is O(n),
where n is the size of the input array.
Base Case
"The time complexity of the merge
function in merge sort is O(n), where The base case is when n = 1.
n is the size of the input array." In the base case, the value of t(n) is 2.
Another call of the function is performed.
Complexity Analysis of Merge "In the base case, the value of t(n) is
Sort Algorithm 2."
Recursive Case
Substitute n/2 for t(n) in the equation.
Simplify the equation to find the value of t(n)/2, which
is equal to log n base 2 plus n/2.