Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
30
Task/Quickselect-algorithm/REXX/quickselect-algorithm-1.rexx
Normal file
30
Task/Quickselect-algorithm/REXX/quickselect-algorithm-1.rexx
Normal 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*/
|
||||
32
Task/Quickselect-algorithm/REXX/quickselect-algorithm-2.rexx
Normal file
32
Task/Quickselect-algorithm/REXX/quickselect-algorithm-2.rexx
Normal file
|
|
@ -0,0 +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)
|
||||
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.*/
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
/*REXX program sorts a list (which may be numbers) using quick select.*/
|
||||
parse arg list; if list='' then list=9 8 7 6 5 0 1 2 3 4 /*default?*/
|
||||
do #=1 for words(list); @.#=word(list,#); end /*#*/; #=#-1
|
||||
/* [↓] #=number of items in list*/
|
||||
do j=1 for # /*show 1 ──► # place and value.*/
|
||||
say ' item' right(j,length(#))", value: " qSel(1,#,j)
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────QPART subroutine────────────────────*/
|
||||
qPart: procedure expose @.; parse arg L 1 ?,R,pivotIndex;pVal=@.pivotIndex
|
||||
parse value @.pivotIndex @.R with @.R @.pivotIndex /*swap 2 items*/
|
||||
do k=L to R-1 /*process the left side of list. */
|
||||
if @.k>pVal then iterate /*when item>pivotValue, skip it. */
|
||||
parse value @.? @.k with @.k @.? /*swap 2 items*/
|
||||
?=?+1 /*next item. */
|
||||
end /*k*/
|
||||
parse value @.R @.? with @.? @.R /*swap 2 items*/
|
||||
return ?
|
||||
/*──────────────────────────────────QSEL subroutine─────────────────────*/
|
||||
qSel: procedure expose @.; parse arg L,R,z; if L==R then return @.L
|
||||
do forever /*keep looping until all done. */
|
||||
pivotNewIndex=qPart(L, R, (L+R)%2) /*partition the list into ≈ ½. */
|
||||
pivotDist=pivotNewIndex-L+1
|
||||
if pivotDist==z then return @.pivotNewIndex
|
||||
else if z<pivotDist then R=pivotNewIndex-1
|
||||
else do
|
||||
z=z-pivotDist
|
||||
L=pivotNewindex+1
|
||||
end
|
||||
end /*forever*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue