Data update

This commit is contained in:
Ingy döt Net 2025-06-11 20:16:52 -04:00
parent 72eb4943cb
commit 4d5544505c
2347 changed files with 62432 additions and 16731 deletions

View file

@ -1,107 +1,87 @@
/*REXX program sorts a stemmed array using the quicksort algorithm. */
call gen@ /*generate the elements for the array. */
call show@ 'before sort' /*show the before array elements. */
call qSort # /*invoke the quicksort subroutine. */
call show@ ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
inOrder: parse arg n; do j=1 for n-1; k= j+1; if @.j>@.k then return 0; end; return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
qSort: procedure expose @.; a.1=1; parse arg b.1; $= 1 /*access @.; get @. size; pivot.*/
if inOrder(b.1) then return /*Array already in order? Return*/
do while $\==0; L= a.$; t= b.$; $= $ - 1; if t<2 then iterate
H= L + t - 1; ?= L + t % 2
if @.H<@.L then if @.?<@.H then do; p= @.H; @.H= @.L; end
else if @.?>@.L then p= @.L
else do; p= @.?; @.?= @.L; end
else if @.?<@.L then p=@.L
else if @.?>@.H then do; p= @.H; @.H= @.L; end
else do; p= @.?; @.?= @.L; end
j= L+1; k= h
do forever
do j=j while j<=k & @.j<=p; end /*a teeny─tiny loop.*/
do k=k by -1 while j< k & @.k>=p; end /*another " " */
if j>=k then leave /*segment finished? */
_= @.j; @.j= @.k; @.k= _ /*swap J&K elements.*/
end /*forever*/
$= $ + 1
k= j - 1; @.L= @.k; @.k= p
if j<=? then do; a.$= j; b.$= H-j+1; $= $+1; a.$= L; b.$= k-L; end
else do; a.$= L; b.$= k-L; $= $+1; a.$= j; b.$= H-j+1; end
end /*while $¬==0*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show@: w= length(#); do j=1 for #; say 'element' right(j,w) arg(1)":" @.j; end
say copies('', maxL + w + 22) /*display a separator (between outputs)*/
return
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
gen@: @.=; maxL=0 /*assign a default value for the array.*/
@.1 = " Rivers that form part of a (USA) state's border " /*this value is adjusted later to include a prefix & suffix.*/
@.2 = '=' /*this value is expanded later. */
@.3 = "Perdido River Alabama, Florida"
@.4 = "Chattahoochee River Alabama, Georgia"
@.5 = "Tennessee River Alabama, Kentucky, Mississippi, Tennessee"
@.6 = "Colorado River Arizona, California, Nevada, Baja California (Mexico)"
@.7 = "Mississippi River Arkansas, Illinois, Iowa, Kentucky, Minnesota, Mississippi, Missouri, Tennessee, Louisiana, Wisconsin"
@.8 = "St. Francis River Arkansas, Missouri"
@.9 = "Poteau River Arkansas, Oklahoma"
@.10 = "Arkansas River Arkansas, Oklahoma"
@.11 = "Red River (Mississippi watershed) Arkansas, Oklahoma, Texas"
@.12 = "Byram River Connecticut, New York"
@.13 = "Pawcatuck River Connecticut, Rhode Island and Providence Plantations"
@.14 = "Delaware River Delaware, New Jersey, New York, Pennsylvania"
@.15 = "Potomac River District of Columbia, Maryland, Virginia, West Virginia"
@.16 = "St. Marys River Florida, Georgia"
@.17 = "Chattooga River Georgia, South Carolina"
@.18 = "Tugaloo River Georgia, South Carolina"
@.19 = "Savannah River Georgia, South Carolina"
@.20 = "Snake River Idaho, Oregon, Washington"
@.21 = "Wabash River Illinois, Indiana"
@.22 = "Ohio River Illinois, Indiana, Kentucky, Ohio, West Virginia"
@.23 = "Great Miami River (mouth only) Indiana, Ohio"
@.24 = "Des Moines River Iowa, Missouri"
@.25 = "Big Sioux River Iowa, South Dakota"
@.26 = "Missouri River Kansas, Iowa, Missouri, Nebraska, South Dakota"
@.27 = "Tug Fork River Kentucky, Virginia, West Virginia"
@.28 = "Big Sandy River Kentucky, West Virginia"
@.29 = "Pearl River Louisiana, Mississippi"
@.30 = "Sabine River Louisiana, Texas"
@.31 = "Monument Creek Maine, New Brunswick (Canada)"
@.32 = "St. Croix River Maine, New Brunswick (Canada)"
@.33 = "Piscataqua River Maine, New Hampshire"
@.34 = "St. Francis River Maine, Quebec (Canada)"
@.35 = "St. John River Maine, Quebec (Canada)"
@.36 = "Pocomoke River Maryland, Virginia"
@.37 = "Palmer River Massachusetts, Rhode Island and Providence Plantations"
@.38 = "Runnins River Massachusetts, Rhode Island and Providence Plantations"
@.39 = "Montreal River Michigan (upper peninsula), Wisconsin"
@.40 = "Detroit River Michigan, Ontario (Canada)"
@.41 = "St. Clair River Michigan, Ontario (Canada)"
@.42 = "St. Marys River Michigan, Ontario (Canada)"
@.43 = "Brule River Michigan, Wisconsin"
@.44 = "Menominee River Michigan, Wisconsin"
@.45 = "Red River of the North Minnesota, North Dakota"
@.46 = "Bois de Sioux River Minnesota, North Dakota, South Dakota"
@.47 = "Pigeon River Minnesota, Ontario (Canada)"
@.48 = "Rainy River Minnesota, Ontario (Canada)"
@.49 = "St. Croix River Minnesota, Wisconsin"
@.50 = "St. Louis River Minnesota, Wisconsin"
@.51 = "Halls Stream New Hampshire, Canada"
@.52 = "Salmon Falls River New Hampshire, Maine"
@.53 = "Connecticut River New Hampshire, Vermont"
@.54 = "Arthur Kill New Jersey, New York (tidal strait)"
@.55 = "Kill Van Kull New Jersey, New York (tidal strait)"
@.56 = "Hudson River (lower part only) New Jersey, New York"
@.57 = "Rio Grande New Mexico, Texas, Tamaulipas (Mexico), Nuevo Leon (Mexico), Coahuila de Zaragoza (Mexico), Chihuahua (Mexico)"
@.58 = "Niagara River New York, Ontario (Canada)"
@.59 = "St. Lawrence River New York, Ontario (Canada)"
@.60 = "Poultney River New York, Vermont"
@.61 = "Catawba River North Carolina, South Carolina"
@.62 = "Blackwater River North Carolina, Virginia"
@.63 = "Columbia River Oregon, Washington"
do #=1 until @.#=='' /*find how many entries in array, and */
maxL=max(maxL, length(@.#)) /* also find the maximum width entry.*/
end /*#*/; #= #-1 /*adjust the highest element number. */
@.1= center(@.1, maxL, '-') /* " " header information. */
@.2= copies(@.2, maxL) /* " " " separator. */
return
/*REXX*/
a = '4 65 2 -31 0 99 83 782 1'
do i = 1 to words(a)
queue word(a, i)
end
call quickSort
parse pull item
do queued()
call charout ,item', '
parse pull item
end
say item
exit
quickSort: procedure
/* In classic Rexx, arguments are passed by value, not by reference so stems
cannot be passed as arguments nor used as return values. Putting their
contents on the external data queue is a way to bypass this issue. */
/* construct the input stem */
arr.0 = queued()
do i = 1 to arr.0
parse pull arr.i
end
less.0 = 0
pivotList.0 = 0
more.0 = 0
if arr.0 <= 1 then do
if arr.0 = 1 then
queue arr.1
return
end
else do
pivot = arr.1
do i = 1 to arr.0
item = arr.i
select
when item < pivot then do
j = less.0 + 1
less.j = item
less.0 = j
end
when item > pivot then do
j = more.0 + 1
more.j = item
more.0 = j
end
otherwise
j = pivotList.0 + 1
pivotList.j = item
pivotList.0 = j
end
end
end
/* recursive call to sort the less. stem */
do i = 1 to less.0
queue less.i
end
if queued() > 0 then do
call quickSort
less.0 = queued()
do i = 1 to less.0
parse pull less.i
end
end
/* recursive call to sort the more. stem */
do i = 1 to more.0
queue more.i
end
if queued() > 0 then do
call quickSort
more.0 = queued()
do i = 1 to more.0
parse pull more.i
end
end
/* put the contents of all 3 stems on the queue in order */
do i = 1 to less.0
queue less.i
end
do i = 1 to pivotList.0
queue pivotList.i
end
do i = 1 to more.0
queue more.i
end
return

View file

@ -1,87 +1,24 @@
/*REXX*/
a = '4 65 2 -31 0 99 83 782 1'
do i = 1 to words(a)
queue word(a, i)
end
call quickSort
parse pull item
do queued()
call charout ,item', '
parse pull item
end
say item
exit
quickSort: procedure
/* In classic Rexx, arguments are passed by value, not by reference so stems
cannot be passed as arguments nor used as return values. Putting their
contents on the external data queue is a way to bypass this issue. */
/* construct the input stem */
arr.0 = queued()
do i = 1 to arr.0
parse pull arr.i
end
less.0 = 0
pivotList.0 = 0
more.0 = 0
if arr.0 <= 1 then do
if arr.0 = 1 then
queue arr.1
return
end
else do
pivot = arr.1
do i = 1 to arr.0
item = arr.i
select
when item < pivot then do
j = less.0 + 1
less.j = item
less.0 = j
end
when item > pivot then do
j = more.0 + 1
more.j = item
more.0 = j
end
otherwise
j = pivotList.0 + 1
pivotList.j = item
pivotList.0 = j
end
end
end
/* recursive call to sort the less. stem */
do i = 1 to less.0
queue less.i
end
if queued() > 0 then do
call quickSort
less.0 = queued()
do i = 1 to less.0
parse pull less.i
end
end
/* recursive call to sort the more. stem */
do i = 1 to more.0
queue more.i
end
if queued() > 0 then do
call quickSort
more.0 = queued()
do i = 1 to more.0
parse pull more.i
end
end
/* put the contents of all 3 stems on the queue in order */
do i = 1 to less.0
queue less.i
end
do i = 1 to pivotList.0
queue pivotList.i
end
do i = 1 to more.0
queue more.i
end
return
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

@ -1,24 +1,20 @@
Elegant:
Recursive:
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
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

@ -1,20 +1,56 @@
Recursive:
Optimized:
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
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
if l < j then
call Recursive l j
if i < r then
call Recursive i r
return

View file

@ -1,10 +1,204 @@
include Settings
say 'QUICKSORT - 4 Mar 2025'
say version
say
numeric digits 9
arg n v
if n = '' then n = 10
if v = '' then v = 1
show = (n > 0); n = Abs(n)
say 'Timing Version' v 'for' n 'random numbers'
call Generate
if show then call ShowSave
if v = 0 | v = 1 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
if v = 0 | v = 2 then do
call Save2Stem
call Time 'r'; call Elegant; say 'Elegant' format(time('e'),3,3) 'seconds'
if show then call ShowStem
end
if v = 0 | v = 3 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
if v = 0 | v = 4 then do
call Save2Stem
call Time 'r'; call Optimized; say 'Optimized' format(time('e'),3,3) 'seconds'
if show then call ShowStem
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
say
return
ShowStem:
do x = 1 to 5
say x stem.x
end
do x = n-4 to n
say x stem.x
end
say
return
Save2Stem:
do x = 0 to n
stem.x = save.x
end
return
Save2Stack:
do x = 1 to n
queue save.x
end
return
Quicksort: procedure
arr.0 = queued()
do i = 1 to arr.0
parse pull arr.i
end
less.0 = 0
pivotList.0 = 0
more.0 = 0
if arr.0 <= 1 then do
if arr.0 = 1 then
queue arr.1
return
end
else do
pivot = arr.1
do i = 1 to arr.0
item = arr.i
select
when item < pivot then do
j = less.0 + 1
less.j = item
less.0 = j
end
when item > pivot then do
j = more.0 + 1
more.j = item
more.0 = j
end
otherwise
j = pivotList.0 + 1
pivotList.j = item
pivotList.0 = j
end
end
end
do i = 1 to less.0
queue less.i
end
if queued() > 0 then do
call quickSort
less.0 = queued()
do i = 1 to less.0
parse pull less.i
end
end
do i = 1 to more.0
queue more.i
end
if queued() > 0 then do
call quickSort
more.0 = queued()
do i = 1 to more.0
parse pull more.i
end
end
do i = 1 to less.0
queue less.i
end
do i = 1 to pivotList.0
queue pivotList.i
end
do i = 1 to more.0
queue more.i
end
return
Stack2Stem:
do x = 1 to n
parse pull stem.x
end
return
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
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
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
if r-l < 20 then do
do i = l+1 to r
a = stem.i
do j=i-1 by -1 to l while stem.j > a
@ -54,3 +248,5 @@ do until s = 0
end
end
return
include Abend

View file

@ -1,73 +0,0 @@
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 */