Sorting algorithms/Stooge sort: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 25:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F stoogesort(&l, i, j) -> N
I l[j] < l[i]
swap(&l[i], &l[j])
Line 39:
V data = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7]
stooge(&data)
print(data)</langsyntaxhighlight>
 
{{out}}
Line 48:
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<langsyntaxhighlight Actionlang="action!">DEFINE MAX_COUNT="100"
INT ARRAY stack(MAX_COUNT)
INT stackSize
Line 127:
Test(c,8)
Test(d,12)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Stooge_sort.png Screenshot from Atari 8-bit computer]
Line 156:
Using slices and 'First / 'Last removes the need for I / J parameters.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
procedure Stooge is
type Integer_Array is array (Positive range <>) of Integer;
Line 187:
end loop;
Ada.Text_IO.New_Line;
end Stooge;</langsyntaxhighlight>
 
{{out}}
Line 194:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># swaps the values of the two REF INTs #
PRIO =:= = 1;
OP =:= = ( REF INT a, b )VOID: ( INT t := a; a := b; b := t );
Line 220:
# test the stooge sort #
[]INT data = ( 67, -201, 0, 9, 9, 231, 4 );
print( ( "before: ", data, newline, "after: ", stooge sort( data ), newline ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 228:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">StoogeSort(L, i:=1, j:=""){
if !j
j := L.MaxIndex()
Line 243:
}
return L
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % map(StoogeSort([123,51,6,73,3,-12,0]))
return
 
Line 251:
res .= v ","
return trim(res, ",")
}</langsyntaxhighlight>
Outputs:<pre>-12,0,3,6,51,73,123</pre>
 
Line 260:
It ''definitely'' does '''not''' work with QBasic.
 
<langsyntaxhighlight lang="qbasic">DECLARE SUB stoogesort (L() AS LONG, i AS LONG, j AS LONG)
 
RANDOMIZE TIMER
Line 293:
stoogesort L(), i, j - t
END IF
END SUB</langsyntaxhighlight>
 
{{out}}
Line 304:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> DIM test%(9)
test%() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
PROCstoogesort(test%(), 0, DIM(test%(),1))
Line 322:
PROCstoogesort(l%(), i%, j%-t%)
ENDIF
ENDPROC</langsyntaxhighlight>
{{out}}
<pre>
Line 329:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let stoogesort(L, i, j) be
Line 357:
stoogesort(array, 0, length-1)
write("After: ", array, length)
$)</langsyntaxhighlight>
{{out}}
<pre>Before: 4 65 2 -31 0 99 2 83 782 1
Line 363:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define SWAP(r,s) do{ t=r; r=s; s=t; } while(0)
Line 393:
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight Clang="c sharp|Cc#"> public static void Sort<T>(List<T> list) where T : IComparable {
if (list.Count > 1) {
StoogeSort(list, 0, list.Count - 1);
Line 413:
StoogeSort(L, i, j - t);
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
<lang Cpp>
#include <iostream>
#include <time.h>
Line 447:
return system( "pause" );
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 464:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn swap [v x y]
(assoc! v y (v x) x (v y)))
 
Line 476:
(stooge-sort v (+ i t) j)
(stooge-sort v i (- j t))))
v))</langsyntaxhighlight>
 
=={{header|COBOL}}==
{{works with|GNU Cobol}}
<langsyntaxhighlight lang="cobol"> >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. stooge-sort-test.
Line 559:
END-IF
.
END PROGRAM stooge-sort.</langsyntaxhighlight>
 
{{out}}
Line 568:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun stooge-sort (vector &optional (i 0) (j (1- (length vector))))
(when (> (aref vector i) (aref vector j))
(rotatef (aref vector i) (aref vector j)))
Line 576:
(stooge-sort vector (+ i third) j)
(stooge-sort vector i (- j third))))
vector)</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.array;
 
