Sorting algorithms/Stooge sort: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Quackery}}: bug fix) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 25:
{{trans|Python}}
<
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)</
{{out}}
Line 48:
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<
INT ARRAY stack(MAX_COUNT)
INT stackSize
Line 127:
Test(c,8)
Test(d,12)
RETURN</
{{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.
<
procedure Stooge is
type Integer_Array is array (Positive range <>) of Integer;
Line 187:
end loop;
Ada.Text_IO.New_Line;
end Stooge;</
{{out}}
Line 194:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<
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 ) )</
{{out}}
<pre>
Line 228:
=={{header|AutoHotkey}}==
<
if !j
j := L.MaxIndex()
Line 243:
}
return L
}</
Examples:<
return
Line 251:
res .= v ","
return trim(res, ",")
}</
Outputs:<pre>-12,0,3,6,51,73,123</pre>
Line 260:
It ''definitely'' does '''not''' work with QBasic.
<
RANDOMIZE TIMER
Line 293:
stoogesort L(), i, j - t
END IF
END SUB</
{{out}}
Line 304:
=={{header|BBC BASIC}}==
<
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</
{{out}}
<pre>
Line 329:
=={{header|BCPL}}==
<
let stoogesort(L, i, j) be
Line 357:
stoogesort(array, 0, length-1)
write("After: ", array, length)
$)</
{{out}}
<pre>Before: 4 65 2 -31 0 99 2 83 782 1
Line 363:
=={{header|C}}==
<
#define SWAP(r,s) do{ t=r; r=s; s=t; } while(0)
Line 393:
return 0;
}</
=={{header|C sharp|C#}}==
<
if (list.Count > 1) {
StoogeSort(list, 0, list.Count - 1);
Line 413:
StoogeSort(L, i, j - t);
}
}</
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <iostream>
#include <time.h>
Line 447:
return system( "pause" );
}
</syntaxhighlight>
{{out}}
<pre>
Line 464:
=={{header|Clojure}}==
<
(assoc! v y (v x) x (v y)))
Line 476:
(stooge-sort v (+ i t) j)
(stooge-sort v i (- j t))))
v))</
=={{header|COBOL}}==
{{works with|GNU Cobol}}
<
IDENTIFICATION DIVISION.
PROGRAM-ID. stooge-sort-test.
Line 559:
END-IF
.
END PROGRAM stooge-sort.</
{{out}}
Line 568:
=={{header|Common Lisp}}==
<
(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)</
=={{header|D}}==
<
void stoogeSort(T)(T[] seq) pure nothrow {
Line 597:
data.stoogeSort();
writeln(data);
}</
{{out}}
[-6, -5, -2, 1, 3, 3, 4, 5, 7, 10]
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
STOOGE_SORT
Line 632:
end
end
</syntaxhighlight>
Test:
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 667:
end
</syntaxhighlight>
{{out}}
<pre>
Line 678:
=={{header|Elena}}==
ELENA 4.x :
<
import system'routines;
Line 708:
console.printLine("before:", list.asEnumerable());
console.printLine("after:", list.stoogeSort().asEnumerable())
}</
{{out}}
<pre>
Line 716:
=={{header|Elixir}}==
<
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</
{{out}}
Line 747:
=={{header|Euphoria}}==
<
object temp
integer t
Line 771:
? s
? stoogesort(s)</
{{out}}
Line 779:
=={{header|Factor}}==
<
IN: rosetta-code.stooge-sort
Line 803:
{ 1 4 5 3 -6 3 7 10 -2 -5 } stooge-sort . ;
MAIN: stooge-sort-demo</
{{out}}
<pre>
Line 811:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 841:
end subroutine
end program</
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 884:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Unsorted
Line 893:
=={{header|Go}}==
<
import "fmt"
Line 917:
stoogesort(a[:len(a)-t])
}
}</
=={{header|Haskell}}==
<
import Control.Arrow
import Control.Monad
Line 940:
xs'
| xs!!j < xs!!i = swapElems xs i j
| otherwise = xs</
Example:
<
[-6,-5,-3,-2,1,3,3,4,5,5,7,7,7,9,10]</
=={{header|Icon}} and {{header|Unicon}}==
<
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</
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]].
Line 982:
=={{header|IS-BASIC}}==
<
110 RANDOMIZE
120 NUMERIC ARRAY(5 TO 16)
Line 1,009:
350 CALL STOOGESORT(A,I,J-T)
360 END IF
370 END DEF</
=={{header|J}}==
<
stoogeSort=: 3 : 0
Line 1,022:
(x-0,t) stoogeSort (x+t,0) stoogeSort (x-0,t) stoogeSort y
else. y end.
)</
Example:
<
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>
=={{header|Java}}==
<
public class Stooge {
Line 1,056:
}
}
}</
{{out}}
<pre>[-6, -5, -2, 1, 3, 3, 4, 5, 7, 10]</pre>
=={{header|JavaScript}}==
<
if (j === undefined) {
j = array.length - 1;
Line 1,082:
stoogeSort(array, i, j-t);
}
};</
Example:
<
stoogeSort(arr);
console.log(arr);</
{{out}}
<pre>[1, 2, 3, 4, 9, 10, 13]</pre>
Line 1,092:
=={{header|jq}}==
{{works with|jq|1.4}}
<
def swap(i;j): .[i] as $t | .[i] = .[j] | .[j] = $t;
Line 1,108:
end;
[., 0, length-1] | ss;</
'''Example'''
<
[1],
[1,2],
[1,3,2,4],
[1,4,5,3,-6,3,7,10,-2,-5]
) | stoogesort</
{{out}}
<
[]
[1]
[1,2]
[1,2,3,4]
[-6,-5,-2,1,3,3,4,5,7,10]</
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Matlab}}
<
if a[j] < a[i]
a[[i, j]] = a[[j, i]];
Line 1,143:
x = randn(10)
@show x stoogesort!(x)</
{{out}}
Line 1,150:
=={{header|Kotlin}}==
<
fun stoogeSort(a: IntArray, i: Int, j: Int) {
Line 1,171:
stoogeSort(a, 0, a.size - 1)
println("Sorted : ${a.asList()}")
}</
{{out}}
Line 1,184:
and made generic with an optional predicate parameter.
<
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>
=={{header|Maple}}==
<
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));</
{{out}}
<pre>
Line 1,247:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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
]</
{{out}}
<pre>stoogeSort[{3,2,9,6,8},1,5]
Line 1,260:
=={{header|MATLAB}} / {{header|Octave}}==
<
%i = 1
%j = length(list)
Line 1,277:
end
end</
{{out}}
<
ans =
-9 -6 1 4</
=={{header|MAXScript}}==
<
(
if i == unsupplied do i = 1
Line 1,303:
)
return arr
)</
{{out}}
<
#(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>
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols binary
Line 1,347:
return L_
</syntaxhighlight>
{{out}}
<pre>
Line 1,355:
=={{header|Nim}}==
<
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</
{{out}}
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
=={{header|Objeck}}==
<
bundle Default {
class Stooge {
Line 1,402:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
<
let tmp = ar.(i) in
ar.(i) <- ar.(j);
Line 1,422:
end
in
aux 0 (Array.length ar - 1)</
testing:
<
let ar = [| 3; 1; 7; 2; 6; 5; 4; 9; 8 |] in
stoogesort ar;
Array.iter (Printf.printf " %d") ar;
print_newline()</
=={{header|ooRexx}}==
{{Trans|NetRexx}}
<
call demo
Line 1,513:
self~put(item, ix)
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,539:
=={{header|Oz}}==
<
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</
{{out}}
Line 1,585:
=={{header|PARI/GP}}==
<
local(v=v); \\ Give children access to v
ss(1,#v); \\ Sort
Line 1,604:
ss(i,j-t)
)
};</
=={{header|Pascal}}==
<
type
Line 1,652:
end;
writeln;
end.</
{{out}}
<pre>./StoogeSort
Line 1,662:
=={{header|Perl}}==
<
use integer;
my ($x, $i, $j) = @_;
Line 1,685:
stooge(\@a);
print "After @a\n";
</syntaxhighlight>
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,712:
=={{header|PHP}}==
<
function stoogeSort(&$arr, $i, $j)
{
Line 1,727:
}
}
</syntaxhighlight>
=={{header|PicoLisp}}==
<
(default N (length L))
(let P (nth L N)
Line 1,740:
(stoogeSort (nth L (inc D)) (- N D))
(stoogeSort L (- N D)) ) )
L )</
Test:
<pre>: (apply < (stoogeSort (make (do 100 (link (rand))))))
Line 1,746:
=={{header|PL/I}}==
<
declare L(*) fixed binary;
declare (i, j, t, temp) fixed binary;
Line 1,762:
end;
end;
end stoogesort;</
=={{header|PowerBASIC}}==
Line 1,776:
This version is closely based on the BASIC code above.
<
SUB stoogesort (L() AS LONG, i AS LONG, j AS LONG)
Line 1,816:
? s
END FUNCTION</
{{out}}
Line 1,827:
=={{header|PowerShell}}==
<
{
$i = 0
Line 1,848:
}
StoogeSort 9, 7, 5, 3, 1, 2, 4, 6, 8</
=={{header|PureBasic}}==
<
If j=0
j=ArraySize(L())
Line 1,864:
Stooge_Sort(L(), i, j-t)
EndIf
EndProcedure</
Implementation may be as<
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</
=={{header|Python}}==
<
>>> 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]</
This alternate solution uses a wrapper function
to compute the initial value of ''j''
rather than detecting the sentinel value ''None''.
<
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]</
=={{header|Quackery}}==
<
[ dup 0 peek over -1 peek
Line 1,937:
[] 33 times [ 90 random 10 + join ]
dup echo cr
stoogesort echo</
{{out}}
Line 1,946:
=={{header|R}}==
<
i = 1
j = length(vect)
Line 1,962:
k = stoogesort(v)
v
k</
{{out}}
<pre>
Line 1,970:
=={{header|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>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
@L[$j,$i] = @L[$i,$j] if @L[$i] > @L[$j];
Line 2,004:
stoogesort(@L).Str.say;
</syntaxhighlight>
=={{header|REXX}}==
This REXX example starts an array at element zero (but any integer could be used); a zero-
<br>based index was used because the algorithm shown in the Rosetta Code task used 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</
{{out|output|text= when using the default (internal generated) inputs:}}
Line 2,081:
=={{header|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
</
Output:
<pre>
Line 2,107:
=={{header|Ruby}}==
<
def stoogesort
self.dup.stoogesort!
Line 2,126:
end
p [1,4,5,3,-6,3,7,10,-2,-5].stoogesort </
{{out}}
Line 2,132:
=={{header|Rust}}==
<
where E: PartialOrd
{
Line 2,153:
stoogesort(&mut numbers);
println!("After: {:?}", &numbers);
}</
=={{header|Scala}}==
<
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(", ")}")
}</
See it running in your browser by [https://scastie.scala-lang.org/QTCrb5SNTVqDNC6oRQRmZw Scastie (JVM)].
=={{header|Sidef}}==
<
if (x[j] < x[i]) {
x.swap(i, j)
Line 2,197:
say "Before #{a}"
stooge(a, 0, a.end)
say "After #{a}"</
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<
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.</
Here is a "functional" variation (a 1:1 adaption of the original algorithm):
<
(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</
Output:
Line 2,247:
=={{header|Swift}}==
<
if j == -1 {
j = arr.count - 1
Line 2,268:
stoogeSort(&a)
println(a)</
{{out}}
<pre>
Line 2,276:
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<
proc stoogesort {L {i 0} {j -42}} {
Line 2,297:
}
stoogesort {1 4 5 3 -6 3 7 10 -2 -5}</
{{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</
=={{header|Wren}}==
<
stoogeSort = Fn.new { |a, i, j|
if (a[j] < a[i]) {
Line 2,378:
System.print("After : %(a)")
System.print()
}</
{{out}}
Line 2,390:
=={{header|XPL0}}==
<
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, ^ )];
]</
{{out}}
Line 2,419:
Based on pseudocode, except using Yorick's 1-based arrays.
Sorts in place.
<
if(is_void(i)) i = 1;
if(is_void(j)) j = numberof(L);
Line 2,430:
stoogesort, L, i, j-t;
}
}</
Example interactive use:
Line 2,439:
=={{header|zkl}}==
<
if(list[j]<list[i]) list.swap(i,j);
if(j - i >1){
Line 2,448:
}
list
}</
<
{{out}}
<pre>
|