http://t3x.org/t3x/0/exam-sort.html

T3X

T3X/0

Example Programs

T3X/0 homepage
Examples: bubblesort | mandelbrot | ntoa | strings

Sort a vector using Bubblesort, then print it.

use t3x: t;
use string: str;
use io;

bubblesort(n, v, cmp) do var i, swapped, tmp;
    swapped := %1;
    while (swapped) do
        swapped := 0;
        for (i=0, n-1) do
            if (\call cmp(v[i], v[i+1])) do
                tmp := v[i];
                v[i] := v[i+1];
                v[i+1] := tmp;
                swapped := %1;
            end
        end
    end
end

lesseq(a, b) return a <= b;

do var v;
    v := [ 5, 8, 2, 7, 3, 0, 6, 9, 4, 1 ];
    bubblesort(10, v, @lesseq);
    for (i=0, 10)
        io.writeln(str.ntoa(v[i], 10));
end

contact  |  privacy