void stoogeSort(T)(T[] seq) pure nothrow {
Line 597:
data.stoogeSort();
writeln(data);
}</langsyntaxhighlight>
{{out}}
[-6, -5, -2, 1, 3, 3, 4, 5, 7, 10]
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
STOOGE_SORT
Line 632:
end
end
</syntaxhighlight>
</lang>
Test:
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 667:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 678:
=={{header|Elena}}==
ELENA 4.x :
<langsyntaxhighlight lang="elena">import extensions;
import system'routines;
Line 708:
console.printLine("before:", list.asEnumerable());
console.printLine("after:", list.stoogeSort().asEnumerable())
}</langsyntaxhighlight>
{{out}}
<pre>
Line 716:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Sort do
def stooge_sort(list) do
stooge_sort(List.to_tuple(list), 0, length(list)-1) |> Tuple.to_list
Line 738:
 
(for _ <- 1..20, do: :rand.uniform(20)) |> IO.inspect
|> Sort.stooge_sort |> IO.inspect</langsyntaxhighlight>
 
{{out}}
Line 747:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function stooge(sequence s, integer i, integer j)
object temp
integer t
Line 771:
 
? s
? stoogesort(s)</langsyntaxhighlight>
 
{{out}}
Line 779:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel locals math prettyprint sequences ;
IN: rosetta-code.stooge-sort
 
Line 803:
{ 1 4 5 3 -6 3 7 10 -2 -5 } stooge-sort . ;
 
MAIN: stooge-sort-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 811:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program Stooge
implicit none
 
Line 841:
 
end subroutine
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 23-10-2016
' compile with: fbc -s console
 
Line 884:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Unsorted
Line 893:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 917:
stoogesort(a[:len(a)-t])
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List
import Control.Arrow
import Control.Monad
Line 940:
xs'
| xs!!j < xs!!i = swapElems xs i j
| otherwise = xs</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="haskell">*Main> stoogeSort [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7]
[-6,-5,-3,-2,1,3,3,4,5,5,7,7,7,9,10]</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main() #: demonstrate various ways to sort a list and string
demosort(stoogesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 967:
}
return X # X must be returned and assigned to sort a string
end</langsyntaxhighlight>
 
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]].
Line 982:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "StoogSrt.bas"
110 RANDOMIZE
120 NUMERIC ARRAY(5 TO 16)
Line 1,009:
350 CALL STOOGESORT(A,I,J-T)
360 END IF
370 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">swapElems=: |.@:{`[`]}
 
stoogeSort=: 3 : 0
Line 1,022:
(x-0,t) stoogeSort (x+t,0) stoogeSort (x-0,t) stoogeSort y
else. y end.
)</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="j"> (,: stoogeSort) ?~13
3 10 8 4 7 12 1 2 11 6 5 9 0
0 1 2 3 4 5 6 7 8 9 10 11 12
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class Stooge {
Line 1,056:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[-6, -5, -2, 1, 3, 3, 4, 5, 7, 10]</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">function stoogeSort (array, i, j) {
if (j === undefined) {
j = array.length - 1;
Line 1,082:
stoogeSort(array, i, j-t);
}
};</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="javascript">arr = [9,1,3,10,13,4,2];
stoogeSort(arr);
console.log(arr);</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3, 4, 9, 10, 13]</pre>
Line 1,092:
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq">def stoogesort:
def swap(i;j): .[i] as $t | .[i] = .[j] | .[j] = $t;
Line 1,108:
end;
 
[., 0, length-1] | ss;</langsyntaxhighlight>
'''Example'''
<langsyntaxhighlight lang="jq">([],
[1],
[1,2],
[1,3,2,4],
[1,4,5,3,-6,3,7,10,-2,-5]
) | stoogesort</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -c -n -f stooge_sort.jq
[]
[1]
[1,2]
[1,2,3,4]
[-6,-5,-2,1,3,3,4,5,7,10]</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Matlab}}
<langsyntaxhighlight lang="julia">function stoogesort!(a::Array, i::Int=1, j::Int=length(a))
if a[j] < a[i]
a[[i, j]] = a[[j, i]];
Line 1,143:
 
