interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

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

83 lines · 1385 bytes Raw Blame History
1note Rosetta Code task "Sorting algorithms/Bubble sort": https://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7note emit writes every byte of a to stdout; a short write is retried, an error exits 1
8fn emit a view uchar * to void
9o = ulong 0
10while lt o len a bound 4096
11k = write 1 a o sub len a o
12if le k 0
13exit 1
14end
15o = add o ulong k
16end
17end
18note emitu writes n in decimal; 20 digits hold every ulong
19fn emitu n ulong to void
20d = new uchar 20 0
21i = ulong 20
22v = n
23while true bound 20
24i = sub i 1
25put d i uchar add rem v 10 48
26v = div v 10
27if eq v 0
28break
29end
30end
31emit part d i sub 20 i
32end
33note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
34fn emiti n long to void
35if lt n 0
36emit minus
37emitu add bitcast ulong not n 1
38else
39emitu ulong n
40end
41end
42fn bsort a edit long 10 to void
43for p ulong 9
44swapped = false
45for i ulong 9
46if gt get a i get a add i 1
47t = get a i
48put a i get a add i 1
49put a add i 1 t
50swapped = true
51end
52end
53if not swapped
54break
55end
56end
57end
58fn show a view long 10 to void
59for i ulong 10
60if gt i 0
61emit sp
62end
63emiti get a i
64end
65emit nl
66end
67fn main to int
68a = new long 10 0
69put a 0 5
70put a 1 -3
71put a 2 9
72put a 3 0
73put a 4 12
74put a 5 -8
75put a 6 7
76put a 7 7
77put a 8 1
78put a 9 100
79show a
80bsort a
81show a
82ret 0
83end