Jump to content

Sorting algorithms/Bubble sort: Difference between revisions

Line 124:
==[[Forth]]==
[[Category:Forth]]
Sorts the 'cnt' cells stored at 'addr' using the test stored in the deferred word 'bubble-test'. Uses forth local variables for clarity.
 
defer bubble-test
' > is bubble-test
: bubble { addr cnt -- }
cnt 1 DOdo
addr cnt i - cells bounds DOdo
i 2@ >bubble-test if i 2@ swap i 2! then
cell +LOOP loop
LOOPloop ;
 
This is the same algorithm done without the local variables:
 
: bubble ( addr cnt -- )
dup 1 DOdo
2dup i - cells bounds DOdo
i 2@ >bubble-test if i 2@ swap i 2! then
cell +LOOP loop
LOOPloop ;
 
Test either version with this:
create test
8 , 1 , 4 , 2 , 10 , 3 , 7 , 9 , 6 , 5 ,
here test - cell / constant tcnt
test tcnt cells dump
' > is bubble-test
test tcnt bubble
test tcnt cells dump
' < is bubble-test
test tcnt bubble
test tcnt cells dump
 
==[[Haskell]]==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.