That nn now lets look at how we generally compare the

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: e use a notation called big-O. We big So order n2 is written O(n2). Applied Programming So we need to look at best/worst case So best/worst performance. All three of the sorting algorithms discussed so far All are O(n2). are Is there some initial arrangement of the data which Is makes the algorithm perform better or worse than O(n2)? 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 Thus, 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 For that is needed to recognise this. No exchanges are required. So it becomes O(n). No But, if the data is completely reversed, the full set But, of comparisons and all the moves will be needed, so worst case is O(n2). so On average, less than n(n-1)/2 comparisons will 1)/2 be needed. be Applied Programming 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 Each already sorted data. So O(n). already 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 So Bubble and Insertion. This is only on comparisons – not moves. Applied Programming...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online