0% found this document useful (0 votes)
34 views14 pages

Merge Sort

The document explains how to sort arrays using the merge sort algorithm. Merge sort divides the array into halves recursively until reaching single elements, then merges the sorted halves back together to produce the fully sorted array. Examples show applying merge sort to sort arrays in both ascending and descending order.

Uploaded by

Ezekiel Nequit
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
0% found this document useful (0 votes)
34 views14 pages

Merge Sort

The document explains how to sort arrays using the merge sort algorithm. Merge sort divides the array into halves recursively until reaching single elements, then merges the sorted halves back together to produce the fully sorted array. Examples show applying merge sort to sort arrays in both ascending and descending order.

Uploaded by

Ezekiel Nequit
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/ 14

Sorting

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.

To understand merge sort, we take an unsorted array as


the following −

Sort it to ascending pattern


2
Merge Sort
We know that merge sort first divides the whole array iteratively
into equal halves unless the atomic values are achieved.
We see here that an array of 8 items is divided into two arrays
of size 4.

3
Merge Sort
This does not change the sequence of appearance of items in the
original. Now we divide these two arrays into halves.

We further divide these arrays and we achieve atomic value


which can no more be divided.

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

You might also like