Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/cycle-sort.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
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}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text data "5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1"
10 text twr "writes: "
11 global vals long 16
12 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13 fn emit a view uchar * to void
14 o = ulong 0
15 while lt o len a bound 4096
16 k = write 1 a o sub len a o
17 if le k 0
18 exit 1
19 end
20 o = add o ulong k
21 end
22 end
23 note emitu writes n in decimal; 20 digits hold every ulong
24 fn emitu n ulong to void
25 d = new uchar 20 0
26 i = ulong 20
27 v = n
28 while true bound 20
29 i = sub i 1
30 put d i uchar add rem v 10 48
31 v = div v 10
32 if eq v 0
33 break
34 end
35 end
36 emit part d i sub 20 i
37 end
38 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
39 fn emiti n long to void
40 if lt n 0
41 emit minus
42 emitu add bitcast ulong not n 1
43 else
44 emitu ulong n
45 end
46 end
47 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
48 note returns how many; one pass over s, so the loop is bounded by len s
49 fn parse s view uchar * to ulong
50 n = ulong 0
51 v = long 0
52 sg = long 1
53 dig = false
54 for i ulong len s
55 c = get s i
56 if eq c 45
57 sg = long -1
58 else
59 if and ge c 48 le c 57
60 v = add mul v 10 long sub c 48
61 dig = true
62 else
63 if dig
64 put vals n mul sg v
65 n = add n 1
66 end
67 v = long 0
68 sg = long 1
69 dig = false
70 end
71 end
72 end
73 if dig
74 put vals n mul sg v
75 n = add n 1
76 end
77 ret n
78 end
79 note show writes vals[0..n-1] separated by spaces, then a newline
80 fn show n ulong to void
81 for i ulong n
82 if gt i 0
83 emit sp
84 end
85 emiti get vals i
86 end
87 emit nl
88 end
89 note place counts the elements after vals[c] that are smaller than item: that is item's sorted position
90 fn place c ulong n ulong item long to ulong
91 p = c
92 for x ulong sub sub n c 1
93 if lt get vals add add c x 1 item
94 p = add p 1
95 end
96 end
97 ret p
98 end
99 note cysort rotates each cycle into place, writing every misplaced element exactly once, and returns
100 note the number of writes; each rotation pass writes one element, so n passes bound one cycle
101 fn cysort n ulong to ulong
102 w = ulong 0
103 for c ulong sub n 1
104 item = get vals c
105 p = place c n item
106 if eq p c
107 continue
108 end
109 while eq item get vals p bound 16
110 p = add p 1
111 end
112 t = get vals p
113 put vals p item
114 item = t
115 w = add w 1
116 while ne p c bound 16
117 p = place c n item
118 while eq item get vals p bound 16
119 p = add p 1
120 end
121 t = get vals p
122 put vals p item
123 item = t
124 w = add w 1
125 end
126 end
127 ret w
128 end
129 fn main to int
130 n = parse data
131 show n
132 w = cysort n
133 show n
134 emit twr
135 emitu w
136 emit nl
137 ret 0
138 end
139
140 {{out}}
141
142 5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1
143 0 0 0 1 1 1 1 2 2 3 5 5 5 6 8 9
144 writes: 14
145