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,30 +1,32 @@
/*REXX pgm sorts a list (which may be numbers) using quick select algorithm.*/
parse arg list; if list='' then list=9 8 7 6 5 0 1 2 3 4 /*use the default?*/
do #=1 for words(list); @.#=word(list,#) /*assign item──►@.*/
end /*#*/ /* [↑] #: number of items in the list*/
#=#-1 /*adjust number of items in the list. */
do j=1 for # /*show 1 ──► # of items place and value*/
say right('item',20) right(j,length(#))", value: " qSel(1,#,j)
/*REXX program sorts a list (which may be numbers) by using the quick select algorithm. */
parse arg list; if list='' then list=9 8 7 6 5 0 1 2 3 4 /*Not given? Use default.*/
say right('list: ', 22) list
say
do #=1 for words(list); @.#=word(list,#) /*assign all the items ──► @. (array). */
end /*#*/ /* [↑] #: number of items in the list.*/
#=#-1 /*adjust number of items in the list. */
do j=1 for # /*show 1 ──► # items place and value.*/
say right('item', 20) right(j, length(#))", value: " qSel(1, #, j)
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────QPART subroutine──────────────────────────*/
qPart: procedure expose @.; parse arg L 1 ?,R,X; xVal = @.X
parse value @.X @.R with @.R @.X /*swap the two names items (X and R). */
do k=L to R-1 /*process the left side of the list. */
if @.k>xVal then iterate /*when an item > item #X, then skip it.*/
parse value @.? @.k with @.k @.? /*swap the two named items (? and K). */
?=?+1 /*bump the item number (point to next)*/
end /*k*/
parse value @.R @.? with @.? @.R /*swap the two named items (R and ?). */
return ? /*return item num.*/
/*──────────────────────────────────QSEL subroutine───────────────────────────*/
qSel: procedure expose @.; parse arg L,R,z; if L==R then return @.L /*one?*/
do forever /*keep searching until we're all done. */
new=qPart(L, R, (L+R)%2) /*partition the list into roughly ½. */
dist=new-L+1 /*calculate the pivot distance less L+1*/
if dist==z then return @.new /*we're all done with this pivot part. */
else if z<dist then R=new-1 /*decrease right half. */
else do; z=z-dist /*decrease the distance.*/
L=new+1 /*increase the left half*/
end
end /*forever*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
qPart: procedure expose @.; parse arg L 1 ?,R,X; xVal=@.X
parse value @.X @.R with @.R @.X /*swap the two names items (X and R). */
do k=L to R-1 /*process the left side of the list. */
if @.k>xVal then iterate /*when an item > item #X, then skip it.*/
parse value @.? @.k with @.k @.? /*swap the two named items (? and K). */
?=?+1 /*bump the item number (point to next).*/
end /*k*/
parse value @.R @.? with @.? @.R /*swap the two named items (R and ?). */
return ? /*return the item number to invoker. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
qSel: procedure expose @.; parse arg L,R,z; if L==R then return @.L /*only one item?*/
do forever /*keep searching until we're all done. */
new=qPart(L, R, (L+R) % 2) /*partition the list into roughly ½. */
$=new-L+1 /*calculate pivot distance less L+1. */
if $==z then return @.new /*we're all done with this pivot part. */
else if z<$ then R=new-1 /*decrease the right half of the array.*/
else do; z=z-$ /*decrease the distance. */
L=new+1 /*increase the left half *f the array.*/
end
end /*forever*/

View file

@ -1,32 +1,34 @@
/*REXX pgm sorts a list (which may be numbers) using quick select algorithm.*/
parse arg list; if list='' then list=9 8 7 6 5 0 1 2 3 4 /*use the default?*/
do #=1 for words(list); @.#=word(list,#) /*assign item──►@.*/
end /*#*/ /* [↑] #: number of items in the list*/
#=#-1 /*adjust number of items in the list. */
do j=1 for # /*show 1 ──► # of items place and value*/
say right('item',20) right(j,length(#))", value: " qSel(1,#,j)
/*REXX program sorts a list (which may be numbers) by using the quick select algorithm. */
parse arg list; if list='' then list=9 8 7 6 5 0 1 2 3 4 /*Not given? Use default.*/
say right('list: ', 22) list
say
do #=1 for words(list); @.#=word(list,#) /*assign all the items ──► @. (array). */
end /*#*/ /* [↑] #: number of items in the list.*/
#=#-1 /*adjust number of items in the list. */
do j=1 for # /*show 1 ──► # items place and value.*/
say right('item', 20) right(j, length(#))", value: " qSel(1, #, j)
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────QPART subroutine──────────────────────────*/
qPart: procedure expose @.; parse arg L 1 ?,R,X; xVal = @.X
call swap X,R /*swap the two named items (X and R). */
do k=L to R-1 /*process the left side of the list. */
if @.k>xVal then iterate /*when an item > item #X, then skip it.*/
call swap ?,k /*swap the two named items (? and K). */
?=?+1 /*bump item number we're working with. */
end /*k*/
call swap R,? /*swap the two named items (R and ?). */
return ? /*return item num.*/
/*──────────────────────────────────QSEL subroutine───────────────────────────*/
qSel: procedure expose @.; parse arg L,R,z; if L==R then return @.L /*one?*/
do forever /*keep searching until we're all done. */
new=qPart(L, R, (L+R)%2) /*partition the list into roughly ½. */
dist=new-L+1 /*calculate the pivot distance less L+1*/
if dist==z then return @.new /*we're all done with this pivot part. */
else if z<dist then R=new-1 /*decrease right half. */
else do; z=z-dist /*decrease the distance.*/
L=new+1 /*increase the left half*/
end
end /*forever*/
/*──────────────────────────────────SWAP subroutine───────────────────────────*/
swap: parse arg _1,_2; parse value @._1 @._2 with @._2 @._1; return /*swap 2.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
qPart: procedure expose @.; parse arg L 1 ?,R,X; xVal=@.X
call swap X,R /*swap the two named items (X and R). */
do k=L to R-1 /*process the left side of the list. */
if @.k>xVal then iterate /*when an item > item #X, then skip it.*/
call swap ?,k /*swap the two named items (? and K). */
?=?+1 /*bump the item number (point to next).*/
end /*k*/
call swap R,? /*swap the two named items (R and ?). */
return ? /*return the item number to invoker. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
qSel: procedure expose @.; parse arg L,R,z; if L==R then return @.L /*only one item?*/
do forever /*keep searching until we're all done. */
new=qPart(L, R, (L+R)%2) /*partition the list into roughly ½. */
$=new-L+1 /*calculate the pivot distance less L+1*/
if $==z then return @.new /*we're all done with this pivot part. */
else if z<$ then R=new-1 /*decrease the right half of the array.*/
else do; z=z-$ /*decrease the distance. */
L=new+1 /*increase the left half of the array.*/
end
end /*forever*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
swap: parse arg _1,_2; parse value @._1 @._2 with @._2 @._1; return /*swap 2 items.*/