x = randn(10)
@show x stoogesort!(x)</langsyntaxhighlight>
 
{{out}}
Line 1,150:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.0
 
fun stoogeSort(a: IntArray, i: Int, j: Int) {
Line 1,171:
stoogeSort(a, 0, a.size - 1)
println("Sorted : ${a.asList()}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,184:
and made generic with an optional predicate parameter.
 
<langsyntaxhighlight lang="lua">
local Y = function (f)
return (function(x) return x(x) end)(function(x) return f(function(...) return x(x)(...) end) end)
Line 1,212:
print(unpack(stoogesort{9,7,8,5,6,3,4,2,1,0}))
 
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">swap := proc(arr, a, b)
local temp := arr[a];
arr[a] := arr[b];
Line 1,239:
 
arr := Array([4, 2, 6, 1, 3, 7, 9, 5, 8]):
stoogesort(arr, 1, numelems(arr));</langsyntaxhighlight>
{{out}}
<pre>
Line 1,247:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">stoogeSort[lst_, I_, J_] := Module[{i = I, j = J, list = lst},
If[list[[j]] < list[[i]], list[[{i,j}]] = list[[{j,i}]];]
If[(j-i) > 1, t = Round[(j-i+1)/3];
Line 1,254:
list=stoogeSort[list,i,j-t];];
list
]</langsyntaxhighlight>
{{out}}
<pre>stoogeSort[{3,2,9,6,8},1,5]
Line 1,260:
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">%Required inputs:
%i = 1
%j = length(list)
Line 1,277:
end
 
end</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight MATLABlang="matlab">>> stoogeSort([1 -6 4 -9],1,4)
 
ans =
 
-9 -6 1 4</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight MAXScriptlang="maxscript">fn stoogeSort arr i: j: =
(
if i == unsupplied do i = 1
Line 1,303:
)
return arr
)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight MAXScriptlang="maxscript">a = for i in 1 to 15 collect random 1 20
#(10, 2, 1, 19, 18, 20, 18, 5, 13, 2, 13, 9, 7, 10, 6)
stoogeSort a
#(1, 2, 2, 5, 6, 7, 9, 10, 10, 13, 13, 18, 18, 19, 20)
</syntaxhighlight>
</lang>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 1,347:
 
