2-twoup - Algorithm Correctness An algorithm is said to be...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Algorithm Correctness An algorithm is said to be correct if, whenever the precondition is true at the beginning of execution, the postcondition is true at the end of execution. Claim: SimpleSelect is correct. Note that we do not need an algorithm for Sort in order to prove the correctness of SimpleSelect . 1 Precondition: A [1 ..n ] is an array of Number s, 1 ≤ k ≤ n , k and n are Nat s. Postcondition: Returns the value x in A [1 ..n ] such that fewer than k elements of A [1 ..n ] are strictly less than x , and at least k elements of A [1 ..n ] are less than or equal to x . The elements of A [1 ..n ] may be permuted. SimpleSelect ( A [1 ..n ] , k ) Sort ( A [1 ..n ] ) return A [ k ] Precondition: A [1 ..n ] is an array of Number s, n is a Nat . Postcondition: A [1 ..n ] is a permutation of its initial values such that for 1 ≤ i < j ≤ n , A [ i ] ≤ A [ j ] . Sort ( A [1 ..n ] ) 2 Proof • Assume that the precondition for SimpleSelect ( A [1 ..n ]) is satisfied initially. • Then the precondition for Sort ( A [1 ..n ]) is satisfied. • Let A ′ denote the final value A ....
View Full Document

This note was uploaded on 09/06/2009 for the course CIS 11274 taught by Professor Howell during the Spring '09 term at Kansas State University.

Page1 / 6

2-twoup - Algorithm Correctness An algorithm is said to be...

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

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