
Selection sort is better than Bubble sort due to less swapping required. Note: In Bubble sort, we can identify whether list is sorted or not in 1st iteration but in Selection sort we can't able to identify that. Compared to Selection sort, Bubble sort should be used when the given array is almost sorted. Selection sort Time Complexity AnalysisThe time complexity is O(n2) because it has gone through two for loops.

The number of comparisons involved in an average bubble sort is 0.5*(n*n-n). If you are curious to know the reason for this, please read the article about Bubble sort carefully. Just like “ Bubble sort”, selection sort is also considered as an inefficient sorting algorithm. In this article we are going to see another sorting algorithm, named, Selection sorting. In the previous article, we have analysed Bubble sort algorithm and it’s implementation in C programs.

Java Selection Sort Vs Bubble Sort Code Performs Sorting
It is then compared with other elements one by one (using the 2nd FOR loop). The element in the first position is assumed to be minimum. The code performs sorting in ascending order using selection sort./* limit = number of elements in the array *//* min = variable used to hold the assumed minimum element */For(i=0 i a) // Compares the minimum element with all other members using inner FOR loop.Temp=a // 3 statements below exchanges the elementsAll the four passes of outer FOR loop is shown in the image below:-You can easily understand the working of selection sort by interpreting the 2 images given above & the example code snippet. The code snippet for implementing selection sort in C is given below. Based on this comparisons result, exchanges are made between the min/max element and the other element.Lets study this algorithm through an example. Now all other members are compared against this assumed min/max element.
You may go through the Bubble Sorting in C/C++ article and find out how modifications are made in the algorithm. We can assume that in an average case, Selection sort is much efficient than Bubble sorting algorithm.Just like we made the Bubble sort algorithm better with slight modifications, we can make the Selection sort algorithm better too. However the total number of exchanges required (in the average case) is very low for selection sorting. The total number of comparisons required is almost same for both Bubble sorting and Selection sorting. Selection sort requires 0.5 *(n*n-n) comparisons. When the second pass of first FOR loop begins, the next element (i.e second element) is assumed to be minimum and the whole process repeats.The selection sorting algorithm is also an inefficient one (just like Bubble sort).
