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

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

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

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

View Full Document
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 . From the postcondition of Sort : If A [ i ] < A [ k ] , then i < k ; hence, there are fewer than k elements of A less than A [ k ] .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern