Professional Documents
Culture Documents
Merge Sort
Merge Sort
Merge Sort
Merge sort is a sorting technique based on divide and conquer
technique.Merge sort first divides the array into equal halves
and then combines them in a sorted manner.
3
Merge Sort
This does not change the sequence of appearance of items in the
original. Now we divide these two arrays into halves.
4
Merge Sort
Now, we combine them in exactly the same manner as they were
broken down but first compare the element for each list and then
combine them into another list in a sorted manner.
5
Merge Sort
In the next iteration of the combining phase, we compare lists of
two data values, and merge them into a list of found data values
placing all in a sorted order.
6
Merge Sort
After the final merging, the list should look like this −
7
Example #1
Sort the given below in ascending pattern.
35 23 40 6 56 9 11
1
35 23 40 6 56 9 11
2 3
35 23 40 6 56 9 11
First half:
4 7 14 steps
5 6
3 iteration process
35 23 40 6 56 9 11
8 9 10 11 12 13 14
8
Example #1
Sort the given below in ascending pattern.
35 23 40 6 56 9 11
23 35 6 40 9 56 11
15 16 17
6 23 35 40 9 11 56
18 19
Second half: 20
Therefore:
6 steps 20 steps
3 iteration process 6 9 11 12 35 40 56 6 iteration process 9
Example #2
Sort the given below in ascending pattern.
46 13 23 1 75 79 86
1
46 13 23 1 75 79 86
2 3
46 13 23 1 75 79 86
First half:
4 7 14 steps
5 6
3 iteration process
46 13 23 1 75 79 86
8 9 10 11 12 13 14
10
Example #2
Sort the given below in ascending pattern.
46 13 23 1 75 79 86
13 46 1 23 75 79 86
15 16
1 13 23 46 75 79 86
17
Second half: Therefore:
3 steps 17 steps
3 iteration process 1 13 23 46 75 79 86 6 iteration process 11
Example #3
Sort the given below in descending pattern.
63 12 18 0 75 9
1
63 12 18 0 75 9
2 3
63 12 18 0 75 9
First half:
7 11 steps
5 6
4 3 iteration process
18 0 75 9
63 12
10 11
8 9
12
Example #3
Sort the given below in descending pattern.
63 12 18 0 75 9
63 12 0 18 75 9
Therefore:
12
14 steps
6 iteration process
63 18 12 0 75 9
13
Second half:
14
3 steps
3 iteration process 75 63 18 12 9 0
13
Activity:
Sort the given below in ascending and descending
using Merge Sort
23 2 8 14 25 11 12 0
8 15 0 33 13 1 42 5 39 56
14