Advertisement
Jan-Langevad

BUBBLESORT.TXT

Dec 11th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | Source Code | 0 0
  1. \ BUBBLESORT.TXT
  2. : BUBBLE ( a1 ... an n-1 -- one pass )
  3. DUP
  4. IF >R
  5. OVER OVER <
  6. IF SWAP
  7. THEN
  8. R> SWAP >R 1-
  9. RECURSE ( or BUBBLE in other systems )
  10. R>
  11. ELSE DROP
  12. THEN
  13. ;
  14. : SORT ( a1 .. an n -- sorted )
  15. 1- DUP 0
  16. DO >R R@
  17. BUBBLE
  18. R>
  19. LOOP
  20. DROP
  21. ;
  22. -3 2 4 2 100 -12 9 0 8 SORT \ Example call
  23. ( Result: 100 9 4 2 2 0 -3 -12 --> )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement