You are on page 1of 8

Merge Sort

Sorting Algorithms
Overview

 Merge sort is a divide-and-conquer algorithm that works


by recursively dividing the input array into smaller
subarrays, sorting those subarrays, and then merging them
back together into a single sorted array.
 Here's how it works in more detail:
Step 1
Divide array into 2 halves
Step 2
Recursively sort each half using merge sort
Step 3: Merge the two sorted halves back together:

 [27, 38, 3, 43, 9, 10, 82] -> [27, 38, 3, 43] [9, 10, 82]
 [27, 38, 3, 43] -> [27, 38] [3, 43]
 [27, 38] -> [27, 38]
 [3, 43] -> [3, 43]
 [27, 38, 3, 43] -> [3, 27, 38, 43]

 [9, 10, 82] -> [9] [10, 82]


 [10, 82] -> [10, 82]
 [9, 10, 82] -> [9, 10, 82]

 [3, 27, 38, 43] [9, 10, 82] -> [3, 9, 10, 27, 38, 43, 82]
Finally

The entire array is now sorted.So, the


final sorted array is

[3, 9, 10, 27, 38, 43, 82].


Merge Sort Algorithm

 Merge sort is a divide-and-conquer algorithm that works by recursively dividing the input
array into smaller subarrays, sorting those subarrays, and then merging them back together
into a single sorted array. Here's how it works in more detail:
1. Divide the input array into two halves.
2. Recursively sort each half by applying the merge sort algorithm to each half.
3. Merge the two sorted halves back together into a single sorted array.
4. Repeat steps 1-3 until the entire array is sorted.
Animation

You might also like