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/radix-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 / radix-sort.wiki`
152 lines ยท 2904 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 "170 45 75 -90 -802 24 2 66 0 1999 -1 333"
10 global vals long 16
11 global key ulong 16
12 global tk ulong 16
13 global bk ulong 10
14 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15 fn emit a view uchar * to void
16 o = ulong 0
17 while lt o len a bound 4096
18 k = write 1 a o sub len a o
19 if le k 0
20 exit 1
21 end
22 o = add o ulong k
23 end
24 end
25 note emitu writes n in decimal; 20 digits hold every ulong
26 fn emitu n ulong to void
27 d = new uchar 20 0
28 i = ulong 20
29 v = n
30 while true bound 20
31 i = sub i 1
32 put d i uchar add rem v 10 48
33 v = div v 10
34 if eq v 0
35 break
36 end
37 end
38 emit part d i sub 20 i
39 end
40 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
41 fn emiti n long to void
42 if lt n 0
43 emit minus
44 emitu add bitcast ulong not n 1
45 else
46 emitu ulong n
47 end
48 end
49 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
50 note returns how many; one pass over s, so the loop is bounded by len s
51 fn parse s view uchar * to ulong
52 n = ulong 0
53 v = long 0
54 sg = long 1
55 dig = false
56 for i ulong len s
57 c = get s i
58 if eq c 45
59 sg = long -1
60 else
61 if and ge c 48 le c 57
62 v = add mul v 10 long sub c 48
63 dig = true
64 else
65 if dig
66 put vals n mul sg v
67 n = add n 1
68 end
69 v = long 0
70 sg = long 1
71 dig = false
72 end
73 end
74 end
75 if dig
76 put vals n mul sg v
77 n = add n 1
78 end
79 ret n
80 end
81 note show writes vals[0..n-1] separated by spaces, then a newline
82 fn show n ulong to void
83 for i ulong n
84 if gt i 0
85 emit sp
86 end
87 emiti get vals i
88 end
89 emit nl
90 end
91 note rxsort shifts every value by the minimum so the keys are unsigned, then sorts the keys with a
92 note stable least-significant-digit-first counting pass per decimal digit; 20 passes cover any ulong
93 fn rxsort n ulong to void
94 lo = get vals 0
95 for i ulong n
96 if lt get vals i lo
97 lo = get vals i
98 end
99 end
100 mx = ulong 0
101 for i ulong n
102 put key i ulong sub get vals i lo
103 if gt get key i mx
104 mx = get key i
105 end
106 end
107 e = ulong 1
108 for p ulong 20
109 for b ulong 10
110 put bk b 0
111 end
112 for i ulong n
113 dg = rem div get key i e 10
114 put bk dg add get bk dg 1
115 end
116 s = ulong 0
117 for b ulong 10
118 c = get bk b
119 put bk b s
120 s = add s c
121 end
122 for i ulong n
123 dg = rem div get key i e 10
124 put tk get bk dg get key i
125 put bk dg add get bk dg 1
126 end
127 for i ulong n
128 put key i get tk i
129 end
130 note stop when no key has a digit at 10*e; e > mx/10 means 10*e > mx, and 10*e is never formed past that
131 if gt e div mx 10
132 break
133 end
134 e = mul e 10
135 end
136 for i ulong n
137 put vals i add lo long get key i
138 end
139 end
140 fn main to int
141 n = parse data
142 show n
143 rxsort n
144 show n
145 ret 0
146 end
147
148 {{out}}
149
150 170 45 75 -90 -802 24 2 66 0 1999 -1 333
151 -802 -90 -1 0 2 24 45 66 75 170 333 1999
152