Analysis of Selection Sort in Data Structures

\$\$/\$\$

Like our previous algorithms, in this lesson, you will analyse selection sort for efficiency. Remember that selection sort requires less number of swaps than bubble sort, and you must be curious to see how that makes a difference on its Big O. Let’s look at the analysis in the next video.

\$\$/\$\$

Video Transcript

So now we'll take a look at the time complexity of selection sort. So here consider that I have this array ten 9876. In the next step what will happen is that in the first iteration what I'll be doing is that I will begin from this index and I will keep my min also here and consequently check for all these elements. So after the first iteration I would find that my min index is here so I would swap this with this. As a result of this operation in my next step my array would become something like this because the six and ten have been swapped, my six will come here and ten will be here. For the next iteration I will begin checking at this point I will keep my min here and consequently check which is the min element. In the rest of the array I'll find that my min element will occur here which is seven. So I will swap nine with seven and I would get this array as my result which will be 6789 and ten. In the next step I would start checking at this point and again check for the min element.

In the rest of the array I would find that the min element is the same as my starting element so there is no need to update anything. In my next step I would begin checking. At this point I would repeat the same procedure and find that since my min element corresponds and coincides to this same element there is no need to change anything. Similarly in the mala step I would begin checking at this point and since I am left with only one element it must surely be at its correct position. Now let us try to analyze and see what are the total number of swaps and what are the total number of comparisons which I did for this entire process. So here I had an array of five elements. Here I was comparing ten with nine, eight, seven and six. First comparison, second comparison, third comparison and fourth comparison. So in the first step I was doing four number of comparisons. Since my n, which is the number of elements here is five, I can say that the total number of comparisons I did in the first step were n minus one. In the second step I started from nine and compared it with eight, seven and ten. First comparison, second comparison and third comparison. So similarly applying the same logic as above I can say that the number of comparisons I did here is n minus two. Similarly for this step the number of comparisons will be n minus three, n minus four and it will go on till I'm left with a single comparison which is the comparison of this element with itself. So now these are the total number of comparisons which I am doing. Let me denote this by t of n. Now let us check what are the total number of swaps which are doing in the first step we only swapped ten with six. What are the number of swaps? The number of swaps is one here. Similarly, in the second step we only swapped nine with seven. So here also the number of swaps is one. So extrapolating this we can say that at maximum in each step of selection sort we are doing only one number of swap. Now, if you recall, how many swaps were we doing in bubble sort? If you remember, in bubble sort in each of these iterations, for example, in the first iteration we were swapping all the elements which were out of order. So we were actually doing n minus one number of swaps. Compare it with the one swap we are doing here. Similarly, in the second iteration of bubble sort we were again doing n minus two number of swaps. In the worst case here also we have a worst case scenario since all the elements are in reverse order but still at max, we are doing only one swap here. So we can conclude that selection sort has far less number of swaps involved as compared to bubble sort. Now here for calculating the time complexity we are only going to consider the total number of comparisons involved. So what will be the total number of comparisons? I will have to do the summation of all these comparisons. I'll write t of n is equal to one plus two plus three plus n minus three plus n minus two plus n minus one. I can write it like this.

Now, if you remember, this is nothing but the sum of the first n minus one natural numbers. As you saw in the time analysis of bubble sort, this comes out to be n minus one into n divided by two. Applying a similar logic, this can be reduced to half of n square minus n. On looking at this expression carefully, you'll see that I can ignore n in front of this bigger value which is N square. Moreover, I can also ignore this half since this is a constant. So this t of n basically reduces to the order of n square. So I will say that t of n is equal to theta of n square. So in conclusion, the time complexity of selection sort is also theta of n square. So how does it differ from the bubble sort? Well, the overall time complexity for both bubble sort and selection sort is of the order of theta n square. However, the number of swaps involved here are far less as compared to bubble sort. So the number of steps, the number of expensive steps involved in selection sort are very much less as compared to bubble sort. So we can say that selection sort is a better sorting algorithm as compared to bubble sort.

Video Recap

• Selection sort involves iterating through an array and swapping the minimum element found with the first unsorted element, and repeating the process with the remaining unsorted elements.

• The total number of comparisons involved in selection sort is equal to the sum of the first natural numbers.

• The time complexity of selection sort is .

• The number of swaps involved in selection sort is far less than bubble sort, making it a better sorting algorithm overall.

• The time complexity of both bubble sort and selection sort is of the order of .

So, as far as goes, selection sort also has a time complexity of O(N). Remember that you learnt that it performs fewer swaps than bubble sort. So ideally, it should run a bit more efficiently than bubble sort. In the next video, you will see sorting of a deck of the same six cards and realise the answer to this efficiency problem.

\$\$/\$\$

The video explained the answer to your question in detail. The actual number of steps a selection sort takes is N/2. But N/2 also falls into the time complexity of O(N). Therefore, despite selection sort being more efficient than bubble sort, it falls into the same Big O category as does bubble sort. In the next segment, you will learn about the next sorting algorithm — insertion sort.