Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

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