Sorting algorithms/Cocktail sort: Difference between revisions
Sorting algorithms/Cocktail sort (view source)
Revision as of 23:03, 16 September 2020
, 3 years ago→{{header|R}}: Used a cool R trick and tried to follow pseudo-code more closely. Also added more outputs.
m (→{{header|REXX}}: corrected a typo.) |
ReeceGoding (talk | contribs) (→{{header|R}}: Used a cool R trick and tried to follow pseudo-code more closely. Also added more outputs.) |
||
Line 3,195:
=={{header|R}}==
The previously solution missed out on a cool R trick for swapping items. As R is 1-indexed, we have made some minor adjustments to the given pseudo-code. Otherwise, we have aimed to be faithful to it.
<lang
{
repeat
{▼
for(i in
▲ {
{
swapped<-TRUE
▲ x[i] <- x[i+1]
▲ swapped <- TRUE
}
}
if(!swapped)
{
{
swapped<-TRUE
▲ x[i] <- x[i+1]
}
}
A
}
#Examples taken from the Haxe solution.
ints<-c(1,10,2,5,-1,5,-19,4,23,0)
numerics<-c(1,-3.2,5.2,10.8,-5.7,7.3,3.5,0,-4.1,-9.5)
strings<-c("We","hold","these","truths","to","be","self-evident","that","all","men","are","created","equal")</lang>
{{out}}
<pre>> cocktailSort(ints)
[1] -19 -1 0 1 2 4 5 5 10 23
> cocktailSort(numerics)
[1] -9.5 -5.7 -4.1 -3.2 0.0 1.0 3.5 5.2 7.3 10.8
> cocktailSort(strings)
[1] "all" "are" "be" "created" "equal" "hold" "men"
[8] "self-evident" "that" "these" "to" "truths" "We"</pre>
=={{header|Racket}}==
|