interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / cocktail-sort-with-shifting-bounds / cocktail-sort-with-shifting-bounds.om

125 lines · 2447 bytes Raw Blame History
1note Rosetta Code task "Sorting algorithms/Cocktail sort with shifting bounds": https://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort_with_shifting_bounds
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text data "21 -4 7 7 0 88 -15 3 42 9 1 -4 60"
8global vals long 16
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitu writes n in decimal; 20 digits hold every ulong
21fn emitu n ulong to void
22d = new uchar 20 0
23i = ulong 20
24v = n
25while true bound 20
26i = sub i 1
27put d i uchar add rem v 10 48
28v = div v 10
29if eq v 0
30break
31end
32end
33emit part d i sub 20 i
34end
35note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
36fn emiti n long to void
37if lt n 0
38emit minus
39emitu add bitcast ulong not n 1
40else
41emitu ulong n
42end
43end
44note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
45note returns how many; one pass over s, so the loop is bounded by len s
46fn parse s view uchar * to ulong
47n = ulong 0
48v = long 0
49sg = long 1
50dig = false
51for i ulong len s
52c = get s i
53if eq c 45
54sg = long -1
55else
56if and ge c 48 le c 57
57v = add mul v 10 long sub c 48
58dig = true
59else
60if dig
61put vals n mul sg v
62n = add n 1
63end
64v = long 0
65sg = long 1
66dig = false
67end
68end
69end
70if dig
71put vals n mul sg v
72n = add n 1
73end
74ret n
75end
76note show writes vals[0..n-1] separated by spaces, then a newline
77fn show n ulong to void
78for i ulong n
79if gt i 0
80emit sp
81end
82emiti get vals i
83end
84emit nl
85end
86note cssort runs forward and backward passes over vals[b..e]; after a forward pass everything past the
87note last swap is in place, so e moves to it, and b likewise after a backward pass; each round moves
88note e down or b up by at least one, so n rounds (at most 16) suffice
89fn cssort n ulong to void
90b = ulong 0
91e = sub n 1
92while lt b e bound 16
93ls = b
94i = b
95while lt i e bound 16
96if gt get vals i get vals add i 1
97t = get vals i
98put vals i get vals add i 1
99put vals add i 1 t
100ls = i
101end
102i = add i 1
103end
104e = ls
105fs = e
106i = e
107while gt i b bound 16
108if gt get vals sub i 1 get vals i
109t = get vals i
110put vals i get vals sub i 1
111put vals sub i 1 t
112fs = i
113end
114i = sub i 1
115end
116b = fs
117end
118end
119fn main to int
120n = parse data
121show n
122cssort n
123show n
124ret 0
125end