From b4eb623979cc9566d2a22c718b4ba8ded3bca5a4 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sat, 10 May 2025 10:10:24 -0700 Subject: clean up tests BubbleSort is complete but ugly risc5 encoding <= test EvalR5/Arm handles stdout/stderr. --- chapter22/docs/examples/BubbleSort.smp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'chapter22/docs') diff --git a/chapter22/docs/examples/BubbleSort.smp b/chapter22/docs/examples/BubbleSort.smp index f6b30752..a80d6bd8 100644 --- a/chapter22/docs/examples/BubbleSort.smp +++ b/chapter22/docs/examples/BubbleSort.smp @@ -1,5 +1,10 @@ // -*- mode: java; -*- +// An implementation of +// https://sampleprograms.io/projects/bubble-sort/ +// +// Bubble sort, with reading (parsing) and writing (formatting) the data. + val _exit = { u8[~] msg -> sys.io.p(msg); sys.libc._exit(1); @@ -40,6 +45,19 @@ val number = { Scan s -> return n; }; +val sort = { int[] ns, int len -> + var done = false; + while( !done ) { + done = true; + for( var i = 0; i < len-1; i++ ) + if( ns[i] > ns[i+1] ) { + int tmp = ns[i]; + ns[i] = ns[i+1]; + ns[i+1] = tmp; // Swap + done = false; + } + } +}; // Read from stdin a line of the form "[ 4, 3, 2, 1]" and print it sorted. val main = { -> @@ -47,23 +65,26 @@ val main = { -> require(s,'['); // Array of parsed results int[] !ns = new int[16]; - i32 num = 0; + i32 len = 0; // Parse a list of numbers if( !peek(s,']') ) { // Handle the empty list // Parse "123, " until a something else while( s.x < s.buf# ) { int n = number(s); - ns[num++] = n; + ns[len++] = n; if( !peek(s,',') ) break; }; require(s,']'); // Close the list } + // Bubble sort array of integers + sort(ns,len); + // Print a list of numbers sys.io.p("[ "); u8[] !junk = new u8[1]; - for( int i=0; i sys.io.p(", "); } sys.io.p("]"); - - return num; + return 0; }; -- cgit v1.2.3