lec022 - 1 Insertion Sort for (int i = 1; i <...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
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 2 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. 3 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 4 Comparison Count Worst-case count = maximum count Best-case count = minimum count Average count Worst-Case Comparison Count...
View Full Document

Page1 / 10

lec022 - 1 Insertion Sort for (int i = 1; i <...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online