return L_
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,355:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc stoogeSort[T](a: var openarray[T], i, j: int) =
if a[j] < a[i]: swap a[i], a[j]
if j - i > 1:
Line 1,365:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
stoogeSort a, 0, a.high
echo a</langsyntaxhighlight>
{{out}}
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
bundle Default {
class Stooge {
Line 1,402:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let swap ar i j =
let tmp = ar.(i) in
ar.(i) <- ar.(j);
Line 1,422:
end
in
aux 0 (Array.length ar - 1)</langsyntaxhighlight>
 
testing:
<langsyntaxhighlight lang="ocaml">let () =
let ar = [| 3; 1; 7; 2; 6; 5; 4; 9; 8 |] in
stoogesort ar;
Array.iter (Printf.printf " %d") ar;
print_newline()</langsyntaxhighlight>
 
=={{header|ooRexx}}==
{{Trans|NetRexx}}
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
 
call demo
Line 1,513:
self~put(item, ix)
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,539:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
proc {StoogeSort Arr}
proc {Swap I J}
Line 1,578:
for I in {Array.low Arr}..{Array.high Arr} do
{System.printInfo Arr.I#", "}
end</langsyntaxhighlight>
 
{{out}}
Line 1,585:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">stoogeSort(v)={
local(v=v); \\ Give children access to v
ss(1,#v); \\ Sort
Line 1,604:
ss(i,j-t)
)
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program StoogeSortDemo;
type
Line 1,652:
end;
writeln;
end.</langsyntaxhighlight>
{{out}}
<pre>./StoogeSort
Line 1,662:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub stooge {
use integer;
my ($x, $i, $j) = @_;
Line 1,685:
stooge(\@a);
print "After @a\n";
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,705:
<span style="color: #0000FF;">?</span><span style="color: #000000;">stoogesort</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;">10</span><span style="color: #0000FF;">)))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,712:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
function stoogeSort(&$arr, $i, $j)
{
Line 1,727:
}
}
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de stoogeSort (L N)
(default N (length L))
(let P (nth L N)
Line 1,740:
(stoogeSort (nth L (inc D)) (- N D))
(stoogeSort L (- N D)) ) )
L )</langsyntaxhighlight>
Test:
<pre>: (apply < (stoogeSort (make (do 100 (link (rand))))))
Line 1,746:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">stoogesort: procedure (L) recursive; /* 16 August 2010 */
declare L(*) fixed binary;
declare (i, j, t, temp) fixed binary;
Line 1,762:
end;
end;
end stoogesort;</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
Line 1,776:
This version is closely based on the BASIC code above.
 
<langsyntaxhighlight lang="powerbasic">%arraysize = 10
 
SUB stoogesort (L() AS LONG, i AS LONG, j AS LONG)
Line 1,816:
 
? s
END FUNCTION</langsyntaxhighlight>
 
{{out}}
Line 1,827:
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">Function StoogeSort( [Int32[]] $L )
{
$i = 0
Line 1,848:
}
 
StoogeSort 9, 7, 5, 3, 1, 2, 4, 6, 8</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure Stooge_Sort(Array L.i(1), i=0 , j=0)
If j=0
j=ArraySize(L())
Line 1,864:
Stooge_Sort(L(), i, j-t)
EndIf
EndProcedure</langsyntaxhighlight>
Implementation may be as<langsyntaxhighlight PureBasiclang="purebasic">Define AmountOfPosts=(?Stop_Data-?Start_data)/SizeOf(Integer)
Dim Xyz.i(AmountOfPosts)
CopyMemory(?Start_data, @Xyz(), ?Stop_Data-?Start_data)
Line 1,879:
Data.i 1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7
Stop_Data:
EndDataSection</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> data = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7]
>>> def stoogesort(L, i=0, j=None):
if j is None:
Line 1,896:
 
>>> stoogesort(data)
[-6, -5, -3, -2, 1, 3, 3, 4, 5, 5, 7, 7, 7, 9, 10]</langsyntaxhighlight>
 
This alternate solution uses a wrapper function
to compute the initial value of ''j''
rather than detecting the sentinel value ''None''.
<langsyntaxhighlight lang="python">>>> def stoogesort(L, i, j):
if L[j] < L[i]:
L[i], L[j] = L[j], L[i]
Line 1,915:
>>> data = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7]
>>> stooge(data)
[-6, -5, -3, -2, 1, 3, 3, 4, 5, 5, 7, 7, 7, 9, 10]</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ 2 * 3 /mod 0 > + ] is twothirds ( n --> n )
 
[ dup 0 peek over -1 peek
Line 1,937:
[] 33 times [ 90 random 10 + join ]
dup echo cr
stoogesort echo</langsyntaxhighlight>
 
{{out}}
Line 1,946:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">stoogesort = function(vect) {
i = 1
j = length(vect)
Line 1,962:
k = stoogesort(v)
v
k</langsyntaxhighlight>
{{out}}
<pre>
Line 1,970:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define (stooge-sort xs [i 0] [j (- (vector-length xs) 1)])
Line 1,983:
(stooge-sort xs i (- j t)))
xs)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub stoogesort( @L, $i = 0, $j = @L.end ) {
@L[$j,$i] = @L[$i,$j] if @L[$i] > @L[$j];
 
Line 2,004:
 
stoogesort(@L).Str.say;
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
This REXX example starts an array at element zero &nbsp; (but any integer could be used); &nbsp; a zero-
<br>based index was used because the algorithm shown in the Rosetta Code task used zero.
<langsyntaxhighlight REXXlang="rexx">/*REXX program sorts an integer array @. [the first element starts at index zero].*/
parse arg N . /*obtain an optional argument from C.L.*/
if N=='' | N=="," then N=19 /*Not specified? Then use the default.*/
Line 2,031:
call stoogeSort i , j-t /* " " */
end
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default (internal generated) inputs:}}
 
