New Text Document

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Selection sort has no end conditions built in, so it will always compare every e lement with every other

element. This gives it a best-, worst-, and average-case complexity of O(n2).

Answer: Short answer Best case: O(n) Worst case: O(n2) Long answer Let's assume we're sorting data in an array of length n. Let's also assume that we're sorting in ascending order (low-high). The worst case is that you will have the smallest value in the last space in the array. This means that it will move exactly once each pass towards the first sp ace in the array. It will take n-1 passes to do this, doing n comparisons on eac h pass: O(n2) The best case is that the data comes to us already sorted. Assuming that you hav e a smart implementation (which you should, because it's easy) which stops itsel f once a pass makes no changes, then we only need to do n comparisons over a sin gle pass: O(n)

You might also like