This preview shows page 1. Sign up to view the full content.
Unformatted text preview: e use a notation called big-O.
So order n2 is written O(n2). Applied Programming So we need to look at best/worst case
performance. All three of the sorting algorithms discussed so far
are Is there some initial arrangement of the data which
makes the algorithm perform better or worse than
O(n This doesn't mean that they're all equal. Selection Sort has constant performance. They differ in the constant multiplier. Thus, if the data is already sorted, it will take just
as long as if the data is in reverse order.
as But, they all act the same as n increases. Programming
Applied Applied Programming For Bubble Sort, one pass through the data is all
that is needed to recognise this.
No exchanges are required. So it becomes O(n).
But, if the data is completely reversed, the full set
of comparisons and all the moves will be needed,
so worst case is O(n2).
On average, less than n(n-1)/2 comparisons will
Applied Can we improve on O(n2)? Applied Programming Insertion Sort's best case is sorted.
Each new entry is compared to the last of the
already sorted data. So O(n).
The worst case is again reversed data.
This is O(n2).
The average is about n(n-1)/4, still O(n2).
So Selection seems to be worst, followed by
Bubble and Insertion. This is only on comparisons
– not moves.
View Full Document
- Spring '14