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

141 lines · 2868 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 "3 -1 4 1 5 9 2"
10text tshuf "shuffles: "
11global vals long 8
12global seed ulong 1
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note emitu writes n in decimal; 20 digits hold every ulong
25fn emitu n ulong to void
26d = new uchar 20 0
27i = ulong 20
28v = n
29while true bound 20
30i = sub i 1
31put d i uchar add rem v 10 48
32v = div v 10
33if eq v 0
34break
35end
36end
37emit part d i sub 20 i
38end
39note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
40fn emiti n long to void
41if lt n 0
42emit minus
43emitu add bitcast ulong not n 1
44else
45emitu ulong n
46end
47end
48note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
49note returns how many; one pass over s, so the loop is bounded by len s
50fn parse s view uchar * to ulong
51n = ulong 0
52v = long 0
53sg = long 1
54dig = false
55for i ulong len s
56c = get s i
57if eq c 45
58sg = long -1
59else
60if and ge c 48 le c 57
61v = add mul v 10 long sub c 48
62dig = true
63else
64if dig
65put vals n mul sg v
66n = add n 1
67end
68v = long 0
69sg = long 1
70dig = false
71end
72end
73end
74if dig
75put vals n mul sg v
76n = add n 1
77end
78ret n
79end
80note show writes vals[0..n-1] separated by spaces, then a newline
81fn show n ulong to void
82for i ulong n
83if gt i 0
84emit sp
85end
86emiti get vals i
87end
88emit nl
89end
90note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407
91note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33
92fn rnd to ulong
93x = add mul get seed 0 6364136223846793005 1442695040888963407
94put seed 0 x
95ret shr x 33
96end
97note shuffle is Fisher-Yates: for i = n-1 .. 1 swap vals[i] with vals[rnd mod (i+1)]
98fn shuffle n ulong to void
99for x ulong sub n 1
100i = sub sub n x 1
101j = rem rnd add i 1
102t = get vals i
103put vals i get vals j
104put vals j t
105end
106end
107fn inorder n ulong to bool
108for i ulong sub n 1
109if gt get vals i get vals add i 1
110ret false
111end
112end
113ret true
114end
115note bgsort shuffles until sorted; with this generator and input it takes the count printed, well under
116note the bound of 200000 (7! = 5040 orders, so a working generator lands far inside it)
117fn bgsort n ulong to ulong
118c = ulong 0
119while not inorder n bound 200000
120shuffle n
121c = add c 1
122end
123ret c
124end
125fn main to int
126n = parse data
127show n
128c = bgsort n
129show n
130emit tshuf
131emitu c
132emit nl
133ret 0
134end
135</syntaxhighlight>
136{{out}}
137<pre>
1383 -1 4 1 5 9 2
139-1 1 2 3 4 5 9
140shuffles: 2314
141</pre>