interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / merge-sort.wiki

137 lines · 2556 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text data "38 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8"
10global vals long 16
11global tmp long 16
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
39fn emiti n long to void
40if lt n 0
41emit minus
42emitu add bitcast ulong not n 1
43else
44emitu ulong n
45end
46end
47note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
48note returns how many; one pass over s, so the loop is bounded by len s
49fn parse s view uchar * to ulong
50n = ulong 0
51v = long 0
52sg = long 1
53dig = false
54for i ulong len s
55c = get s i
56if eq c 45
57sg = long -1
58else
59if and ge c 48 le c 57
60v = add mul v 10 long sub c 48
61dig = true
62else
63if dig
64put vals n mul sg v
65n = add n 1
66end
67v = long 0
68sg = long 1
69dig = false
70end
71end
72end
73if dig
74put vals n mul sg v
75n = add n 1
76end
77ret n
78end
79note show writes vals[0..n-1] separated by spaces, then a newline
80fn show n ulong to void
81for i ulong n
82if gt i 0
83emit sp
84end
85emiti get vals i
86end
87emit nl
88end
89note msort sorts vals[lo..hi-1]: sort both halves, merge them into tmp, copy back; the merge writes
90note one element per pass, so hi-lo <= 16 passes; the halves are strictly smaller, so recursion ends
91fn msort lo ulong hi ulong to void
92if le sub hi lo 1
93ret
94end
95m = add lo shr sub hi lo 1
96msort lo m
97msort m hi
98i = lo
99j = m
100k = lo
101while lt k hi bound 16
102left = false
103if ge j hi
104left = true
105else
106if lt i m
107if le get vals i get vals j
108left = true
109end
110end
111end
112if left
113put tmp k get vals i
114i = add i 1
115else
116put tmp k get vals j
117j = add j 1
118end
119k = add k 1
120end
121for x ulong sub hi lo
122put vals add lo x get tmp add lo x
123end
124end
125fn main to int
126n = parse data
127show n
128msort 0 n
129show n
130ret 0
131end
132</syntaxhighlight>
133{{out}}
134<pre>
13538 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8
136-19 -5 0 3 3 8 9 10 14 27 38 43 50 61 77 82
137</pre>