2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,6 +1,11 @@
{{sorting Algorithm}}{{wikipedia|Stooge sort}}
{{sorting Algorithm}}
{{wikipedia|Stooge sort}}
{{omit from|GUISS}}
Show the [[wp:Stooge sort|Stooge Sort]] for an array of integers.
;Task:
Show the   [[wp:Stooge sort|Stooge Sort]]   for an array of integers.
The Stooge Sort algorithm is as follows:
<b>algorithm</b> stoogesort(<b>array</b> L, i = 0, j = <b>length</b>(L)-1)
@ -12,3 +17,4 @@ The Stooge Sort algorithm is as follows:
stoogesort(L, i+t, j )
stoogesort(L, i , j-t)
<b>return</b> L
<br><br>

View file

@ -0,0 +1,27 @@
# swaps the values of the two REF INTs #
PRIO =:= = 1;
OP =:= = ( REF INT a, b )VOID: ( INT t := a; a := b; b := t );
# returns the array of INTs sorted via the stooge sort algorithm #
PROC stooge sort = ( []INT array )[]INT:
BEGIN
PROC stooge sort segment = ( REF[]INT l, INT i, j )VOID:
BEGIN
IF l[j] < l[i] THEN l[ i ] =:= l[ j ] FI;
IF j - i > 1
THEN
INT t := (j - i + 1) OVER 3;
stooge sort segment( l, i, j - t );
stooge sort segment( l, i + t, j );
stooge sort segment( l, i, j - t )
FI
END # stooge sort segment # ;
[ LWB array : UPB array ]INT result := array;
stooge sort segment( result, LWB result, UPB result );
result
END # stooge sort # ;
# test the stooge sort #
[]INT data = ( 67, -201, 0, 9, 9, 231, 4 );
print( ( "before: ", data, newline, "after: ", stooge sort( data ), newline ) )

View file

@ -0,0 +1,23 @@
defmodule Sort do
def stooge_sort(list) do
stooge_sort(List.to_tuple(list), 0, length(list)-1) |> Tuple.to_list
end
defp stooge_sort(tuple, i, j) do
if (vj = elem(tuple, j)) < (vi = elem(tuple, i)) do
tuple = put_elem(tuple,i,vj) |> put_elem(j,vi)
end
if j - i > 1 do
t = div(j - i + 1, 3)
tuple
|> stooge_sort(i, j-t)
|> stooge_sort(i+t, j)
|> stooge_sort(i, j-t)
else
tuple
end
end
end
(for _ <- 1..20, do: :rand.uniform(20)) |> IO.inspect
|> Sort.stooge_sort |> IO.inspect

View file

@ -0,0 +1,22 @@
function stoogeSort (array, i, j) {
if (j === undefined) {
j = array.length - 1;
}
if (i === undefined) {
i = 0;
}
if (array[j] < array[i]) {
var aux = array[i];
array[i] = array[j];
array[j] = aux;
}
if (j - i > 1) {
var t = Math.floor((j - i + 1) / 3);
stoogeSort(array, i, j-t);
stoogeSort(array, i+t, j);
stoogeSort(array, i, j-t);
}
};

View file

@ -0,0 +1,3 @@
arr = [9,1,3,10,13,4,2];
stoogeSort(arr);
console.log(arr);

View file

@ -1,4 +1,4 @@
sub stoogesort( @L is rw, $i = 0, $j = @L.end ) {
sub stoogesort( @L, $i = 0, $j = @L.end ) {
@L[$j,$i] = @L[$i,$j] if @L[$i] > @L[$j];
my $interval = $j - $i;

View file

@ -0,0 +1,17 @@
stoogesort = function(vect) {
i = 1
j = length(vect)
if(vect[j] < vect[i]) vect[c(j, i)] = vect[c(i, j)]
if(j - i > 1) {
t = (j - i + 1) %/% 3
vect[i:(j - t)] = stoogesort(vect[i:(j - t)])
vect[(i + t):j] = stoogesort(vect[(i + t):j])
vect[i:(j - t)] = stoogesort(vect[i:(j - t)])
}
vect
}
v = sample(21, 20)
k = stoogesort(v)
v
k

View file

@ -1,35 +1,25 @@
/*REXX program to sort an integer array L [elements start at zero]. */
highItem=19 /*define 0 ──► 19 elements. */
widthH=length(highItem) /*width of biggest element number*/
widthL=0 /*width of largest element value.*/
do k=0 to highItem /*populate the array with stuff. */
L.k=2*k + (k * -1**k) /*kinda generate randomish nums. */
if L.k==0 then L.k=-100-k /*if zero, make a negative number*/
widthL=max(widthL,length(L.k)) /*compute maximum width so far. */
end /*k*/
call showL 'before sort' /*show the before array elements.*/
call stoogeSort 0,highItem /*invoke the Stooge Sort. */
call showL ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SHOWL subroutine────────────────────*/
showL: sepLength=22+widthH+widthL /*compute separator width. */
say copies('-',sepLength) /*show the 1st separator line. */
do j=0 to highItem
say 'element' right(j,widthH) arg(1)":" right(L.j,widthL)
end /*j*/
say copies('=', sepLength) /*show the 2nd separator line. */
return
/*──────────────────────────────────STOOGESORT subroutine───────────────*/
stoogeSort: procedure expose L.; parse arg i,j /*sort from I ──> J.*/
if L.j<L.i then parse value L.i L.j with L.j L.i /*swap L.i with L.j*/
if j-i>1 then do
t=(j-i+1) % 3 /* % is REXX integer division. */
call stoogesort i , j-t
call stoogesort i+t, j
call stoogesort i , j-t
end
return
/*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.*/
wV=0 /*width of the largest value, so far.*/
do k=0 to N; @.k=k*2 + k*-1**k /*generate some kinda scattered numbers*/
if @.k//7==0 then @.k= -100 -k /*Multiple of 7? Then make a negative#*/
wV=max(wV, length(@.k)) /*find maximum width of values, so far.*/
end /*k*/ /* [↑] // is REXX division remainder.*/
wN=length(N) /*width of the largest element number.*/
call show 'before sort' /*show the before array elements. */
say copies('', wN+wV+ 50) /*show a separator line (between shows)*/
call stoogeSort 0, N /*invoke the Stooge Sort. */
call show ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=0 to N; say right('element',22) right(j,wN) arg(1)":" right(@.j,wV); end;return
/*──────────────────────────────────────────────────────────────────────────────────────*/
stoogeSort: procedure expose @.; parse arg i,j /*sort from I ───► J. */
if @.j<@.i then parse value @.i @.j with @.j @.i /*swap @.i with @.j */
if j-i>1 then do; t=(j-i+1) % 3 /*%: integer division.*/
call stoogeSort i , j-t /*invoke recursively. */
call stoogeSort i+t, j /* " " */
call stoogeSort i , j-t /* " " */
end
return

View file

@ -0,0 +1,22 @@
fn stoogesort<E>(a: &mut [E])
where E: PartialOrd
{
let len = a.len();
if a.first().unwrap() > a.last().unwrap() {
a.swap(0, len - 1);
}
if len - 1 > 1 {
let t = len / 3;
stoogesort(&mut a[..len - 1]);
stoogesort(&mut a[t..]);
stoogesort(&mut a[..len - 1]);
}
}
fn main() {
let mut numbers = vec![1_i32, 9, 4, 7, 6, 5, 3, 2, 8];
println!("Before: {:?}", &numbers);
stoogesort(&mut numbers);
println!("After: {:?}", &numbers);
}