# Comparison of Sorting Algorithms in Data Structure

\$\$/\$\$

You have gone through five sorting algorithms until now. So, you have a good grasp of the time complexity of each algorithm. In this segment, you will compare them all to see which algorithm to choose in different cases.

\$\$/\$\$

## Video Transcript

Now let us take a look at some of the important properties associated with Quicksort and merge sort. So the first property is that of stability. Here we can see that merge sort is stable, whereas Quicksort in not. So what do we mean by stability? By stability we mean that suppose I have a collection of coordinates like this, where this coordinate indicates the X coordinate and this coordinate indicates the Y coordinate. Now, suppose I have to sort these collection of coordinates according to the X coordinate. So here you would see that these two elements here, both of them have X coordinate equal to one. Now, what do you think is going to come here first? Is it going to be one comma five, or is it going to be one comma four? So if I use a stable sorting algorithm, in that case, the individual order of these two elements relative to each other will not be destroyed. That is, if I use a stable sorting algorithm, it will preserve the order between one comma five and one comma four. That is, one comma five will come here, then one comma four and then the rest of the elements sorted according to their X coordinate. So this was an example of stable sorting algorithm, whereas if I would have chosen an unstable sorting algorithm, it might be possible that here the order of one comma five and one comma four would have been interchanged. That is, the array would look something like this and so on. So what exactly is the utility of an algorithm being stable? Well, you can read about it in more detail in the link provided in this segment. But for now, just remember the fact that a stable sorting algorithm does not destroy the relative order of elements which have equal key, whereas you cannot guarantee anything in case of an unstable sorting algorithm. Now, let us take a look at a second property which we call as a sorting algorithm being in place. Here you can see that merge sort is not in place, whereas Quicksort is. So what does in place mean? So in place essentially means that if an algorithm uses extra space which is comparable to the size of the data set, that is, if I have an array of size n, and if I'm using an extra space of the size of order n itself, then that particular sorting algorithm is said to be not in place. Whereas if I do not use any additional data structures, or if the extra data structures which I am using is very less compared to the size of my data set that is very less than order n, then that particular algorithm is said to be in place. As an example, you can see in Quicksort what we were doing is that we were overwriting all our new elements, over the already existing array of elements. So there I was not using any additional array. That means that Quicksort was in place whereas in merch sort in each step I was dividing my array into two different subarrays and writing them down into new arrays by declaring new data structures. So my overall extra space which I was using was where much comparable to the size of the input array that is the extra space which I was using in case of merge sort was of the order of N and hence I say that merge sort is not in place.

## Video Recap

Quicksort and merge sort are important sorting algorithms

• Merge sort is stable, whereas Quicksort is not

• Stability refers to the preservation of the order between elements with equal keys during sorting

• A stable sorting algorithm ensures that the relative order of such elements is not destroyed, unlike an unstable sorting algorithm

• The utility of a stable sorting algorithm is explained in detail in a linked resource

• Merge sort is not in place, whereas Quicksort is

• In-place sorting algorithms do not use extra space comparable to the size of the data set being sorted

• Quicksort is in place as it overwrites new elements over the already existing array of elements

• Merge sort, on the other hand, creates new subarrays and data structures, using extra space of the order of N

• In summary, Quicksort and merge sort have different properties such as stability and in-place sorting capabilities.

Now you have seen the essential differences between Merge Sort and Quick Sort when it comes to these algorithms being in-place and stable, let us now hear what Ankit has to tell us about comparing all the sorting algorithms we have learnt so far.

\$\$/\$\$

Great! Now you know that Bubble sort, Selection sort, and Insertion sort have a time complexity of O() in average and worst cases. So, generally they are not used unless we are very sure that the dataset is very close to the best case (where insertion sort has an O(N) time complexity). Both Merge sort and Quick Sort has an O(N log N) time complexity. But Quicksort has O( ) in worst case. Also, Merge sort is a stable sort, which Quicksort is not. So, in case you need a stable sorting algorithm, the only sorting algorithm you can opt for is Merge sort.