Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,32 @@
|
|||
with Ada.Text_IO;
|
||||
procedure Stooge is
|
||||
type Integer_Array is array (Positive range <>) of Integer;
|
||||
procedure Swap (Left, Right : in out Integer) is
|
||||
Temp : Integer := Left;
|
||||
begin
|
||||
Left := Right;
|
||||
Right := Temp;
|
||||
end Swap;
|
||||
procedure Stooge_Sort (List : in out Integer_Array) is
|
||||
T : Natural := List'Length / 3;
|
||||
begin
|
||||
if List (List'Last) < List (List'First) then
|
||||
Swap (List (List'Last), List (List'First));
|
||||
end if;
|
||||
if List'Length > 2 then
|
||||
Stooge_Sort (List (List'First .. List'Last - T));
|
||||
Stooge_Sort (List (List'First + T .. List'Last));
|
||||
Stooge_Sort (List (List'First .. List'Last - T));
|
||||
end if;
|
||||
end Stooge_Sort;
|
||||
Test_Array : Integer_Array := (1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7);
|
||||
begin
|
||||
Stooge_Sort (Test_Array);
|
||||
for I in Test_Array'Range loop
|
||||
Ada.Text_IO.Put (Integer'Image (Test_Array (I)));
|
||||
if I /= Test_Array'Last then
|
||||
Ada.Text_IO.Put (", ");
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end Stooge;
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
on stoogesort(lst, i, j)
|
||||
if item j of lst < item i of lst then
|
||||
set a to item j of lst
|
||||
set b to item i of lst
|
||||
set item i of lst to a
|
||||
set item j of lst to b
|
||||
end if
|
||||
if j - i > 1 then
|
||||
set t to (j - i + 1) / 3 as integer
|
||||
stoogesort(lst, i, j - t)
|
||||
stoogesort(lst, i + t, j)
|
||||
stoogesort(lst, i, j - t)
|
||||
end if
|
||||
return lst
|
||||
end stoogesort
|
||||
|
||||
on stooge(lst)
|
||||
return stoogesort(lst, 1, length of lst)
|
||||
end stooge
|
||||
|
||||
set test_list to {}
|
||||
repeat 20 times
|
||||
set test_list to test_list & (random number from 1 to 20)
|
||||
end repeat
|
||||
|
||||
stooge(test_list)
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
StoogeSort(L, i:=1, j:=""){
|
||||
if !j
|
||||
j := L.MaxIndex()
|
||||
if (L[j] < L[i]){
|
||||
temp := L[i]
|
||||
L[i] := L[j]
|
||||
L[j] := temp
|
||||
}
|
||||
if (j - i > 1){
|
||||
t := floor((j - i + 1)/3)
|
||||
StoogeSort(L, i, j-t)
|
||||
StoogeSort(L, i+t, j)
|
||||
StoogeSort(L, i, j-t)
|
||||
}
|
||||
return L
|
||||
if !j
|
||||
j := L.MaxIndex()
|
||||
if (L[j] < L[i]){
|
||||
temp := L[i]
|
||||
L[i] := L[j]
|
||||
L[j] := temp
|
||||
}
|
||||
if (j - i > 1){
|
||||
t := floor((j - i + 1)/3)
|
||||
StoogeSort(L, i, j-t)
|
||||
StoogeSort(L, i+t, j)
|
||||
StoogeSort(L, i, j-t)
|
||||
}
|
||||
return L
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ MsgBox % map(StoogeSort([123,51,6,73,3,-12,0]))
|
|||
return
|
||||
|
||||
map(obj){
|
||||
for k, v in obj
|
||||
res .= v ","
|
||||
return trim(res, ",")
|
||||
for k, v in obj
|
||||
res .= v ","
|
||||
return trim(res, ",")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ public:
|
|||
void sort( int* arr, int start, int end )
|
||||
{
|
||||
if( arr[start] > arr[end - 1] ) swap( arr[start], arr[end - 1] );
|
||||
int n = end - start; if( n > 2 )
|
||||
{
|
||||
n /= 3; sort( arr, start, end - n );
|
||||
sort( arr, start + n, end ); sort( arr, start, end - n );
|
||||
int n = end - start; if( n > 2 )
|
||||
{
|
||||
n /= 3; sort( arr, start, end - n );
|
||||
sort( arr, start + n, end ); sort( arr, start, end - n );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
>>SOURCE FREE
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. stooge-sort-test.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 Arr-Len CONSTANT 7.
|
||||
|
||||
01 arr-area VALUE "00004001000020000005000230000000000".
|
||||
03 arr-elt PIC 9(5) OCCURS Arr-Len TIMES
|
||||
INDEXED BY arr-idx.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY "Unsorted: " NO ADVANCING
|
||||
PERFORM VARYING arr-idx FROM 1 BY 1 UNTIL Arr-Len < arr-idx
|
||||
DISPLAY arr-elt (arr-idx) " " NO ADVANCING
|
||||
END-PERFORM
|
||||
DISPLAY SPACE
|
||||
|
||||
CALL "stooge-sort" USING arr-area, OMITTED, OMITTED
|
||||
|
||||
DISPLAY "Sorted: " NO ADVANCING
|
||||
PERFORM VARYING arr-idx FROM 1 BY 1 UNTIL Arr-Len < arr-idx
|
||||
DISPLAY arr-elt (arr-idx) " " NO ADVANCING
|
||||
END-PERFORM
|
||||
DISPLAY SPACE
|
||||
.
|
||||
END PROGRAM stooge-sort-test.
|
||||
|
||||
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. stooge-sort RECURSIVE.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 Arr-Len CONSTANT 7.
|
||||
|
||||
01 i PIC 99 COMP.
|
||||
01 j PIC 99 COMP.
|
||||
|
||||
01 temp PIC 9(5).
|
||||
|
||||
01 t PIC 99 COMP.
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 arr-area.
|
||||
03 arr-elt PIC 9(5) OCCURS Arr-Len TIMES.
|
||||
|
||||
01 i-val PIC 99 COMP.
|
||||
01 j-val PIC 99 COMP.
|
||||
|
||||
PROCEDURE DIVISION USING arr-area, OPTIONAL i-val, OPTIONAL j-val.
|
||||
IF i-val IS OMITTED
|
||||
MOVE 1 TO i
|
||||
ELSE
|
||||
MOVE i-val TO i
|
||||
END-IF
|
||||
IF j-val IS OMITTED
|
||||
MOVE Arr-Len TO j
|
||||
ELSE
|
||||
MOVE j-val TO j
|
||||
END-IF
|
||||
|
||||
IF arr-elt (j) < arr-elt (i)
|
||||
MOVE arr-elt (i) TO temp
|
||||
MOVE arr-elt (j) TO arr-elt (i)
|
||||
MOVE temp TO arr-elt (j)
|
||||
END-IF
|
||||
|
||||
IF j - i + 1 >= 3
|
||||
COMPUTE t = (j - i + 1) / 3
|
||||
SUBTRACT t FROM j
|
||||
CALL "stooge-sort" USING arr-area, CONTENT i, j
|
||||
ADD t TO i, j
|
||||
CALL "stooge-sort" USING arr-area, CONTENT i, j
|
||||
SUBTRACT t FROM i, j
|
||||
CALL "stooge-sort" USING arr-area, CONTENT i, j
|
||||
END-IF
|
||||
.
|
||||
END PROGRAM stooge-sort.
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
module Indexable::Mutable
|
||||
def stooge_sort!
|
||||
stooge_sort_impl 0, size-1
|
||||
self
|
||||
end
|
||||
|
||||
private def stooge_sort_impl (i, j)
|
||||
swap(i, j) if self[i] > self[j]
|
||||
if j - i + 1 > 2
|
||||
t = (j - i + 1) // 3
|
||||
stooge_sort_impl i, j-t
|
||||
stooge_sort_impl i+t, j
|
||||
stooge_sort_impl i, j-t
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
arr = Array(Int32).new(15) { (-50..100).sample }
|
||||
puts "before: #{arr}"
|
||||
puts "after: #{arr.stooge_sort!}"
|
||||
|
|
@ -2,18 +2,18 @@ procedure StoogeSort(var L: array of integer; I,J: integer);
|
|||
var T,M: integer;
|
||||
begin
|
||||
if L[j] < L[i] then
|
||||
begin
|
||||
M:=L[I];
|
||||
L[I]:=L[J];
|
||||
L[J]:=M;
|
||||
end;
|
||||
begin
|
||||
M:=L[I];
|
||||
L[I]:=L[J];
|
||||
L[J]:=M;
|
||||
end;
|
||||
if (J - I) > 1 then
|
||||
begin
|
||||
T:=(J - I + 1) div 3;
|
||||
StoogeSort(L, I, J-T);
|
||||
StoogeSort(L, I+T, J);
|
||||
StoogeSort(L, I, J-T);
|
||||
end;
|
||||
begin
|
||||
T:=(J - I + 1) div 3;
|
||||
StoogeSort(L, I, J-T);
|
||||
StoogeSort(L, I+T, J);
|
||||
StoogeSort(L, I, J-T);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
|
|
@ -31,10 +31,10 @@ var I: integer;
|
|||
begin
|
||||
Result:='[';
|
||||
for I:=0 to High(IA) do
|
||||
begin
|
||||
if I>0 then Result:=Result+' ';
|
||||
Result:=Result+Format('%3d',[IA[I]]);
|
||||
end;
|
||||
begin
|
||||
if I>0 then Result:=Result+' ';
|
||||
Result:=Result+Format('%3d',[IA[I]]);
|
||||
end;
|
||||
Result:=Result+']';
|
||||
end;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@ proc stsort left right &d[] .
|
|||
stsort left right - t d[]
|
||||
.
|
||||
.
|
||||
for i = 1 to 100 : d[] &= random 1000
|
||||
for i = 1 to 50 : d[] &= random 0 999
|
||||
stsort 1 len d[] d[]
|
||||
print d[]
|
||||
|
|
|
|||
|
|
@ -1,29 +1,29 @@
|
|||
class
|
||||
STOOGE_SORT
|
||||
STOOGE_SORT
|
||||
feature
|
||||
stoogesort (ar: ARRAY[INTEGER]; i,j: INTEGER)
|
||||
stoogesort (ar: ARRAY[INTEGER]; i,j: INTEGER)
|
||||
-- Sorted array in ascending order.
|
||||
require
|
||||
ar_not_empty: ar.count >= 0
|
||||
i_in_range: i>=1
|
||||
j_in_range: j <= ar.count
|
||||
boundary_set: i<=j
|
||||
local
|
||||
t: REAL_64
|
||||
third: INTEGER
|
||||
swap: INTEGER
|
||||
do
|
||||
if ar[j]< ar[i] then
|
||||
swap:= ar[i]
|
||||
ar[i]:=ar[j]
|
||||
ar[j]:= swap
|
||||
end
|
||||
if j-i >1 then
|
||||
t:= (j-i+1)/3
|
||||
third:= t.floor
|
||||
stoogesort(ar, i, j-third)
|
||||
stoogesort(ar, i+third, j)
|
||||
stoogesort(ar, i, j-third)
|
||||
end
|
||||
end
|
||||
require
|
||||
ar_not_empty: ar.count >= 0
|
||||
i_in_range: i>=1
|
||||
j_in_range: j <= ar.count
|
||||
boundary_set: i<=j
|
||||
local
|
||||
t: REAL_64
|
||||
third: INTEGER
|
||||
swap: INTEGER
|
||||
do
|
||||
if ar[j]< ar[i] then
|
||||
swap:= ar[i]
|
||||
ar[i]:=ar[j]
|
||||
ar[j]:= swap
|
||||
end
|
||||
if j-i >1 then
|
||||
t:= (j-i+1)/3
|
||||
third:= t.floor
|
||||
stoogesort(ar, i, j-third)
|
||||
stoogesort(ar, i+third, j)
|
||||
stoogesort(ar, i, j-third)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,32 +1,32 @@
|
|||
class
|
||||
APPLICATION
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
do
|
||||
test := <<2, 5, 66, -2, 0, 7>>
|
||||
io.put_string ("%Nunsorted:%N")
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + "%T")
|
||||
end
|
||||
create stoogesort
|
||||
stoogesort.stoogesort (test, 1, test.count)
|
||||
io.put_string ("%Nsorted:%N")
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + "%T")
|
||||
end
|
||||
end
|
||||
make
|
||||
do
|
||||
test := <<2, 5, 66, -2, 0, 7>>
|
||||
io.put_string ("%Nunsorted:%N")
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + "%T")
|
||||
end
|
||||
create stoogesort
|
||||
stoogesort.stoogesort (test, 1, test.count)
|
||||
io.put_string ("%Nsorted:%N")
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + "%T")
|
||||
end
|
||||
end
|
||||
|
||||
test: ARRAY [INTEGER]
|
||||
test: ARRAY [INTEGER]
|
||||
|
||||
stoogesort: STOOGE_SORT
|
||||
stoogesort: STOOGE_SORT
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
function stooge(sequence s, integer i, integer j)
|
||||
object temp
|
||||
integer t
|
||||
if compare(s[j], s[i]) < 0 then
|
||||
temp = s[i]
|
||||
s[i] = s[j]
|
||||
s[j] = temp
|
||||
end if
|
||||
if j - i > 1 then
|
||||
t = floor((j - i + 1)/3)
|
||||
s = stooge(s, i , j-t)
|
||||
s = stooge(s, i+t, j )
|
||||
s = stooge(s, i , j-t)
|
||||
end if
|
||||
return s
|
||||
end function
|
||||
|
||||
function stoogesort(sequence s)
|
||||
return stooge(s,1,length(s))
|
||||
end function
|
||||
|
||||
constant s = rand(repeat(1000,10))
|
||||
|
||||
? s
|
||||
? stoogesort(s)
|
||||
|
|
@ -6,7 +6,7 @@ insertAt e k = uncurry(++).second ((e:).drop 1). splitAt k
|
|||
|
||||
swapElems :: [a] -> Int -> Int -> [a]
|
||||
swapElems xs i j = insertAt (xs!!j) i $ insertAt (xs!!i) j xs
|
||||
|
||||
|
||||
stoogeSort [] = []
|
||||
stoogeSort [x] = [x]
|
||||
stoogeSort xs = doss 0 (length xs - 1) xs
|
||||
|
|
@ -15,6 +15,6 @@ doss i j xs
|
|||
| j-i>1 = doss i (j-t) $ doss (i+t) j $ doss i (j-t) xs'
|
||||
| otherwise = xs'
|
||||
where t = (j-i+1)`div`3
|
||||
xs'
|
||||
| xs!!j < xs!!i = swapElems xs i j
|
||||
| otherwise = xs
|
||||
xs'
|
||||
| xs!!j < xs!!i = swapElems xs i j
|
||||
| otherwise = xs
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ def stoogesort:
|
|||
| if $j - $i > 1 then
|
||||
(($j - $i + 1) / 3 | floor) as $t
|
||||
| [., $i, $j-$t] | ss
|
||||
| [., $i+$t, $j] | ss
|
||||
| [., $i, $j-$t] | ss
|
||||
| [., $i+$t, $j] | ss
|
||||
| [., $i, $j-$t] | ss
|
||||
else .
|
||||
end;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
fn stoogeSort arr i: j: =
|
||||
(
|
||||
if i == unsupplied do i = 1
|
||||
if j == unsupplied do j = arr.count
|
||||
|
||||
if arr[j] < arr[i] do
|
||||
(
|
||||
swap arr[j] arr[i]
|
||||
)
|
||||
if j - i > 1 do
|
||||
(
|
||||
local t = (j - i+1)/3
|
||||
arr = stoogeSort arr i:i j:(j-t)
|
||||
arr = stoogeSort arr i:(i+t) j:j
|
||||
arr = stoogeSort arr i:i j:(j-t)
|
||||
)
|
||||
return arr
|
||||
if i == unsupplied do i = 1
|
||||
if j == unsupplied do j = arr.count
|
||||
|
||||
if arr[j] < arr[i] do
|
||||
(
|
||||
swap arr[j] arr[i]
|
||||
)
|
||||
if j - i > 1 do
|
||||
(
|
||||
local t = (j - i+1)/3
|
||||
arr = stoogeSort arr i:i j:(j-t)
|
||||
arr = stoogeSort arr i:(i+t) j:j
|
||||
arr = stoogeSort arr i:i j:(j-t)
|
||||
)
|
||||
return arr
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
swap := proc(arr, a, b)
|
||||
local temp := arr[a];
|
||||
arr[a] := arr[b];
|
||||
arr[b] := temp;
|
||||
local temp := arr[a];
|
||||
arr[a] := arr[b];
|
||||
arr[b] := temp;
|
||||
end proc:
|
||||
|
||||
stoogesort:= proc(arr, start_index, end_index)
|
||||
local cur;
|
||||
if (arr[end_index] < arr[start_index]) then
|
||||
swap(arr, start_index, end_index);
|
||||
end if;
|
||||
|
||||
if end_index - start_index > 1 then
|
||||
cur := trunc((end_index - start_index + 1)/3);
|
||||
stoogesort(arr, start_index, end_index - cur);
|
||||
stoogesort(arr, start_index + cur, end_index);
|
||||
stoogesort(arr, start_index, end_index - cur);
|
||||
end if;
|
||||
|
||||
return arr;
|
||||
local cur;
|
||||
if (arr[end_index] < arr[start_index]) then
|
||||
swap(arr, start_index, end_index);
|
||||
end if;
|
||||
|
||||
if end_index - start_index > 1 then
|
||||
cur := trunc((end_index - start_index + 1)/3);
|
||||
stoogesort(arr, start_index, end_index - cur);
|
||||
stoogesort(arr, start_index + cur, end_index);
|
||||
stoogesort(arr, start_index, end_index - cur);
|
||||
end if;
|
||||
|
||||
return arr;
|
||||
end proc:
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
function stoogeSort(&$arr, $i, $j)
|
||||
{
|
||||
if($arr[$j] < $arr[$i])
|
||||
{
|
||||
list($arr[$j],$arr[$i]) = array($arr[$i], $arr[$j]);
|
||||
}
|
||||
if(($j - $i) > 1)
|
||||
{
|
||||
$t = ($j - $i + 1) / 3;
|
||||
stoogesort($arr, $i, $j - $t);
|
||||
stoogesort($arr, $i + $t, $j);
|
||||
stoogesort($arr, $i, $j - $t);
|
||||
}
|
||||
if($arr[$j] < $arr[$i])
|
||||
{
|
||||
list($arr[$j],$arr[$i]) = array($arr[$i], $arr[$j]);
|
||||
}
|
||||
if(($j - $i) > 1)
|
||||
{
|
||||
$t = ($j - $i + 1) / 3;
|
||||
stoogesort($arr, $i, $j - $t);
|
||||
stoogesort($arr, $i + $t, $j);
|
||||
stoogesort($arr, $i, $j - $t);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
local function stooge_sort(a, i, j)
|
||||
if a[j] < a[i] then
|
||||
a[i], a[j] = a[j], a[i]
|
||||
end
|
||||
if j - i > 1 then
|
||||
local t = (j - i + 1) // 3
|
||||
stooge_sort(a, i, j - t)
|
||||
stooge_sort(a, i + t, j)
|
||||
stooge_sort(a, i, j - t)
|
||||
end
|
||||
end
|
||||
|
||||
local array = { {4, 65, 2, -31, 0, 99, 2, 83, 782, 1}, {7, 5, 2, 6, 1, 4, 2, 6, 3} }
|
||||
for array as a do
|
||||
print($"Before: \{{a:concat(", ")}}")
|
||||
stooge_sort(a, 1, #a)
|
||||
print($"After : \{{a:concat(", ")}}")
|
||||
print()
|
||||
end
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
Function StoogeSort( [Int32[]] $L )
|
||||
{
|
||||
$i = 0
|
||||
$j = $L.length-1
|
||||
if( $L[$j] -lt $L[$i] )
|
||||
{
|
||||
$L[$i] = $L[$i] -bxor $L[$j]
|
||||
$L[$j] = $L[$i] -bxor $L[$j]
|
||||
$L[$i] = $L[$i] -bxor $L[$j]
|
||||
}
|
||||
if( $j -gt 1 )
|
||||
{
|
||||
$t = [int] ( ( $j + 1 ) / 3 )
|
||||
$k = $j - $t + 1
|
||||
[Array]::Copy( [Int32[]] ( StoogeSort( $L[0..( $j - $t ) ] ) ), $L, $k )
|
||||
[Array]::ConstrainedCopy( [Int32[]] ( StoogeSort( $L[$t..$j ] ) ), 0, $L, $t, $k )
|
||||
[Array]::Copy( [Int32[]] ( StoogeSort( $L[0..( $j - $t ) ] ) ), $L, $k )
|
||||
}
|
||||
$L
|
||||
}
|
||||
|
||||
StoogeSort 9, 7, 5, 3, 1, 2, 4, 6, 8
|
||||
|
|
@ -1,15 +1,15 @@
|
|||
>>> 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:
|
||||
j = len(L) - 1
|
||||
if L[j] < L[i]:
|
||||
L[i], L[j] = L[j], L[i]
|
||||
if j - i > 1:
|
||||
t = (j - i + 1) // 3
|
||||
stoogesort(L, i , j-t)
|
||||
stoogesort(L, i+t, j )
|
||||
stoogesort(L, i , j-t)
|
||||
return L
|
||||
if j is None:
|
||||
j = len(L) - 1
|
||||
if L[j] < L[i]:
|
||||
L[i], L[j] = L[j], L[i]
|
||||
if j - i > 1:
|
||||
t = (j - i + 1) // 3
|
||||
stoogesort(L, i , j-t)
|
||||
stoogesort(L, i+t, j )
|
||||
stoogesort(L, i , j-t)
|
||||
return L
|
||||
|
||||
>>> stoogesort(data)
|
||||
[-6, -5, -3, -2, 1, 3, 3, 4, 5, 5, 7, 7, 7, 9, 10]
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
>>> def stoogesort(L, i, j):
|
||||
if L[j] < L[i]:
|
||||
L[i], L[j] = L[j], L[i]
|
||||
if j - i > 1:
|
||||
t = (j - i + 1) // 3
|
||||
stoogesort(L, i , j-t)
|
||||
stoogesort(L, i+t, j )
|
||||
stoogesort(L, i , j-t)
|
||||
return L
|
||||
if L[j] < L[i]:
|
||||
L[i], L[j] = L[j], L[i]
|
||||
if j - i > 1:
|
||||
t = (j - i + 1) // 3
|
||||
stoogesort(L, i , j-t)
|
||||
stoogesort(L, i+t, j )
|
||||
stoogesort(L, i , j-t)
|
||||
return L
|
||||
|
||||
>>> def stooge(L): return stoogesort(L, 0, len(L) - 1)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
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
|
||||
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 <- print(sample(21, 20))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Sorting algorithms/Stooge sort"
|
||||
file: %Sorting_algorithms-Stooge_sort.r3
|
||||
url: https://rosettacode.org/wiki/Sorting_algorithms-Stooge_sort
|
||||
]
|
||||
|
||||
stooge-sort: function/with [
|
||||
"Recursively sort a block in-place using the Stooge Sort algorithm"
|
||||
data [block!] "The block to sort"
|
||||
][
|
||||
;; Public entry point: delegate to the private helper with full index range
|
||||
stoogesort data 1 length? data
|
||||
][
|
||||
;; --- private context: defined once, shared across all calls to stooge-sort ---
|
||||
stoogesort: function [
|
||||
L [block! ] ;; The block to sort
|
||||
i [integer!] ;; Start index (1-based)
|
||||
j [integer!] ;; End index (1-based)
|
||||
][
|
||||
;; Swap endpoints if out of order
|
||||
if L/:j < L/:i [ swap at L j at L i ]
|
||||
;; Recurse only if the subrange contains more than 2 elements
|
||||
if j - i > 1 [
|
||||
;; Divide the range into thirds; integer-truncate to get whole steps
|
||||
t: to integer! ((j - i + 1) / 3)
|
||||
stoogesort L i j - t ;; Sort first two-thirds
|
||||
stoogesort L i + t j ;; Sort last two-thirds
|
||||
stoogesort L i j - t ;; Sort first two-thirds again
|
||||
]
|
||||
L ;; Return the (now sorted) block
|
||||
]
|
||||
]
|
||||
|
||||
; --- example usage ---
|
||||
print ["Input: " mold data: [1 4 5 3 -6 3 7 10 -2 -5 7 5 9 -3 7]]
|
||||
print ["Sorted:" mold stooge-sort data]
|
||||
|
|
@ -1,20 +1,20 @@
|
|||
OrderedCollection extend [
|
||||
stoogeSortFrom: i to: j [
|
||||
(self at: j) < (self at: i)
|
||||
ifTrue: [ self swapElement: i with: j ].
|
||||
j - i > 1
|
||||
(self at: j) < (self at: i)
|
||||
ifTrue: [ self swapElement: i with: j ].
|
||||
j - i > 1
|
||||
ifTrue: [
|
||||
|t| t := (j - i + 1)//3.
|
||||
self stoogeSortFrom: i to: (j-t).
|
||||
self stoogeSortFrom: (i+t) to: j.
|
||||
self stoogeSortFrom: i to: (j-t)
|
||||
|t| t := (j - i + 1)//3.
|
||||
self stoogeSortFrom: i to: (j-t).
|
||||
self stoogeSortFrom: (i+t) to: j.
|
||||
self stoogeSortFrom: i to: (j-t)
|
||||
]
|
||||
]
|
||||
stoogeSort [ self stoogeSortFrom: 1 to: (self size) ]
|
||||
swapElement: i with: j [
|
||||
|t| t := self at: i.
|
||||
|t| t := self at: i.
|
||||
self at: i put: (self at: j).
|
||||
self at: j put: t
|
||||
self at: j put: t
|
||||
]
|
||||
].
|
||||
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ next i
|
|||
|
||||
print "unsort ";
|
||||
for i = a to b
|
||||
print array(i) using("####");
|
||||
if i = b then print ""; else print ", "; : fi
|
||||
print array(i) using("####");
|
||||
if i = b then print ""; else print ", "; : fi
|
||||
next i
|
||||
|
||||
stoogeSort(array(), a, b) // ordenar el array
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue