Sorting algorithms/Cocktail sort with shifting bounds: Difference between revisions

m
syntax highlighting fixup automation
(Added solution for Action!)
m (syntax highlighting fixup automation)
Line 45:
Pseudocode for the &nbsp; <big> 2<sup>nd</sup> </big> &nbsp; algorithm &nbsp; (from
[[wp:Cocktail sort|Wikipedia]]) &nbsp; with an added comment and changed indentations:
<langsyntaxhighlight lang="matlab">function A = cocktailShakerSort(A)
% `beginIdx` and `endIdx` marks the first and last index to check.
beginIdx = 1;
Line 75:
beginIdx = newBeginIdx + 1;
end
end</langsyntaxhighlight>
<big>'''%'''</big> &nbsp; indicates a comment, &nbsp; and &nbsp; '''deal''' &nbsp; indicates a &nbsp; ''swap''.
 
Line 92:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F cocktailshiftingbounds(&A)
V beginIdx = 0
V endIdx = A.len - 1
Line 113:
V test1 = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
cocktailshiftingbounds(&test1)
print(test1)</langsyntaxhighlight>
 
{{out}}
Line 123:
For maximum compatibility, this program uses only the basic instruction set.
The program uses also HLASM structured macros (DO,ENDDO,IF,ELSE,ENDIF) for readability and two ASSIST/360 macros (XDECO,XPRNT) to keep the code as short as possible.
<langsyntaxhighlight lang="360asm">* Cocktail sort with shifting bounds 10/05/2020
COCKSHIS CSECT
USING COCKSHIS,R13 base register
Line 206:
REGEQU
RI EQU 6 i
END COCKSHIS </langsyntaxhighlight>
{{out}}
<pre>
Line 214:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program cocktailSortbound64.s */
Line 401:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Line 470:
Test(c,8)
Test(d,12)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cocktail_sort_with_shifting_bounds.png Screenshot from Atari 8-bit computer]
Line 497:
=={{header|ALGOL 60}}==
{{works with|A60}}
<langsyntaxhighlight lang="algol60">begin
comment Sorting algorithms/Cocktail sort with shifting bounds - Algol 60;
integer nA;
Line 553:
writetable(1,nA)
end
end </langsyntaxhighlight>
{{out}}
<pre>
Line 562:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program cocktailSortBound.s */
Line 736:
.include "../affichage.inc"
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">cocktailShiftSort: function [items][
a: new items
beginIdx: 0
Line 774:
]
 
print cocktailShiftSort [3 1 2 8 5 7 9 4 6]</langsyntaxhighlight>
 
{{out}}
Line 781:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">cocktailShakerSort(A){
beginIdx := 1
endIdx := A.Count() - 1
Line 806:
beginIdx := newBeginIdx + 1
}
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">A := [8,6,4,3,5,2,7,1]
cocktailShakerSort(A)
for k, v in A
output .= v ", "
MsgBox % "[" Trim(output, ", ") "]" ; show output</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 877:
print(a, len);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 886:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cassert>
#include <iostream>
Line 938:
print(v.begin(), v.end());
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 949:
=={{header|FreeBASIC}}==
{{trans|VBA}}
<langsyntaxhighlight lang="freebasic">
Sub cocktailShakerSort(bs() As Long)
Dim As Long i, lb = Lbound(bs), ub = Ubound(bs) -1
Line 994:
Print !"\n--- terminado, pulsa RETURN---"
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,004:
=={{header|Go}}==
If you run this a few times, the figures for 8K random numbers upwards are quite stable at around the levels shown.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,107:
fmt.Printf(" %2dk %0.3f\n", n/1000, sum/runs)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,129:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class CocktailSort {
static void main(String[] args) {
Integer[] array = [ 5, 1, -6, 12, 3, 13, 2, 4, 0, 15 ]
Line 1,173:
array[j] = tmp
}
}</langsyntaxhighlight>
{{out}}
<pre>before: [5, 1, -6, 12, 3, 13, 2, 4, 0, 15]
Line 1,179:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class CocktailSort {
Line 1,224:
array[j] = tmp;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,235:
The implementation chosen is to extend the native Julia base sort! function, which by default in base Julia supports insertion sort,
quick sort, partial quick sort, and merge sort.
<langsyntaxhighlight lang="julia">module CocktailShakerSorts
 
using Base.Order, Base.Sort
Line 1,288:
println("\nUsing CocktailShakerSort:")
@btime cocktailshakersort!([5, 8, 2, 0, 6, 1, 9, 3, 4])
</langsyntaxhighlight>{{out}}
<pre>
[5, 8, 2, 0, 6, 1, 9, 3, 4] => [0, 1, 2, 3, 4, 5, 6, 8, 9] => [9, 8, 6, 5, 4, 3, 2, 1, 0]
Line 1,301:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">fun <T> swap(array: Array<T>, i: Int, j: Int) {
val temp = array[i]
array[i] = array[j]
Line 1,344:
cocktailSort(array)
println("after: ${array.contentToString()}")
}</langsyntaxhighlight>
{{out}}
<pre>before: [5, 1, -6, 12, 3, 13, 2, 4, 0, 15]
Line 1,350:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[CocktailShakerSort]
CocktailShakerSort[in_List] :=
Module[{x = in, swapped, begin = 1, end = Length[in] - 1},
Line 1,378:
x
]
CocktailShakerSort[{44, 21, 5, 88, 52, 44, 36, 93, 66, 18, 88, 61, 45, 47, 47, 68, 19, 60}]</langsyntaxhighlight>
{{out}}
<pre>{5, 18, 19, 21, 36, 44, 44, 45, 47, 47, 52, 60, 61, 66, 68, 88, 88, 93}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">proc cocktailShakerSort[T](a: var openarray[T]) =
 
