This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: The Selection Problem Revisited We will show how to reduce this problem to the following three: the Dutch national flag problem ; the problem of finding a median; and a smaller instance of itself. The resulting algorithm will contain both iteration and recursion. 1 The Dutch National Flag Problem Input: An array of items, each having a value of red , white , or blue . Output: A permutation of the items such that all red items precede all white items, which precede all blue items. 2 A Restriction We will restrict the input to be an array of numbers. For a given value p , we will consider a number x to be: red if x < p ; white if x = p ; or blue if x > p . The problem is then to place all values less than p before all values equal to p , followed by all values greater than p . 3 Specification for DutchFlag Precondition: A [ lo .. hi ] is an array of Number s, lo and hi are Int s such that hi lo 1 , and p is a Number . Postcondition: A [ lo .. hi ] is a permutation of its original values such that all items less than p precede all items equal to p , which in turn precede all items greater than p . Returns an array N [1 .. 3] in which N  is the number of items less than p , N  is the number of items...
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.
- Spring '09