Lecture 1.2.5
Lecture 1.2.5
ENGINEERING
DEPARTMENT OF COMPUTER
SCIENCE AND ENGG.
Bachelor of Engineering (Computer Science & Engineering)
DATA STRUCTURES 21CSH-211
•Sorting
• Merge Sort
2
Merge Sort
• Merge sort is a sorting technique based on divide and conquer technique.
With worst-case time complexity being Ο(n log n), it is one of the most
respected algorithms.
• Merge sort first divides the array into equal halves and then combines them
in a sorted manner.
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be
divided. By definition, if it is only one element in the list, it is sorted. Then,
merge sort combines the smaller sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be
divided.
Step 3 − merge the smaller lists into new list in sorted order.
3
• Merge sort is a sorting technique based on divide and conquer
technique. With worst-case time complexity being Ο(n log n), it is
one of the most respected algorithms.
• Merge sort first divides the array into equal halves and then
combines them in a sorted manner.
4
• How Merge Sort Works?
• To understand merge sort, we take an unsorted array as the
following −
5
6
Algorithm
• Merge sort keeps on dividing the list into equal halves until it can no
more be divided. By definition, if it is only one element in the list, it
is sorted. Then, merge sort combines the smaller sorted lists keeping
the new list sorted too.
• Step 1 − if it is only one element in the list it is already sorted,
return.
• Step 2 − divide the list recursively into two halves until it can no
more be divided.
• Step 3 − merge the smaller lists into new list in sorted order.
7
Example
8
Complexity Analysis
References
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley
Student Edition.
• https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
• https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecturehtml
• https://www.w3resource.com/php-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-4.
php
• https://www.programiz.com/dsa/merge-sort
• https://www.geeksforgeeks.org/binary-search/
10
THANK YOU