YAPC::EU 2018 Glasgow Update!
This commit is contained in:
parent
22f33d4004
commit
4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions
|
|
@ -0,0 +1,10 @@
|
|||
a = [5, 4, 3, 2, 1]
|
||||
puts a.sort
|
||||
# => [1, 2, 3, 4, 5]
|
||||
|
||||
puts a
|
||||
# => [5, 4, 3, 2, 1]
|
||||
|
||||
a.sort!
|
||||
puts a
|
||||
# => [1, 2, 3, 4, 5]
|
||||
41
Task/Sort-an-integer-array/Forth/sort-an-integer-array-2.fth
Normal file
41
Task/Sort-an-integer-array/Forth/sort-an-integer-array-2.fth
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
100000 CONSTANT SIZE
|
||||
|
||||
CREATE MYARRAY SIZE CELLS ALLOT
|
||||
|
||||
: [] ( n addr -- addr[n]) SWAP CELLS + ;
|
||||
|
||||
: FILLIT ( -- ) ( reversed order)
|
||||
SIZE 0 DO SIZE I - I MYARRAY [] ! LOOP ;
|
||||
|
||||
: SEEIT ( -- )
|
||||
SIZE 0 DO I MYARRAY [] ? LOOP ;
|
||||
|
||||
\ define non-standard words used by Quicksort author
|
||||
1 CELLS CONSTANT CELL
|
||||
CELL NEGATE CONSTANT -CELL
|
||||
: CELL- CELL - ;
|
||||
|
||||
: MID ( l r -- mid ) OVER - 2/ -CELL AND + ;
|
||||
|
||||
: EXCH ( addr1 addr2 -- )
|
||||
OVER @ OVER @ ( read values)
|
||||
SWAP ROT ! SWAP ! ; ( exchange values)
|
||||
|
||||
: PARTITION ( l r -- l r r2 l2 )
|
||||
2DUP MID @ >R ( r: pivot )
|
||||
2DUP
|
||||
BEGIN
|
||||
SWAP BEGIN DUP @ R@ < WHILE CELL+ REPEAT
|
||||
SWAP BEGIN R@ OVER @ < WHILE CELL- REPEAT
|
||||
2DUP <= IF 2DUP EXCH >R CELL+ R> CELL- THEN
|
||||
2DUP >
|
||||
UNTIL
|
||||
R> DROP ;
|
||||
|
||||
: QSORT ( l r -- )
|
||||
PARTITION SWAP ROT
|
||||
2DUP < IF RECURSE ELSE 2DROP THEN
|
||||
2DUP < IF RECURSE ELSE 2DROP THEN ;
|
||||
|
||||
: QUICKSORT ( array len -- )
|
||||
DUP 2 < IF 2DROP EXIT THEN 1- CELLS OVER + QSORT ;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
FILLIT ok
|
||||
MYARRAY SIZE QUICKSORT ok
|
||||
|
|
@ -1,37 +1,29 @@
|
|||
/*REXX program sorts an array (using E-sort), in this case, the array contains integers.*/
|
||||
/*REXX program sorts an array (using E─sort), in this case, the array contains integers.*/
|
||||
numeric digits 30 /*enables handling larger Euler numbers*/
|
||||
@. = 0 /*the default for all Euler numbers. */
|
||||
@.1 = 1
|
||||
@.3 = -1
|
||||
@.5 = 5
|
||||
@.7 = -61
|
||||
@.9 = 1385
|
||||
@.11= -50521
|
||||
@.13= 2702765
|
||||
@.15= -199360981
|
||||
@.17= 19391512145
|
||||
@.19= -2404879675441
|
||||
@.21= 370371188237525
|
||||
size=21 /*indicate that there're 21 Euler #s.*/
|
||||
call tell 'un-sorted' /*display the array before the eSsort. */
|
||||
call eSort size /*sort the array of some Euler numbers.*/
|
||||
call tell ' sorted' /*display the array after the sort. */
|
||||
@. = 0; @.1 = 1
|
||||
@.3 = -1; @.5 = 5
|
||||
@.7 = -61; @.9 = 1385
|
||||
@.11= -50521; @.13= 2702765
|
||||
@.15= -199360981; @.17= 19391512145
|
||||
@.19= -2404879675441; @.21= 370371188237525
|
||||
#= 21 /*indicate there're 21 Euler numbers.*/
|
||||
call tell 'unsorted' /*display the array before the eSort. */
|
||||
call eSort # /*sort the array of some Euler numbers.*/
|
||||
call tell ' sorted' /*display the array after the eSort. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
eSort: procedure expose @.; parse arg N; h=N
|
||||
do while h>1; h=h%2
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while @.k<@.j
|
||||
parse value @.j @.k with @.k @.j /*swap two array elements.*/
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
eSort: procedure expose @.; parse arg N; h=N /*an eXchange sort.*/
|
||||
do while h>1; h= h%2 /*define a segment.*/
|
||||
do i=1 for N-h; j=i; k= h+i /*sort top segment.*/
|
||||
do while @.k<@.j /*see if need swap.*/
|
||||
parse value @.j @.k with @.k @.j /*swap two elements*/
|
||||
if h>=j then leave; j= j-h; k= k-h /*this part sorted?*/
|
||||
end /*while @.k<@.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
tell: say center(arg(1), 50, '─')
|
||||
do j=1 for size
|
||||
say arg(1) 'array element' right(j, length(size))'='right(@.j, 20)
|
||||
end /*j*/
|
||||
say
|
||||
tell: say copies('─', 65); _= left('',9); w= length(#)
|
||||
do j=1 for #; say _ arg(1) 'array element' right(j, w)"="right(@.j, 20)
|
||||
end /*j*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ $= /*list: define as nul
|
|||
say ' sorted =' space($) /*display the sorted list. */
|
||||
exit /*stick a fork in it, we're all done.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
eSort: procedure expose @.; parse arg N; h=N /*get item count for array. */
|
||||
do while h>1; h=h%2 /*partition the array. */
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while @.k<@.j /*keep swapping while less. */
|
||||
parse value @.j @.k with @.k @.j /*swap two array elements. */
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
end /*while @.k<@.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
eSort: procedure expose @.; parse arg N; h=N /*an eXchange sort.*/
|
||||
do while h>1; h= h%2 /*define a segment.*/
|
||||
do i=1 for N-h; j=i; k= h+i /*sort top segment.*/
|
||||
do while @.k<@.j /*see if need swap.*/
|
||||
parse value @.j @.k with @.k @.j /*swap two elements*/
|
||||
if h>=j then leave; j= j-h; k= k-h /*this part sorted?*/
|
||||
end /*while @.k<@.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue