Problem : Why is selection sort's efficiency independent of the data being sorted?Unlike bubble sort, selection sort has no way to short circuit its operation if the list is in order before the algorithm has gone all the way through it. For a list of a given length, selection sort will perform the same number of comparisons regardless of whether the list is in order or hopelessly out of order.
Problem : Consider the following list: (4, 3, 8, 22, 1, 5). What will this list look like after the first swap?It will be: (1, 3, 8, 22, 4, 5). Notice that the smallest element has been placed at the front of the list.
Problem : Imagine the following implementation of selection sort: instead of selecting the smallest element and putting it at the front, we instead select the largest element and put it at the end of the list. Does this change the algorithm's efficiency?In theory, no. The number of selections and swaps are the same. In practice it depends on the implementation, but with care it wouldn't be hard to implement an O(n2) algorithm as described above.
Problem : When would bubble sort be a better algorithm to use than selection sort?Bubble sort works most efficiently when the data it is applied to are in order or nearly in order. Selection sort has the same efficiency no matter what the composition of the data. Thus, bubble sort is a better algorithm to choose when the data are close to being in sorted order.
Take a Study Break!