Re: forth Insertion Sort
- From: "bnhcomputing" <bnhcomputing@xxxxxxxxx>
- Date: 4 May 2006 10:12:16 -0700
Yes he did, but I am unable to find a FORTH that will run the solution.
Gforth get's all the way to the :noname definition and then errors on
"third" (Line 63). Win32Forth errors on "postpone i' " (Line 14). Mr.
Melice example is great as he describes in detail the how and why, I
just can't seem to be able to execute it.
All suggestion Appreciated.
<For Reference >
\ INSERTION-SORT sample.
\ COMPARE: returns...
\ -1 if array[i] < array[j]
\ 0 if array[i] = array[j]
\ +1 if array[i] > array[j]
defer compare-key ( array i j -- result )
\ EXCHANGE array[i], array[j].
defer swap-data ( array i j -- )
\ TOOL: in a DO..LOOP, II returns the descendant index.
: ii ( -- index )
postpone i'
postpone i
postpone -
postpone 1- ; immediate
\ ---------------------------------
\ WHILE array[i+1]<array[i] AND i>=0, Swap(array[i], array[i+1]), i=i-1
\ There is another, faster way; not using SWAP-DATA:
\ TEMP=array[i+1]
\ WHILE I>=0 AND array[i+1]<array[i] DO array[i+1]=array[i], i=i-1
\ array[i+1]=TEMP
: move-key-down ( array index -- )
0 ?do
dup ii dup 1+ ( array i i+1 )
compare-key 0< ( array pair-sorted? )
if leave then
dup ii dup 1+ swap-data
loop drop ;
\ Find the first non-sorted pair, then MOVE-KEY-DOWN.
: insertion-sort ( array count -- )
1 ?do
dup i dup 1- compare-key 0< if
dup i move-key-down
then
loop drop ;
\ -----------------------
\ Test a bit complicated because I don't use specialized array access
word.
create myarray
here
9 , 3 , 4 , 12 , 7 , 1 , 4 , 3 , 2 ,
here swap - cell / constant mycount
:noname [ is compare-key ] ( array i j-- result )
cells third + @ >r cells + @ r>
( a b ) 2dup > if 2drop 1 else < then ;
:noname [ is swap-data ] ( array i j -- )
cells third + >r cells + r> ( a' b' )
2dup @ >r @ swap ! r> swap ! ;
: test
myarray mycount insertion-sort
cr
myarray mycount 0 ?do @+ . loop drop
cr ;
.
- Follow-Ups:
- Re: forth Insertion Sort
- From: Marcel Hendrix
- Re: forth Insertion Sort
- From: Charles Melice
- Re: Comparison of Commercial Windows Forths?
- From: Marcel Hendrix
- Re: forth Insertion Sort
- From: Elizabeth D Rather
- Re: forth Insertion Sort
- References:
- forth Insertion Sort
- From: bnhcomputing
- Re: forth Insertion Sort
- From: Jerry Avins
- forth Insertion Sort
- Prev by Date: [sftalk] Re: Forth Insertion sort (Research Paper)
- Next by Date: Re: forth Insertion Sort
- Previous by thread: Re: forth Insertion Sort
- Next by thread: Re: forth Insertion Sort
- Index(es):
Relevant Pages
|