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 / pancake-sort.wiki

136 lines · 2503 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 "23 -7 61 4 0 38 15 -7 90 2 11"
10text tflips "flips: "
11global vals 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 flip reverses vals[0..k-1]; k/2 swaps
90fn flip k ulong to void
91for i ulong shr k 1
92t = get vals i
93put vals i get vals sub sub k i 1
94put vals sub sub k i 1 t
95end
96end
97note pksort brings the largest of vals[0..s-1] to the front, then flips it to position s-1, for s = n .. 2;
98note it returns the number of flips made, at most 2*(n-1)
99fn pksort n ulong to ulong
100f = ulong 0
101for x ulong sub n 1
102s = sub n x
103m = ulong 0
104for i ulong s
105if gt get vals i get vals m
106m = i
107end
108end
109if ne m sub s 1
110if ne m 0
111flip add m 1
112f = add f 1
113end
114flip s
115f = add f 1
116end
117end
118ret f
119end
120fn main to int
121n = parse data
122show n
123f = pksort n
124show n
125emit tflips
126emitu f
127emit nl
128ret 0
129end
130</syntaxhighlight>
131{{out}}
132<pre>
13323 -7 61 4 0 38 15 -7 90 2 11
134-7 -7 0 2 4 11 15 23 38 61 90
135flips: 16
136</pre>