Professional Documents
Culture Documents
New Text Document
New Text Document
New Text Document
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)