# lec02a - Complexity Analysis Resource needs Insertion Sort...

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

Complexity Analysis Resource needs

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

View Full Document
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

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

View Full Document
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.

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

View Full Document
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 a[]s and t as well as on i

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

View Full Document
Comparison Count Worst-case count = maximum count Best-case count = minimum count Average count
Worst-Case Comparison Count for (j = i - 1; j >= 0 && t < a[j] ; j--) a[j + 1] = a[j]; a = [1, 2, 3, 4] and t = 0 => 4 compares a = [1,2,3,…,i] and t = 0 => i compares

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

View Full Document
Worst-Case Comparison Count
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/18/2012 for the course COP 3530 taught by Professor Davis during the Fall '08 term at University of Florida.

### Page1 / 22

lec02a - Complexity Analysis Resource needs Insertion Sort...

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

View Full Document
Ask a homework question - tutors are online