# lec026 - 1 Insertion Sort for(int i = 1 i< a.length i...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: 1 Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } Complexity ▲ Space/Memory ▲ Time – Count a particular operation – Count number of steps – Asymptotic complexity Comparison Count for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j] ; j--) a[j + 1] = a[j]; a[j + 1] = t; } Comparison Count ▲ Pick an instance characteristic … n, n = a.length for insertion sort ▲ Determine count as a function of this instance characteristic. Comparison Count for (j = i - 1; j >= 0 && t < a[j] ; j--) a[j + 1] = a[j]; How many comparisons are made? Comparison Count for (j = i - 1; j >= 0 && t < a[j] ; j--) a[j + 1] = a[j]; number of compares depends on as and t as well as on i 2 Comparison Count ¾ Worst-case count = maximum count ¾ Best-case count = minimum count ¾ Average count Worst-Case Comparison Count...
View Full Document

{[ snackBarMessage ]}

### Page1 / 4

lec026 - 1 Insertion Sort for(int i = 1 i< a.length i...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online