# Ch2 - Algorithms Ch2 Sorting(courtesy of Prof.Pecelli with...

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

Algorithms - Ch2 – Sorting ( courtesy of Prof.Pecelli with some changes from Prof. Daniels ) 9/9/2011 91.404 - Algorithms 1

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

View Full Document
Algorithm Description Algorithm Description : -Pseudocode see conventions on p. 20-22 of textbook -Correctness Justification -“Mechanical” “ s dvertised” - As-Advertised -“Asymptotic” Analysis -Execution Time and/or -Storage Required 9/9/2011 91.404 - Algorithms 2
Algorithms - Ch2 - Sorting 1. Problem first described in a "human" language - usually in English here… 2. Problem must be restated so all ambiguity is removed… this may still be in English. 3. Problem must be restated so that it can be unambiguously translated into a computer language… this is usually done using some form of 'pseudo-code'. 4. Problem must be restated in 'code'… using an appropriate computer language. 9/9/2011 91.404 - Algorithms 3

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

View Full Document
Algorithms - Ch2 - Sorting Sorting Input: a sequence of numbers a 1 , a 2 , …, a n . Output: a permutation (reordering) of a' 1 , a' 2 , …, a' n such that a' 1 a' 2 a' n . 9/9/2011 91.404 - Algorithms 4
Algorithms - Ch2 - Sorting Here is the algorithm for Insertion Sort - as in 91.102 9/9/2011 91.404 - Algorithms 5

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

View Full Document
Algorithms - Ch2 - Sorting Correctness - how? Loop Invariants: statements such that: 1. Initialization: The statement is true before the first execution of the body of the loop. 2. Maintenance: If the statement is true before any iteration of the loop, it remains true before the next iteration 3. Termination: At loop termination, the statement can be used to derive a property that helps to show the algorithm correct. How do we relate this to the SPECIFIC problem we have? hat's YOUR problem ll anyone can do is show enough That s YOUR problem - all anyone can do is show enough examples so that you have (hopefully) enough templates to figure out your own. 9/9/2011 91.404 - Algorithms 6
Algorithms - Ch2 - Sorting 1. Initialization: since j = 2, the supposedly sorted part of the array is A = A[1. .j-1] = A[1. .1]: any array of ONE element is sorted (obvious, right?). 2. Maintenance: at the beginning of each iteration we have a sorted array A[1. .j-1], and an element A[j] that is to be inserted in the correct position, possibly moving some elements in A[1. .j- 1]. We have to convince ourselves that this DOES the job we want: A[1. .j] IS sorted before the next iteration. Does it? Keep challenging it until you are completely convinced it works. 3. Termination: j = n + 1. But, by part 2, A[1. .n] IS SORTED. 9/9/2011 91.404 - Algorithms 7

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

View Full Document
Algorithms - Ch2 - Sorting 9/9/2011 91.404 - Algorithms 8
Algorithms - Ch2 - Sorting Analysis…Add things up, to get T(n), the total time.

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.

{[ snackBarMessage ]}

### Page1 / 27

Ch2 - Algorithms Ch2 Sorting(courtesy of Prof.Pecelli with...

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

View Full Document
Ask a homework question - tutors are online