0% found this document useful (0 votes)
5 views5 pages

Merge

Merge Sort is a sorting algorithm that works by dividing an array into two halves, recursively sorting each half, and then merging the sorted halves back together. The process involves comparing elements from each half and placing the smaller element into the result array. The final result is a single sorted array.

Uploaded by

hellosachin5
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)
5 views5 pages

Merge

Merge Sort is a sorting algorithm that works by dividing an array into two halves, recursively sorting each half, and then merging the sorted halves back together. The process involves comparing elements from each half and placing the smaller element into the result array. The final result is a single sorted array.

Uploaded by

hellosachin5
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/ 5

How Merge Sort Works

1.Divide: Split the array into two halves.


2.Conquer: Recursively sort each half.
3.Combine: Merge the two sorted halves to produce a single sorted array.

Splitting the Array: Split the array into two halves.

Recursive Sorting : Apply merge sort to both halves.

Merging:
• Merge the two sorted halves into one sorted array.
• comparing the elements of each half and placing the smaller
element into the result array.
Initial Array: [38, 27, 43, 3, 9, 82, 10]

Divide: Split the array into two halves. [38, 27, 43] and [3, 9, 82, 10]
Conquer: Recursively sort each half.
Split into [38] and [27, 43]

Both [27] and [43] are already sorted.

Merge [38] and [27, 43]

27 is smaller, so it goes into the result array.

38 is smaller, so it goes into the result array.

43 is the only element left, so it goes into the result array


Result: [3, 9, 10, 82]
Result: [27, 38, 43]
Combine:
•Merge [27, 38, 43] and [3, 9, 10, 82]:


Compare 27 and 3: 3 is smaller, so it goes into the result array.
•Compare 27 and 9: 9 is smaller, so it goes into the result array.


Compare 27 and 10: 10 is smaller, so it goes into the result array.


Compare 27 and 27: 27 is smaller, so it goes into the result array.


Compare 38 and 82: 38 is smaller, so it goes into the result array.

•Compare 43 and 82: 43 is smaller, so it goes into the result array.

•82 is the only element left, so it goes into the result array.

You might also like