Performance of selection sort:
The worst case for selection sort occurs when the first item in the list is the largest, and the rest of the list is in order. In this case, we perform one swap on each pass through the algorithm, so the number of swaps is N. The number of comparisons is the same as in the best case, O(N 2).
The average case requires the same number of comparisons, O(N 2), and roughly N/2 swaps. Thus, the number of swaps in the average case is O(N ).
In summary, we say that selection sort is O(N ) in swaps and O(N 2) in comparisons.
No comments:
Post a Comment