Dutch national flag problem: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Tabs -> 4spaces) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 581: | Line 581: | ||
Non-sorted: rbwww |
Non-sorted: rbwww |
||
Sorted: rwwwb</pre> |
Sorted: rwwwb</pre> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <algorithm> |
|||
#include <iostream> |
|||
// Dutch national flag problem |
|||
template <typename BidIt, typename T> |
|||
void dnf_partition(BidIt first, BidIt last, const T& low, const T& high) |
|||
{ |
|||
for (BidIt next = first; next != last; ) { |
|||
if (*next < low) { |
|||
std::iter_swap(first++, next++); |
|||
} else if (!(*next < high)) { |
|||
std::iter_swap(next, --last); |
|||
} else { |
|||
++next; |
|||
} |
|||
} |
|||
} |
|||
enum Colors { RED, WHITE, BLUE }; |
|||
void print(const Colors *balls, size_t size) |
|||
{ |
|||
static const char *label[] = { "red", "white", "blue" }; |
|||
std::cout << "Balls:"; |
|||
for (size_t i = 0; i < size; ++i) { |
|||
std::cout << ' ' << label[balls[i]]; |
|||
} |
|||
std::cout << "\nSorted: " << std::boolalpha << std::is_sorted(balls, balls + size) << '\n'; |
|||
} |
|||
int main() |
|||
{ |
|||
Colors balls[] = { RED, WHITE, BLUE, RED, WHITE, BLUE, RED, WHITE, BLUE }; |
|||
std::random_shuffle(balls, balls + 9); |
|||
print(balls, 9); |
|||
dnf_partition(balls, balls + 9, WHITE, BLUE); |
|||
print(balls, 9); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Balls: blue white red blue red blue white red white |
|||
Sorted: false |
|||
Balls: red red red white white white blue blue blue |
|||
Sorted: true |
|||
</pre> |
|||
=={{header|C_sharp|C#}}== |
=={{header|C_sharp|C#}}== |
||
<lang csharp>using System; |
<lang csharp>using System; |
||
Line 658: | Line 709: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <algorithm> |
|||
#include <iostream> |
|||
// Dutch national flag problem |
|||
template <typename BidIt, typename T> |
|||
void dnf_partition(BidIt first, BidIt last, const T& low, const T& high) |
|||
{ |
|||
for (BidIt next = first; next != last; ) { |
|||
if (*next < low) { |
|||
std::iter_swap(first++, next++); |
|||
} else if (!(*next < high)) { |
|||
std::iter_swap(next, --last); |
|||
} else { |
|||
++next; |
|||
} |
|||
} |
|||
} |
|||
enum Colors { RED, WHITE, BLUE }; |
|||
void print(const Colors *balls, size_t size) |
|||
{ |
|||
static const char *label[] = { "red", "white", "blue" }; |
|||
std::cout << "Balls:"; |
|||
for (size_t i = 0; i < size; ++i) { |
|||
std::cout << ' ' << label[balls[i]]; |
|||
} |
|||
std::cout << "\nSorted: " << std::boolalpha << std::is_sorted(balls, balls + size) << '\n'; |
|||
} |
|||
int main() |
|||
{ |
|||
Colors balls[] = { RED, WHITE, BLUE, RED, WHITE, BLUE, RED, WHITE, BLUE }; |
|||
std::random_shuffle(balls, balls + 9); |
|||
print(balls, 9); |
|||
dnf_partition(balls, balls + 9, WHITE, BLUE); |
|||
print(balls, 9); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Balls: blue white red blue red blue white red white |
|||
Sorted: false |
|||
Balls: red red red white white white blue blue blue |
|||
Sorted: true |
|||
</pre> |
|||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
Line 1,856: | Line 1,857: | ||
Is sorted: true |
Is sorted: true |
||
</pre> |
</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 2,207: | Line 2,206: | ||
are_ordered($balls) or die "Incorrect\n";</lang> |
are_ordered($balls) or die "Incorrect\n";</lang> |
||
You can run it with no parameters, it sorts 10 balls in such a case. If you provide one parameter, it is used as the number of balls. The second parameter turns on debugging that shows how the balls are being swapped. |
You can run it with no parameters, it sorts 10 balls in such a case. If you provide one parameter, it is used as the number of balls. The second parameter turns on debugging that shows how the balls are being swapped. |
||
=={{header|Perl 6}}== |
|||
Here are five ways to do it, all one liners (apart from the test apparatus). |
|||
<lang perl6>enum NL <red white blue>; |
|||
my @colors; |
|||
sub how'bout (&this-way) { |
|||
sub show { |
|||
say @colors; |
|||
say "Ordered: ", [<=] @colors; |
|||
} |
|||
@colors = NL.roll(20); |
|||
show; |
|||
this-way; |
|||
show; |
|||
say ''; |
|||
} |
|||
say "Using functional sort"; |
|||
how'bout { @colors = sort *.value, @colors } |
|||
say "Using in-place sort"; |
|||
how'bout { @colors .= sort: *.value } |
|||
say "Using a Bag"; |
|||
how'bout { @colors = flat red, white, blue Zxx bag(@colors».key)<red white blue> } |
|||
say "Using the classify method"; |
|||
how'bout { @colors = flat (.list for %(@colors.classify: *.value){0,1,2}) } |
|||
say "Using multiple greps"; |
|||
how'bout { @colors = flat (.grep(red), .grep(white), .grep(blue) given @colors) }</lang> |
|||
{{out}} |
|||
<pre>Using functional sort |
|||
red red white white red red red red red red red white red white red red red white white white |
|||
Ordered: False |
|||
red red red red red red red red red red red red red white white white white white white white |
|||
Ordered: True |
|||
Using in-place sort |
|||
red blue white red white blue white blue red white blue blue blue red white white red blue red blue |
|||
Ordered: False |
|||
red red red red red red white white white white white white blue blue blue blue blue blue blue blue |
|||
Ordered: True |
|||
Using a Bag |
|||
red blue blue blue white red white red white blue blue red red red red blue blue red white blue |
|||
Ordered: False |
|||
red red red red red red red red white white white white blue blue blue blue blue blue blue blue |
|||
Ordered: True |
|||
Using the classify method |
|||
blue red white blue blue white white red blue red red white red blue white white red blue red white |
|||
Ordered: False |
|||
red red red red red red red white white white white white white white blue blue blue blue blue blue |
|||
Ordered: True |
|||
Using multiple greps |
|||
red white blue white white red blue white red white red white white white white white red red blue red |
|||
Ordered: False |
|||
red red red red red red red white white white white white white white white white white blue blue blue |
|||
Ordered: True</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,692: | Line 2,628: | ||
==> OK |
==> OK |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Here are five ways to do it, all one liners (apart from the test apparatus). |
|||
<lang perl6>enum NL <red white blue>; |
|||
my @colors; |
|||
sub how'bout (&this-way) { |
|||
sub show { |
|||
say @colors; |
|||
say "Ordered: ", [<=] @colors; |
|||
} |
|||
@colors = NL.roll(20); |
|||
show; |
|||
this-way; |
|||
show; |
|||
say ''; |
|||
} |
|||
say "Using functional sort"; |
|||
how'bout { @colors = sort *.value, @colors } |
|||
say "Using in-place sort"; |
|||
how'bout { @colors .= sort: *.value } |
|||
say "Using a Bag"; |
|||
how'bout { @colors = flat red, white, blue Zxx bag(@colors».key)<red white blue> } |
|||
say "Using the classify method"; |
|||
how'bout { @colors = flat (.list for %(@colors.classify: *.value){0,1,2}) } |
|||
say "Using multiple greps"; |
|||
how'bout { @colors = flat (.grep(red), .grep(white), .grep(blue) given @colors) }</lang> |
|||
{{out}} |
|||
<pre>Using functional sort |
|||
red red white white red red red red red red red white red white red red red white white white |
|||
Ordered: False |
|||
red red red red red red red red red red red red red white white white white white white white |
|||
Ordered: True |
|||
Using in-place sort |
|||
red blue white red white blue white blue red white blue blue blue red white white red blue red blue |
|||
Ordered: False |
|||
red red red red red red white white white white white white blue blue blue blue blue blue blue blue |
|||
Ordered: True |
|||
Using a Bag |
|||
red blue blue blue white red white red white blue blue red red red red blue blue red white blue |
|||
Ordered: False |
|||
red red red red red red red red white white white white blue blue blue blue blue blue blue blue |
|||
Ordered: True |
|||
Using the classify method |
|||
blue red white blue blue white white red blue red red white red blue white white red blue red white |
|||
Ordered: False |
|||
red red red red red red red white white white white white white white blue blue blue blue blue blue |
|||
Ordered: True |
|||
Using multiple greps |
|||
red white blue white white red blue white red white red white white white white white red red blue red |
|||
Ordered: False |
|||
red red red red red red red white white white white white white white white white white blue blue blue |
|||
Ordered: True</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,790: | Line 2,790: | ||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<lang ring> |
<lang ring> |