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

MERGE SORT ALGORITHM AND COMPLEXITY ANALYSIS

Merge sort is a divide and conquer algorithm that sorts an array by recursively dividing it into two halves, sorting each half, and merging them back together. The time complexity of merge sort is O(n log n), where n is the size of the input array, and the merge function has a time complexity of O(n). The algorithm relies on the left index, right index, and middle index to effectively divide and sort the array.

Uploaded by

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

MERGE SORT ALGORITHM AND COMPLEXITY ANALYSIS

Merge sort is a divide and conquer algorithm that sorts an array by recursively dividing it into two halves, sorting each half, and merging them back together. The time complexity of merge sort is O(n log n), where n is the size of the input array, and the merge function has a time complexity of O(n). The algorithm relies on the left index, right index, and middle index to effectively divide and sort the array.

Uploaded by

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

"The middle index (m) is calculated

MERGE SORT using the formula: m = (l + r) / 2,


where l is the left index and r is the
ALGORITHM AND right index."

COMPLEXITY Dividing the Array


ANALYSIS | SORTING To divide the array in merge sort, we consider the
ALGORITHM values of the left index (l) and right index (r). If r is
greater than l, we calculate the middle index (m)
and divide the array into two halves.
Introduction and Overview
 The first half consists of elements from the left index
Merge sort is a divide and conquer algorithm used to the middle index.
for sorting elements in an array. It is one of the  The second half consists of elements from the middle
index + 1 to the right index.
many sorting algorithms, including insertion sort
and quicksort. To understand merge sort, we need
"When dividing the array, we split it
to consider variables such as the left index, right
into two halves based on the values
index, and middle index. The algorithm divides the
of the left index (l), right index (r),
array into two halves, recursively calls itself, and
and middle index (m)."
then merges the sorted halves.

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."

By considering the time complexity of the merge Equivalence Proof


function and the recurrence relationship, we can
analyze the overall time complexity of the merge  Divide t(n) by n, but do not give a result.
sort algorithm.  Equivalence is shown between t(n)/n and log n base
2 times n.
 If n is greater than one, the time complexity can be  k is defined as log n.
expressed as T(n) = 2 * T(n/2) + n.  Simplify the expression to obtain t(n)/n = 1 + k log n +
 Using the substitution method, we find that the time n log n.
complexity of the merge sort algorithm is O(n log n).  The highest order term is n log n.

"The overall time complexity of the "Equivalence is shown between


merge sort algorithm is O(n log n), t(n)/n and log n base 2 times n."
where n is the size of the input
array, based on the analysis of the Summary of Complexity
merge function and the recurrence
relationship." Analysis
 The complexity analysis of the merge sort algorithm
Complexity Analysis of Merge shows that the highest order term in the time
complexity equation is n log n.
Sort Algorithm  The time complexity is O(n log n).

 The final answer is 40. "The complexity analysis of the


 The value of t(n) is divided by 2, squared, plus n over
merge sort algorithm shows that the
2.
 The value of k, which is the exponent, is log of n. highest order term in the time
 The highest order term in the time complexity complexity equation is n log n."
equation is n log n.

"The final answer is 40."

"The highest order term in the time


complexity equation is n log n."

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.

"Simplify the equation to find the


value of t(n)/2, which is equal to log
n base 2 plus n/2."

You might also like