Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/bead-sort.wiki # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / drafts / rosettacode / bead-sort.wiki` 129 lines ยท 2553 bytes Raw Blame History 1 =={{header|Omegese}}== 2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}} 3 Omega has no imports, so the small output helpers (emit and friends) are part of the program. 4 5 om2 6 text nl "\n" 7 text sp " " 8 text minus "-" 9 text data "5 3 1 7 4 1 1 20 0 9" 10 global vals long 16 11 global grid uchar 512 12 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 13 fn emit a view uchar * to void 14 o = ulong 0 15 while lt o len a bound 4096 16 k = write 1 a o sub len a o 17 if le k 0 18 exit 1 19 end 20 o = add o ulong k 21 end 22 end 23 note emitu writes n in decimal; 20 digits hold every ulong 24 fn emitu n ulong to void 25 d = new uchar 20 0 26 i = ulong 20 27 v = n 28 while true bound 20 29 i = sub i 1 30 put d i uchar add rem v 10 48 31 v = div v 10 32 if eq v 0 33 break 34 end 35 end 36 emit part d i sub 20 i 37 end 38 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 39 fn emiti n long to void 40 if lt n 0 41 emit minus 42 emitu add bitcast ulong not n 1 43 else 44 emitu ulong n 45 end 46 end 47 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 48 note returns how many; one pass over s, so the loop is bounded by len s 49 fn parse s view uchar * to ulong 50 n = ulong 0 51 v = long 0 52 sg = long 1 53 dig = false 54 for i ulong len s 55 c = get s i 56 if eq c 45 57 sg = long -1 58 else 59 if and ge c 48 le c 57 60 v = add mul v 10 long sub c 48 61 dig = true 62 else 63 if dig 64 put vals n mul sg v 65 n = add n 1 66 end 67 v = long 0 68 sg = long 1 69 dig = false 70 end 71 end 72 end 73 if dig 74 put vals n mul sg v 75 n = add n 1 76 end 77 ret n 78 end 79 note show writes vals[0..n-1] separated by spaces, then a newline 80 fn show n ulong to void 81 for i ulong n 82 if gt i 0 83 emit sp 84 end 85 emiti get vals i 86 end 87 emit nl 88 end 89 note the abacus has one row per value (at most 16) and one column per unit (at most 32): grid[r*32+c] 90 note holds a bead when c < vals[r]; the values here are 0 .. 20 91 fn bdsort n ulong to void 92 for r ulong n 93 for c ulong ulong get vals r 94 put grid add mul r 32 c 1 95 end 96 end 97 note gravity: each column's beads fall to the bottom rows 98 for c ulong 32 99 h = ulong 0 100 for r ulong n 101 h = add h ulong get grid add mul r 32 c 102 put grid add mul r 32 c 0 103 end 104 for x ulong h 105 put grid add mul sub sub n x 1 32 c 1 106 end 107 end 108 note row r now holds the r-th smallest value as its bead count, read top to bottom 109 for r ulong n 110 v = long 0 111 for c ulong 32 112 v = add v long get grid add mul r 32 c 113 end 114 put vals r v 115 end 116 end 117 fn main to int 118 n = parse data 119 show n 120 bdsort n 121 show n 122 ret 0 123 end 124 125 {{out}} 126

127 5 3 1 7 4 1 1 20 0 9

128 0 1 1 1 3 4 5 7 9 20

129