100% found this document useful (1 vote)
32 views11 pages

Merge Sort

Merge Sort is a divide-and-conquer sorting algorithm that efficiently sorts large datasets by recursively splitting the list into smaller parts and merging them back in order. It guarantees a time complexity of O(n log n) and is stable, maintaining the order of equal items. While effective for large datasets, it may not be the fastest option for small lists.

Uploaded by

sd534679
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
100% found this document useful (1 vote)
32 views11 pages

Merge Sort

Merge Sort is a divide-and-conquer sorting algorithm that efficiently sorts large datasets by recursively splitting the list into smaller parts and merging them back in order. It guarantees a time complexity of O(n log n) and is stable, maintaining the order of equal items. While effective for large datasets, it may not be the fastest option for small lists.

Uploaded by

sd534679
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/ 11

Merge Sort

-------DIVIDE AND CONQUER ALGORITHM-------


Introduction:
 Merge Sort is a well-known method for sorting things that uses a strategy called divide and
conquer. The basic idea is to take a big problem and break it into smaller, easier parts. In this
case, the list that needs to be sorted is split into two halves again and again until each part
has just one item, which is already sorted. This is the “divide” step. After that, the algorithm
starts putting the pieces back together in the right order — this is the “conquer” step — until
everything is sorted.

 This way of sorting is known for being fast and reliable, especially when dealing with large
amounts of data. It always works in about the same amount of time, which makes it more
efficient than simpler methods like Bubble Sort or Insertion Sort. One of the advantages of
Merge Sort is that it keeps the order of equal items the same, which can be important in
some situations.
Divide and Conquer Approach:
The Divide and Conquer approach is a problem-solving strategy that breaks a large problem into
smaller, more manageable subproblems. Here's how it works:

1.Divide: The problem is divided into smaller subproblems that are similar to the original
problem, but easier to solve. This step continues until the subproblems are simple enough to be
solved directly.

2.Conquer: Each subproblem is solved independently, either directly or by recursively applying


the same divide-and-conquer strategy.

3.Combine: After the subproblems are solved, their solutions are combined to form the final
solution to the original problem.

 The advantage of divide and conquer is that it often leads to more efficient algorithms with
lower time complexities, especially for large datasets.
Motivation:
 Handling Large Datasets: Merge Sort handles large datasets
efficiently by breaking the problem into smaller chunks.

File Merging: When combining large, sorted files (e.g., logs,


records), Merge Sort is often used to merge multiple sorted
sequences into a single sorted file, optimizing the process .

External Sorting: Merge Sort is widely used in external sorting,


where data is too large to fit into memory and must be sorted
in external storage (like hard drives).
Algorithm:
Step 1: Check if the array has more than one element. If yes, proceed with sorting. If
the array has only one element, it's already sorted (base case of recursion).
Step 2: Divide the array into two halves.
 Find the middle index: mid = (start + end) / 2.
 Split the array into left and right subarrays.
Step 3: Recursively sort the left half of the array.
Step 4: Recursively sort the right half of the array.
Step 5: Merge the two sorted halves.
 Compare elements from both subarrays.
 Place the smaller element into a temporary array.
 Continue until all elements are merged in sorted order.
Step 6: Copy the merged elements back to the original array. Replace the original part
of the array with the merged and sorted values.
Step 7: End the recursion. Once all subarrays are sorted and merged, the full array will
be sorted.
Pseudocode:
//Problem description: Sort the given unsorted array using divide and
//conquer method.
//Input: An array ‘array’ of size n containing the elements to be //sorted.
//Output: The sorted array
Algorithm MergeSort(array, left, right)
if left < right then
mid ← (left + right) / 2
MergeSort(array, left, mid)
MergeSort(array, mid + 1, right)
Merge(array, left, mid, right)
Merge(array, left, mid, right)
n1 ← mid - left + 1 while i < n1 do
n2 ← right - mid array[k] ← leftArray[i]
for i ← 0 to n1 – 1 i←i+1
leftArray[i] ← array[left + i] k←k+1
for j ← 0 to n2 - 1 while j < n2 do
rightArray[j] ← array[mid + 1 + j] array[k] ← rightArray[j]
i ← 0, j ← 0, k ← left j←j+1
while i < n1 and j < n2 do k←k+1
if leftArray[i] ≤ rightArray[j] then
array[k] ← leftArray[i]
i←i+1
else
array[k] ← rightArray[j]
j←j+1
k←k+1
Numerical Example:
Divide:
[38, 27, 43, 3, 9, 82, 10]
 Mid = low+high/2
Mid = 0+6/2 = 3
 Now split the array into two
subarrays [0,3] and [4,6]
[38, 27, 43, 3] [9, 82, 10]

[38, 27] [43, 3] [9, 82] [10]  Repeat the dividing process
until a single element left in
the array

[38] [27] [43] [3] [9] [82]


Conquer:
[38] [27] [43] [3] [9] [82] [10]
 Now merge the single
elements into a sorted
[27,38] [3, 43] [9, 82] array .

 Now merge Left and


[3, 27, 38, 43] [9, 10, 82] right arrays into single
Left sorted array Right sorted array sorted array.

Merge:
[3, 27, 38, 43] [9, 10, 82]  Here Compare the Left
and Right sorted arrays
and Merge them into a
Sorted array.
[3, 9, 10, 27, 38, 43, 82]
Final Sorted Array
Conclusion:
Merge Sort is an efficient, reliable, and stable sorting algorithm that
uses a divide-and-conquer strategy. It breaks a list into smaller parts,
sorts them, and then merges them back together in order. It works
well for large datasets and guarantees a time complexity of O(n log
n), making it faster than simple algorithms like bubble sort or
insertion sort.
 Time Complexity:
Best case:
Worst case: O(n logn)
Average case:
 Space Complexity: O(n)
Question & Answer

1.Why does Merge Sort consistently run in O(n log n) time,


regardless of input order?
Answer:
Because it always splits the array into halves (log n levels) and
performs linear-time merges at each level, resulting in O(n log n).

2. Is Merge Sort also fast for small lists?


Answer:
Merge sort is applicable for small lists but It is not fast
compared to large lists.

You might also like