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/pancake-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 / pancake-sort.wiki`
136 lines ยท 2503 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 "23 -7 61 4 0 38 15 -7 90 2 11"
10 text tflips "flips: "
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 flip reverses vals[0..k-1]; k/2 swaps
90 fn flip k ulong to void
91 for i ulong shr k 1
92 t = get vals i
93 put vals i get vals sub sub k i 1
94 put vals sub sub k i 1 t
95 end
96 end
97 note pksort brings the largest of vals[0..s-1] to the front, then flips it to position s-1, for s = n .. 2;
98 note it returns the number of flips made, at most 2*(n-1)
99 fn pksort n ulong to ulong
100 f = ulong 0
101 for x ulong sub n 1
102 s = sub n x
103 m = ulong 0
104 for i ulong s
105 if gt get vals i get vals m
106 m = i
107 end
108 end
109 if ne m sub s 1
110 if ne m 0
111 flip add m 1
112 f = add f 1
113 end
114 flip s
115 f = add f 1
116 end
117 end
118 ret f
119 end
120 fn main to int
121 n = parse data
122 show n
123 f = pksort n
124 show n
125 emit tflips
126 emitu f
127 emit nl
128 ret 0
129 end
130
131 {{out}}
132
133 23 -7 61 4 0 38 15 -7 90 2 11
134 -7 -7 0 2 4 11 15 23 38 61 90
135 flips: 16
136