diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-24 15:25:18 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-24 15:25:18 -0700 |
| commit | 6ff5bb750b8d571a48204539b90c52c1e17283d5 (patch) | |
| tree | 48efe646e77dd8f72d17302560116849b50edef7 /chapter26/docs/examples | |
| parent | 8532f4bdd93dedfeb67817db34ac1a63e46b21b5 (diff) | |
| download | Simple-6ff5bb750b8d571a48204539b90c52c1e17283d5.tar.gz Simple-6ff5bb750b8d571a48204539b90c52c1e17283d5.zip | |
Rename construction chapter to 26
Diffstat (limited to 'chapter26/docs/examples')
| -rw-r--r-- | chapter26/docs/examples/A_helloWorld.smp | 3 | ||||
| -rw-r--r-- | chapter26/docs/examples/BubbleSort.smp | 37 |
2 files changed, 40 insertions, 0 deletions
diff --git a/chapter26/docs/examples/A_helloWorld.smp b/chapter26/docs/examples/A_helloWorld.smp new file mode 100644 index 00000000..07ff935f --- /dev/null +++ b/chapter26/docs/examples/A_helloWorld.smp @@ -0,0 +1,3 @@ +// -*- mode: java; -*- +sys.io.p("Hello, World!"); +return 0; diff --git a/chapter26/docs/examples/BubbleSort.smp b/chapter26/docs/examples/BubbleSort.smp new file mode 100644 index 00000000..938e3181 --- /dev/null +++ b/chapter26/docs/examples/BubbleSort.smp @@ -0,0 +1,37 @@ +// -*- mode: java; -*- + +// An implementation of +// https://sampleprograms.io/projects/bubble-sort/ +// +// Bubble sort, with reading (parsing) and writing (formatting) the data. + +val sort = { !sys.aryi64 nums -> + var done = false; + while( !done ) { + done = true; + for( var i = 0; i < nums.len-1; i++ ) + if( nums.buf[i] > nums.buf[i+1] ) { + int tmp = nums.buf[i]; + nums.buf[i] = nums.buf[i+1]; + nums.buf[i+1] = tmp; // Swap + done = false; + } + } +}; + +// Read from stdin a line of the form "[ 4, 3, 2, 1]" and print it sorted. +val main = { -> + // Parse array of numbers from stdin + var nums = sys.parseAryI64( sys.io.stdin() ); + if( !nums ) { + sys.io.p("stdin must look like [4, 3, 2, 1]"); + return 1; + } + + // Bubble sort array of integers + sort(nums); + + // Print a list of numbers to stdout + nums.str().write(1); + return 0; +}; |