var beginIdx = 0
Line 1,407:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
cocktailShakerSort a
echo a</langsyntaxhighlight>
 
{{out}}
Line 1,413:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,444:
}
 
say join ' ', cocktail_sort( <t h e q u i c k b r o w n f o x j u m p s o v e r t h e l a z y d o g> );</langsyntaxhighlight>
{{out}}
<pre>a b c d e e e f g h h i j k l m n o o o o p q r r s t t u u v w x y z</pre>
Line 1,450:
=={{header|Phix}}==
Averages 7 or 8% better than [[Sorting_algorithms/Cocktail_sort#Phix]] which already shifted the bounds, however this shifts >1 (sometimes).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,487:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;">?{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cocktailShakerSort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)}</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"three"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"four"</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">?{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cocktailShakerSort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)}</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,495:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
"""
 
Line 1,536:
cocktailshiftingbounds(test2)
print(''.join(test2))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,547:
{{works with|Rakudo|2020.05}}
 
<syntaxhighlight lang="raku" perl6line>sub cocktail_sort ( @a ) {
my ($min, $max) = 0, +@a - 2;
loop {
Line 1,580:
 
my @weights = (flat 0..9, 'A'..'F').roll(2 + ^4 .roll).join xx 100;
say @weights.sort.Str eq @weights.&cocktail_sort.Str ?? 'ok' !! 'not ok';</langsyntaxhighlight>
 
=={{header|REXX}}==
This REXX version can handle array elements that may contain blanks or spaces.
<langsyntaxhighlight lang="rexx">/*REXX program sorts an array using the cocktail─sort method with shifting bounds. */
call gen /*generate some array elements. */
call show 'before sort' /*show unsorted array elements. */
Line 1,639:
say 'element' right(j, w) arg(1)":" @.j
end /*j*/ /* ↑ */
return /* └─────max width of any line.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
Line 1,685:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn cocktail_shaker_sort<T: PartialOrd>(a: &mut [T]) {
let mut begin = 0;
let mut end = a.len();
Line 1,719:
cocktail_shaker_sort(&mut v);
println!("after: {:?}", v);
}</langsyntaxhighlight>
 
{{out}}
Line 1,729:
=={{header|Swift}}==
{{trans|Rust}}
<langsyntaxhighlight lang="swift">func cocktailShakerSort<T: Comparable>(_ a: inout [T]) {
var begin = 0
var end = a.count
Line 1,768:
print("before: \(array2)")
cocktailShakerSort(&array2)
print(" after: \(array2)")</langsyntaxhighlight>
 
{{out}}
Line 1,779:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">' Sorting algorithms/Cocktail sort with shifting bounds - VBA
 
Function cocktailShakerSort(ByVal A As Variant) As Variant
Line 1,812:
Debug.Print Join(B, ", ")
Debug.Print Join(cocktailShakerSort(B), ", ")
End Sub</langsyntaxhighlight>
{{out}}
<pre>
Line 1,821:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">' Sorting algorithms/Cocktail sort with shifting bounds - VBScript
 
Function cocktailShakerSort(ByVal A)
Line 1,852:
Next
Wscript.Echo Join(cocktailShakerSort(B)," ")
</langsyntaxhighlight>
{{out}}
<pre>
Line 1,860:
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|9.0}}
<langsyntaxhighlight lang="vbnet">' Sorting algorithms/Cocktail sort with shifting bounds - VB.Net
Private Sub Cocktail_Shaker_Sort()
Dim A(20), tmp As Long 'or Integer Long Single Double String
Line 1,891:
'Display the sorted list
Debug.Print(String.Join(", ", A))
End Sub 'Cocktail_Shaker_Sort </langsyntaxhighlight>
{{out}}
<pre>
Line 1,903:
 
Ratios are noticeably less than the Go example (more in keeping with the REXX results in the talk page) and vary more if the script is run a few times. Frankly, I don't know why this is.
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "random" for Random
 
Line 2,003:
}
System.print(" %(Fmt.d(2, (n/1000).floor))k %(Fmt.f(0, sum/runs, 3))")
}</langsyntaxhighlight>
 
{{out}}
10,327

edits