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

145 lines · 2760 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 "5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1"
10text twr "writes: "
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 place counts the elements after vals[c] that are smaller than item: that is item's sorted position
90fn place c ulong n ulong item long to ulong
91p = c
92for x ulong sub sub n c 1
93if lt get vals add add c x 1 item
94p = add p 1
95end
96end
97ret p
98end
99note cysort rotates each cycle into place, writing every misplaced element exactly once, and returns
100note the number of writes; each rotation pass writes one element, so n passes bound one cycle
101fn cysort n ulong to ulong
102w = ulong 0
103for c ulong sub n 1
104item = get vals c
105p = place c n item
106if eq p c
107continue
108end
109while eq item get vals p bound 16
110p = add p 1
111end
112t = get vals p
113put vals p item
114item = t
115w = add w 1
116while ne p c bound 16
117p = place c n item
118while eq item get vals p bound 16
119p = add p 1
120end
121t = get vals p
122put vals p item
123item = t
124w = add w 1
125end
126end
127ret w
128end
129fn main to int
130n = parse data
131show n
132w = cysort n
133show n
134emit twr
135emitu w
136emit nl
137ret 0
138end
139</syntaxhighlight>
140{{out}}
141<pre>
1425 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1
1430 0 0 1 1 1 1 2 2 3 5 5 5 6 8 9
144writes: 14
145</pre>