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,13 @@
(defun quickselect (n _list)
(let* ((ys (remove-if (lambda (x) (< (car _list) x)) (cdr _list)))
(zs (remove-if-not (lambda (x) (< (car _list) x)) (cdr _list)))
(l (length ys))
)
(cond ((< n l) (quickselect n ys))
((> n l) (quickselect (- n l 1) zs))
(t (car _list)))
)
)
(defparameter a '(9 8 7 6 5 0 1 2 3 4))
(format t "~a~&" (mapcar (lambda (x) (quickselect x a)) (loop for i from 0 below (length a) collect i)))

View file

@ -0,0 +1,24 @@
-module(quickselect).
-export([test/0]).
test() ->
V = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4],
lists:map(
fun(I) -> quickselect(I,V) end,
lists:seq(0, length(V) - 1)
).
quickselect(K, [X | Xs]) ->
{Ys, Zs} =
lists:partition(fun(E) -> E < X end, Xs),
L = length(Ys),
if
K < L ->
quickselect(K, Ys);
K > L ->
quickselect(K - L - 1, Zs);
true ->
X
end.

View file

@ -30,10 +30,10 @@ sub select( @vector,
when Same {
return @vector[$pivot-new-index];
}
when Decrease {
when More {
$right = $pivot-new-index - 1;
}
when Increase {
when Less {
$k -= $pivot-dist;
$left = $pivot-new-index + 1;
}

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*/

View 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.*/

View file

@ -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*/