ࡱ> AC@ @ bjbj5*5* "W@W@@IZZZZzzz2228j~,'### ' ' ' ' ' ' '$(R*h/'z# ###/'ZZ D'&&&#4Z@z '&# '&&z& @[b2$&&4Z'0'&J+%lJ+&ZZZZJ+z&H##&#####/'/'2q&2 COMP 401, Spring 2008 The Dutch Flag algorithm Given a char array containing 'R', 'W' and 'B', rearrange the elements so that all the 'R's come first; the 'W's come next, and the 'B's come last. Assume that the method swap(char[] s, int i, int j) swaps elements i and j in character array s. Note, that swapping always produces a permutation. final int n=s.length-1; // Index of last element of array. int x=0; int y=0; int z=n; while (true) { if (y==z+1) break; // Unknown part is empty. switch(s[y]) { case 'W': y++; break; // Character is white. case 'B': swap(s,y,z--);break; // Character is blue. default: swap(s,x++,y++); // Character is red. } } End of while(true) // Pre: s is a char array containing 'R', 'W', and 'B' // and s=old_s. // Post: 0<=x<=y<=n+1 // s[0...x-1] are R // s[x...y-1] are W // s[y...n] are B // and s is a permutation of old_s final int n=s.length-1; // Index of last element of array. int x=0; int y=0; int z=n; while (true) { // Inv: 0<=x<=y<=z+1<=n+1 // s[0...x-1] are R // s[x...y-1] are W // s[y...z] are unknown color // s[z+1...n] are B // and s is a permutation of old_s. if (y==z+1) break; // Unknown part is empty. We're done! switch(s[y]) // Look at the character in position y. { case 'W':y++; // Character is white; in correct place. break; case 'B':swap(s,y,z); // Character is blue; swap with last ?. z--; break; default: swap(s,x,y); // Character is red; swap with first W. x++; y++; } } End of while(true)  x is one past the end of the reds. y is one past the end of the whites. z is one before the beginning of the blues. Note that the invariant uses the fact that a universally quantified assertion with an empty domain is true. 401-19. PAGE 1 RED WHITE BLUE R R W R ? W W W B B B B B ? ? x y z  `    ' ) /       J \ ^ _ l m  !$%<RU^ЭХНННХННh#xCJaJhbhbCJaJhbOJQJh#xOJQJhz/OJQJhI&h h7OJQJh h7OJQJhDmOJQJhr)OJQJ hr)CJhDmh|^>hbhr)"jhr)OJQJUmHnHu645_ `  ? z   gd h7 h8p gd h7 h8p @T  * @ V l  * A d { # ^ l m  h8p   !$%<UVWXYZ[\]^? h8p ]gd h7 h8p ?@CDFGHNOPQRST`e϶ϖ hr)CJhr)B*phhDmhr)hDm0JCJaJmHnHuh#xh h70JCJaJ!jh#xh h70JCJUaJhDmCJaJh#xhr)CJaJh#xh#xCJaJhbOJQJh#xCJaJhbhbCJaJ?@RSTXY_`efhiklnoqrtuwxz{}~ !$a$":pDm/ =!"#$%nx(7c}˥ PNG  IHDR&XRrsBITOPLTE338bKGDHIDATxr@@Qۮ˝AcOĬћxMp9|/ \2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%-[5޽hGdzue?# ifO~w{~J[\~[1S楲Fڟ_wJ˦ד'<ٛw2=4xiv?3m!t}jڝY3Z?M7c3c3|x6:#릺nN2qt@3s/>_=IENDB`8@8 Normal_HmH sH tH R@R Heading 1"$@& h8p CJ>@> Heading 2$$@&a$CJDA@D Default Paragraph FontViV  Table Normal :V 44 la (k(No List 4@4 Header  !4 @4 Footer  !.)@. h7 Page Number !$'*-0369<?BEH&%$#"! ,01 !$'*-0369<?BEHK  45_` ?z*@Vl*Ad{ #^lm  !$%<UVWXYZ[\]^?@RSTXY_`efhiklnoqrtuwxz{}~00x00000x@0@0@0@0@0@0@0@0@0@0@0@0@0@00000000000000000000000000000000000000000 0008000080 00x0 0 000 0(0x00x@0@0| H 0H000000H00000000000!00$00'00*00-00000300600900<00<00<00<p*  ?  !l1,b$4_[qZl$ 2@ " (  n  C 0Anetherlands" h  S " \  3 " b  C " V  # " V  # " V  # " V  # " V  # "  V  # "  V  # "  V  # " V   #  " V ! # !" V " # "" V # # #"  V $ # $"  V % # %"   V & # &"  \B ( S D" \B ) S D" \B * S D"\ , 3 ," \B - S D"\ 0 3 0" \ 1 3 1" B S  ?<=>?@ABCDEFGHIJKLMNOPQgqItI tv?st 41st0[t,pt-t*ttt)--t(t%8t$8t#8t"8t!W8t Jf8t&8t.J8t  8t 8t .8t8t8tw8tLw8t@STejos%+^_@FGST333333333333333333@+"#%Q@GSTWY^`dfgijlmoprsuvxy{|~@ST  q #x h7|^>Dmr)b"z/h(<@RS3\0@]]DoH]]@UnknownGz Times New Roman5Symbol3& z ArialO1 CourierCourier New"qhF$Fi+ + !24d== 3H(?COMP 114, Spring 2000weissweissOh+'0 $ @ L X dpxCOMP 114, Spring 2000weiss Normal.dotweiss3Microsoft Word 10.0@^в@KG?BRoot Entry F`bDData 1TableJ+WordDocument"SummaryInformation(0DocumentSummaryInformation88CompObjj  FMicrosoft Word Document MSWordDocWord.Document.89q