Line 2,081:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
test = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
stoogeSort(test, 1, len(test))
Line 2,100:
stoogeSort(list, i, j-t) ok
return list
</langsyntaxhighlight>
Output:
<pre>
Line 2,107:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Array
def stoogesort
self.dup.stoogesort!
Line 2,126:
end
 
p [1,4,5,3,-6,3,7,10,-2,-5].stoogesort </langsyntaxhighlight>
 
{{out}}
Line 2,132:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn stoogesort<E>(a: &mut [E])
where E: PartialOrd
{
Line 2,153:
stoogesort(&mut numbers);
println!("After: {:?}", &numbers);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object StoogeSort extends App {
def stoogeSort(a: Array[Int], i: Int, j: Int) {
if (a(j) < a(i)) {
Line 2,175:
stoogeSort(a, 0, a.length - 1)
println(s"Sorted : ${a.mkString(", ")}")
}</langsyntaxhighlight>
 
See it running in your browser by [https://scastie.scala-lang.org/QTCrb5SNTVqDNC6oRQRmZw Scastie (JVM)].
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func stooge(x, i, j) {
if (x[j] < x[i]) {
x.swap(i, j)
Line 2,197:
say "Before #{a}"
stooge(a, 0, a.end)
say "After #{a}"</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">OrderedCollection extend [
stoogeSortFrom: i to: j [
(self at: j) < (self at: i)
Line 2,224:
test := #( 1 4 5 3 -6 3 7 10 -2 -5) asOrderedCollection.
test stoogeSort.
test printNl.</langsyntaxhighlight>
 
Here is a "functional" variation (a 1:1 adaption of the original algorithm):
<langsyntaxhighlight lang="smalltalk">stoogesort := [:L :i :j |
(L at:i) > (L at:j) ifTrue:[
L swap:i with:j
Line 2,241:
a := #(1 4 5 3 -6 3 7 10 -2 -5 7 5 9 -3 7) copy.
stoogesort value:a value:1 value:a size.
Transcript showCR:a</langsyntaxhighlight>
 
Output:
Line 2,247:
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">func stoogeSort(inout arr:[Int], _ i:Int = 0, var _ j:Int = -1) {
if j == -1 {
j = arr.count - 1
Line 2,268:
stoogeSort(&a)
 
println(a)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,276:
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
proc stoogesort {L {i 0} {j -42}} {
Line 2,297:
}
 
stoogesort {1 4 5 3 -6 3 7 10 -2 -5}</langsyntaxhighlight>
{{out}}
<pre>-6 -5 -2 1 3 3 4 5 7 10</pre>
Line 2,303:
=={{header|uBasic/4tH}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="text">PRINT "Stooge sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
Line 2,354:
PRINT
RETURN</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var stoogeSort // recursive
stoogeSort = Fn.new { |a, i, j|
if (a[j] < a[i]) {
Line 2,378:
System.print("After : %(a)")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 2,390:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code ChOut=8, IntOut=11; \intrinsic routines
 
proc StoogeSort(L, I, J); \Sort array L
Line 2,409:
StoogeSort(A, 0, 10-1);
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )];
]</langsyntaxhighlight>
 
{{out}}
Line 2,419:
Based on pseudocode, except using Yorick's 1-based arrays.
Sorts in place.
<langsyntaxhighlight lang="yorick">func stoogesort(&L, i, j) {
if(is_void(i)) i = 1;
if(is_void(j)) j = numberof(L);
Line 2,430:
stoogesort, L, i, j-t;
}
}</langsyntaxhighlight>
 
Example interactive use:
Line 2,439:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn stoogeSort(list,i=0,j=Void){ if(Void==j) j=list.len() - 1; // default parameters set before call
if(list[j]<list[i]) list.swap(i,j);
if(j - i >1){
Line 2,448:
}
list
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">stoogeSort(List(67,-201,0,9,9,231,4)).println();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits