interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / cycle-sort / cycle-sort.om

136 lines · 2583 bytes Raw Blame History
1note Rosetta Code task "Sorting algorithms/Cycle sort": https://rosettacode.org/wiki/Sorting_algorithms/Cycle_sort
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text data "5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1"
8text twr "writes: "
9global vals long 16
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
37fn emiti n long to void
38if lt n 0
39emit minus
40emitu add bitcast ulong not n 1
41else
42emitu ulong n
43end
44end
45note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
46note returns how many; one pass over s, so the loop is bounded by len s
47fn parse s view uchar * to ulong
48n = ulong 0
49v = long 0
50sg = long 1
51dig = false
52for i ulong len s
53c = get s i
54if eq c 45
55sg = long -1
56else
57if and ge c 48 le c 57
58v = add mul v 10 long sub c 48
59dig = true
60else
61if dig
62put vals n mul sg v
63n = add n 1
64end
65v = long 0
66sg = long 1
67dig = false
68end
69end
70end
71if dig
72put vals n mul sg v
73n = add n 1
74end
75ret n
76end
77note show writes vals[0..n-1] separated by spaces, then a newline
78fn show n ulong to void
79for i ulong n
80if gt i 0
81emit sp
82end
83emiti get vals i
84end
85emit nl
86end
87note place counts the elements after vals[c] that are smaller than item: that is item's sorted position
88fn place c ulong n ulong item long to ulong
89p = c
90for x ulong sub sub n c 1
91if lt get vals add add c x 1 item
92p = add p 1
93end
94end
95ret p
96end
97note cysort rotates each cycle into place, writing every misplaced element exactly once, and returns
98note the number of writes; each rotation pass writes one element, so n passes bound one cycle
99fn cysort n ulong to ulong
100w = ulong 0
101for c ulong sub n 1
102item = get vals c
103p = place c n item
104if eq p c
105continue
106end
107while eq item get vals p bound 16
108p = add p 1
109end
110t = get vals p
111put vals p item
112item = t
113w = add w 1
114while ne p c bound 16
115p = place c n item
116while eq item get vals p bound 16
117p = add p 1
118end
119t = get vals p
120put vals p item
121item = t
122w = add w 1
123end
124end
125ret w
126end
127fn main to int
128n = parse data
129show n
130w = cysort n
131show n
132emit twr
133emitu w
134emit nl
135ret 0
136end