Let's quickly hop on to the next video where you will learn how Merge Sort works.
So in this session, we are going to take a look at two very interesting sorting algorithms. One of them is merge sort and the other is quicksort. So what is good about merge sort? So you have read in the previous session that the three sorting algorithms we looked at bubble sort, insertion sort and selection sort, had the worst case time complexity of order n square. Now, is there a sorting algorithm which can sort, in the worst case scenario, in order n login? Yes, there is. So merge sort is one very good sorting algorithm which helps us to sort a given array of elements in the worst case scenario in order n login. So let us take a look at the basic idea behind how merge sort works. So, suppose you're given an array of elements like these. So you can see that these elements are not ordered. Now, suppose I have to arrange them in the increasing order. So what I can do is that perhaps I can divide these array of elements into two segments of four elements each. Now, imagine if I could have some way of sorting these individual arrays of four elements each individually, and then perhaps I can combine them to form a single sorted array of elements. So what I'm talking about is that suppose I have a way to sort this particular smaller array of four elements. So on sorting them, I would get something like this.
One would be here. So this is the sorted version of this. Similarly for this array, my sorted array would look like this. Now, suppose you are given these two segments of individually sorted elements. Now, can you think of some way as to how you can merge the elements in these two different group of elements into one single combined list, such that that combined list is sorted in itself? So think about it, how you can merge these two segments into one, such that all the elements are arranged in increasing order. So think about it and answer in the question that follows.
Now that you are being introduced to the idea of how Merge Sort works, let's look at the video below to see how to merge those 2 sorted arrays into one single sorted array.
This session covers two sorting algorithms: Merge Sort and Quick Sort
Merge Sort has a worst-case time complexity of
The basic idea of Merge Sort is to divide an array into smaller segments and then merge them in sorted order
This is achieved by sorting each segment individually and then merging them together
The merged list is sorted in increasing order
Merge Sort is an efficient sorting algorithm for large data sets
It is possible to achieve worst-case time complexity of with Merge Sort
Awesome! Now you know how merge sort utilises ‘divide and conquer’ to sort an array. We keep dividing the array into two halves until we reach the single elements. Once we reach this stage, we start to merge them back in the required order. Let’s move on to the next video where we discuss the pseudocode for this merge function where we merge 2 sorted arrays into 1 single sorted array.