[ Back to the overview Matrix ]

Test case : Bubble sort using Befunge-93

Lines used: 16
v
 XXXXXXXXXXXXXXXXXXXXXXXXXX                Data section.
 XX
>    011p v                                Initialization.
v &,">"   <                 <<
>:!#v_11g2+1p11g1+:11p64*`#v_^             "Read a number" loop.
    $                      v               Stops when n=25 or a[n]=0.
v<<<<<<<<<<<<<<<<<<<<<<<<<<<
              >v             > :12p::2+1v  Bubblesort.  The compact
>122p0>:11g1--|> ::2+1g\3+1g`|  v1g1+3\g<  block to the right is the
^     ^       v              v  >2g2+1p1v  swap routine, using (2,1)
^     ^<<<<<<< <<<<<<< +1 <<<<p220p1+3g2<  as temporary storage.
|  !g22       <                            DONE==true?
   v+1<<<<<<<<<," "<
> 0>:11g-!#v_:2+1g.^                       "Print numbers" loop.
           >52*,                        @  Done.
           
A longer version, which is not valid Befuge93 because its longer than 25 lines, but easier to read:
  
v This is the data section.
  'count' is at (1,1)     'arr' is at (2,1)--(26,1)
  'i' is temp. at (1,2)   'DONE' is at (2,2)

> 011p v  Initialize count to zero.
v      <                              <<<<<<<<<<<
>    v    Give a ">" prompt and input a number. ^
>11g.v    Clear line above to show 'count' too. ^
v,">"<                                          ^
&  If the result is zero, go to the next stage. ^
   Otherwise, put the number into 'arr[count]'. ^
        If 'count' is 25, go to the next stage. ^
  >v                                            ^
>:|> 11g2+1p 11g1+:11p 64*` #v_>>>>>>>>>>>>>>>>>^
  $                          v
v<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
v

  'arr[0..count-1]' now holds some integers.
  Bubble-sort them using the following algorithm:
    do
      set DONE to true
      for each arr[i] from 0 to count-2
        if arr[i] > arr[i+1]
          swap arr[i] and arr[i+1]
          set DONE to false
    while DONE is still false

              >v              > :12p :: 2+1g\3+1g  v
>122p0>:11g1--|> ::2+1g\3+1g `|  v                 <
^     ^       v               v  > 12g2+1p 12g3+1p v
^     ^<<<<<<<#<<<<<<< +1 <<<<<    p220            <
^             v
|  !g22       <   Is DONE true yet?

  If we've gotten to this point, DONE is true.
  Therefore the array is sorted.
  Now we simply print all 'count' elements of
  the array.
   v+1<<<<<<<<<<<<<<<<<
   v     >v           ^
> 0>:11g-|>:2+1g. " ",^
         v
         >52*,@ Done!

Contributed by Arthur J. O`Dwyer at andrew.cmu.edu