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 / heapsort.wiki

135 lines · 2524 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 "12 -4 7 88 0 31 -4 56 23 9 -70 45 2"
10global vals long 16
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
38fn emiti n long to void
39if lt n 0
40emit minus
41emitu add bitcast ulong not n 1
42else
43emitu ulong n
44end
45end
46note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
47note returns how many; one pass over s, so the loop is bounded by len s
48fn parse s view uchar * to ulong
49n = ulong 0
50v = long 0
51sg = long 1
52dig = false
53for i ulong len s
54c = get s i
55if eq c 45
56sg = long -1
57else
58if and ge c 48 le c 57
59v = add mul v 10 long sub c 48
60dig = true
61else
62if dig
63put vals n mul sg v
64n = add n 1
65end
66v = long 0
67sg = long 1
68dig = false
69end
70end
71end
72if dig
73put vals n mul sg v
74n = add n 1
75end
76ret n
77end
78note show writes vals[0..n-1] separated by spaces, then a newline
79fn show n ulong to void
80for i ulong n
81if gt i 0
82emit sp
83end
84emiti get vals i
85end
86emit nl
87end
88note sift moves vals[s] down the max-heap vals[0..n-1]; r at least doubles each pass, so 16 passes suffice
89fn sift s ulong n ulong to void
90r = s
91while lt add mul r 2 1 n bound 16
92c = add mul r 2 1
93if lt add c 1 n
94if lt get vals c get vals add c 1
95c = add c 1
96end
97end
98if ge get vals r get vals c
99ret
100end
101t = get vals r
102put vals r get vals c
103put vals c t
104r = c
105end
106end
107note hsort heapifies bottom-up, then swaps the maximum to the end n-1 times
108fn hsort n ulong to void
109s = shr n 1
110while gt s 0 bound 16
111s = sub s 1
112sift s n
113end
114e = n
115while gt e 1 bound 16
116e = sub e 1
117t = get vals 0
118put vals 0 get vals e
119put vals e t
120sift 0 e
121end
122end
123fn main to int
124n = parse data
125show n
126hsort n
127show n
128ret 0
129end
130</syntaxhighlight>
131{{out}}
132<pre>
13312 -4 7 88 0 31 -4 56 23 9 -70 45 2
134-70 -4 -4 0 2 7 9 12 23 31 45 56 88
135</pre>