Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -1,3 +1,3 @@
qsort {1: e[?] ((<e)/) , ((=e)/) , ((>e)/)}
qsort {1: p[?] ((p)/) , (>p)/}
qsort 31 4 1 5 9 2 6 5 3 5 8
1 2 3 4 5 5 5 6 8 9 31

View file

@ -0,0 +1 @@
qsort=: (((<:#[) ,&$: (>#[)) (?@#{]))^:(1<#@~.)

View file

@ -0,0 +1 @@
qsort 7 4 1 5 9 8 2 4 6

View file

@ -0,0 +1,12 @@
sel=: 1 : 'u # ]'
quicksort=: 3 : 0
if.
1 >: #y
do.
y
else.
p=. y{~?#y
(quicksort y <sel p),(y =sel p),quicksort y >sel p
end.
)

View file

@ -1,12 +0,0 @@
sel=: 1 : 'u # ['
quicksort=: 3 : 0
if.
1 >: #y
do.
y
else.
e=. y{~?#y
(quicksort y <sel e),(y =sel e),quicksort y >sel e
end.
)

View file

@ -1 +1 @@
quicksort:{f:*x@1?#x;:[0=#x;x;,/(_f x@&x<f;x@&x=f;_f x@&x>f)]}
qsort:{$[2>#?x;x;,/o'x@&'~:\x<*1?x]}

View file

@ -1 +1 @@
quicksort 1 3 5 7 9 8 6 4 2
quicksort:{p:*x[1?#x];:[0=#x;x;,/(_f x[&x<p];x[&x=p];_f x[&x>p])]}

View file

@ -1 +1 @@
_f()
quicksort 1 3 5 7 9 8 6 4 2

View file

@ -1,9 +1 @@
:[
0=#x; / if length of x is zero
x; / then return x
/ else
,/( / join the results of:
_f x@&x<f / sort (recursively) elements less than f (pivot)
x@&x=f / element equal to f
_f x@&x>f) / sort (recursively) elements greater than f
]
p:*x[1?#x]

View file

@ -1 +1,9 @@
t@<t:1 3 5 7 9 8 6 4 2
:[
0=#x; / if length of x is zero
x; / then return x
/ else
,/( / join the results of:
_f x[&x<p] / sort (recursively) elements less than pivot p
x[&x=p] / elements equal to p
_f x[&x>p]) / sort (recursively) elements greater than p
]

View file

@ -0,0 +1 @@
t@<t:1 3 5 7 9 8 6 4 2

View file

@ -0,0 +1,24 @@
Elegant:
procedure expose stem.
push 1 stem.0
do while queued() > 0
pull l r
if l < r then do
m = (l+r)%2; p = stem.m; i = l-1; j = r+1
do forever
do until stem.j <= p
j = j-1
end
do until stem.i >= p
i = i+1
end
if i < j then do
t = stem.i; stem.i = stem.j; stem.j = t
end
else
leave
end
push l j; push j+1 r
end
end
return

View file

@ -0,0 +1,20 @@
Recursive:
procedure expose stem.
arg l r
m = (l+r)%2; p = stem.m
i = l; j = r
do while i <= j
do i = i while stem.i < p
end
do j = j by -1 while stem.j > p
end
if i <= j then do
t = stem.i; stem.i = stem.j; stem.j = t
i = i+1; j = j-1
end
end
if l < j then
call Recursive l j
if i < r then
call Recursive i r
return

View file

@ -0,0 +1,56 @@
Optimized:
procedure expose stem.
n = stem.0; s = 1; sl.1 = 1; sr.1 = n
do until s = 0
l = sl.s; r = sr.s; s = s-1
do until l >= r
if r-l < 11 then do
do i = l+1 to r
a = stem.i
do j=i-1 by -1 to l while stem.j > a
k = j+1; stem.k = stem.j
end
k = j+1; stem.k = a
end
if s = 0 then
leave
l = sl.s; r = sr.s; s = s-1
end
else do
m = (l+r)%2
if stem.l > stem.m then do
t = stem.l; stem.l = stem.m; stem.m = t
end
if stem.l > stem.r then do
t = stem.l; stem.l = stem.r; stem.r = t
end
if stem.m > stem.r then do
t = stem.m; stem.m = stem.r; stem.r = t
end
i = l; j = r; p = stem.m
do until i > j
do i = i while stem.i < p
end
do j = j by -1 while stem.j > p
end
if i <= j then do
t = stem.i; stem.i = stem.j; stem.j = t
i = i+1; j = j-1
end
end
if j-l < r-i then do
if i < r then do
s = s+1; sl.s = i; sr.s = r
end
r = j
end
else do
if l < j then do
s = s+1; sl.s = l; sr.s = j
end
l = i
end
end
end
end
return

View file

@ -0,0 +1,73 @@
numeric digits 9
parse version version; say version Digits() 'digits'
arg n v
if n = '' then n = 10
if v = '' then v = 1
show = (n > 0); n = Abs(n)
say 'Quicksort: Timing Version' v 'for' n 'random numbers'
call Generate
if show then call ShowSave
select
when v = 1 then do
call Save2Stem
call Time 'r'; call Qsort n; say 'Qsort' format(time('e'),3,3) 'seconds'
if show then call ShowStem
end
when v = 2 then do
call Time 'r'; call Save2Stack; say 'Save2Stack' format(time('e'),3,3) 'seconds'
call Time 'r'; call Quicksort; say 'Quicksort ' format(time('e'),3,3) 'seconds'
call Time 'r'; call Stack2Stem; say 'Stack2Stem' format(time('e'),3,3) 'seconds'
if show then call ShowStem
end
when v = 3 then do
call Save2Stem
call Time 'r'; call Elegant; say 'Elegant' format(time('e'),3,3) 'seconds'
if show then call ShowStem
end
when v = 4 then do
call Save2Stem
call Time 'r'; call Recursive 1 n; say 'Recursive' format(time('e'),3,3) 'seconds'
if show then call ShowStem
end
when v = 5 then do
call Save2Stem
call Time 'r'; call Optimized; say 'Optimized' format(time('e'),3,3) 'seconds'
if show then call ShowStem
end
otherwise nop
end
say
exit
Generate:
do x = 1 to n
save.x = 10000*Random(0,9999)+Random(0,9999)
end
save.0 = n
return
ShowSave:
do x = 1 to 5
say x save.x
end
do x = n-4 to n
say x save.x
end
return
ShowStem:
do x = 1 to 5
say x stem.x
end
do x = n-4 to n
say x stem.x
end
return
Save2Stem:
do x = 0 to n
stem.x = save.x
end
return
/* Sorting procedures follow, not shown here */

View file

@ -1,36 +1,20 @@
templates quicksort
templates partial
def first: $(1);
def last: $(2);
def pivot: $@quicksort($first);
@: $(1) + 1;
$(2) -> #
when <..~$@> do
def limit: $;
@quicksort($first): $@quicksort($limit);
@quicksort($limit): $pivot;
[ $first, $limit - 1 ] !
[ $limit + 1, $last ] !
when <?($@quicksort($) <$pivot~..>)> do
$ - 1 -> #
when <?($@quicksort($@) <..$pivot>)> do
@: $@ + 1; $ -> #
quicksort templates
partition templates
@ set [];
pivot is $(1);
[$(2..)... -> #]!
$pivot !
$@ !
when <|..$pivot> do
$ !
otherwise
def temp: $@quicksort($@);
@quicksort($@): $@quicksort($);
@quicksort($): $temp;
@: $@ + 1; $ - 1 -> #
end partial
@: $;
[1, $@::length] -> #
$@ !
when <?($(1) <..~$(2)>)> do
$ -> partial -> #
..|@ set $;
end partition
[$ -> #] !
when <|[](1..)> do
$ -> partition -> # !
when <~|[]> do
$ !
end quicksort
[4,5,3,8,1,2,6,7,9,8,5] -> quicksort -> !OUT::write
[4,5,3,8,1,2,6,7,9,8,5] -> quicksort !

View file

@ -0,0 +1,36 @@
templates quicksort
templates partial
def first: $(1);
def last: $(2);
def pivot: $@quicksort($first);
@: $(1) + 1;
$(2) -> #
when <..~$@> do
def limit: $;
@quicksort($first): $@quicksort($limit);
@quicksort($limit): $pivot;
[ $first, $limit - 1 ] !
[ $limit + 1, $last ] !
when <?($@quicksort($) <$pivot~..>)> do
$ - 1 -> #
when <?($@quicksort($@) <..$pivot>)> do
@: $@ + 1; $ -> #
otherwise
def temp: $@quicksort($@);
@quicksort($@): $@quicksort($);
@quicksort($): $temp;
@: $@ + 1; $ - 1 -> #
end partial
@: $;
[1, $@::length] -> #
$@ !
when <?($(1) <..~$(2)>)> do
$ -> partial -> #
end quicksort
[4,5,3,8,1,2,6,7,9,8,5] -> quicksort -> !OUT::write

View file

@ -0,0 +1,37 @@
quicksort templates
partial templates
first is $(1);
last is $(2);
pivot is $@quicksort($first);
@ set $first + 1;
$last -> # !
when <|..~$@> do
limit is $;
@quicksort($first) set $@quicksort($limit);
@quicksort($limit) set $pivot;
[ $first, $limit - 1 ] !
[ $limit + 1, $last ] !
when <|?($@quicksort($) matches <|$pivot~..>)> do
$ - 1 -> # !
when <|?($@quicksort($@) matches <|..$pivot>)> do
@ set $@ + 1; $ -> # !
otherwise
temp is $@quicksort($@);
@quicksort($@) set $@quicksort($);
@quicksort($) set $temp;
@ set $@ + 1; $ - 1 -> # !
end partial
@ set $;
[1, $@::length] -> !#
$@ !
when <|?($(1) matches <|..~$(2)>)> do
$ -> partial -> !#
end quicksort
[4,5,3,8,1,2,6,7,9,8,5] -> quicksort !