Sorting algorithms/Cocktail sort: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: replaced broken algorithm, fiddled with layout) |
No edit summary |
||
Line 2,370: | Line 2,370: | ||
print(list[i]j) |
print(list[i]j) |
||
end</lang> |
end</lang> |
||
=={{header|M2000 Interpreter}}== |
|||
<lang M2000 Interpreter |
|||
Module cocktailSort { |
|||
k=(3,2,1) |
|||
print k |
|||
cocktailSort(k) |
|||
print k |
|||
k=("c","b","a") |
|||
print k |
|||
cocktailSortString(k) |
|||
print k |
|||
Dim a(5) |
|||
a(0)=1,2,5,6,3 |
|||
print a() |
|||
cocktailSort(a()) |
|||
print a() |
|||
End |
|||
Sub cocktailSort(a) |
|||
\\ this is like Link a to a() but using new for a() - shadow any a() |
|||
push &a : read new &a() |
|||
local swapped, lenA2=len(a)-2 |
|||
do |
|||
for i=0 to lenA2 |
|||
if a(i) > a(i+1) then swap a(i), a(i+1): swapped = true |
|||
next |
|||
if swapped then |
|||
swapped~ |
|||
for i=lenA2 to 0 |
|||
if a(i) > a(i+1) then swap a(i), a(i+1): swapped = true |
|||
next |
|||
end if |
|||
until not swapped |
|||
End Sub |
|||
Sub cocktailSortString(a) |
|||
push &a : read new &a$() |
|||
local swapped, lenA2=len(a)-2 |
|||
do |
|||
for i=0 to lenA2 |
|||
if a$(i) > a$(i+1) then |
|||
swap a$(i), a$(i+1) |
|||
swapped = true |
|||
end if |
|||
next |
|||
if swapped then |
|||
swapped~ |
|||
for i=lenA2 to 0 |
|||
if a$(i) > a$(i+1) then |
|||
swap a$(i), a$(i+1) |
|||
swapped = true |
|||
end if |
|||
next |
|||
end if |
|||
until not swapped |
|||
End Sub |
|||
} |
|||
cocktailSort |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
3 2 1 |
|||
1 2 3 |
|||
c b a |
|||
a b c |
|||
1 2 5 6 3 |
|||
1 2 3 5 6 |
|||
</pre> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |