omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / heapsort.wiki
| 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 (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text minus "-" |
| 9 | text data "12 -4 7 88 0 31 -4 56 23 9 -70 45 2" |
| 10 | global vals long 16 |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow |
| 38 | fn emiti n long to void |
| 39 | if lt n 0 |
| 40 | emit minus |
| 41 | emitu add bitcast ulong not n 1 |
| 42 | else |
| 43 | emitu ulong n |
| 44 | end |
| 45 | end |
| 46 | note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and |
| 47 | note returns how many; one pass over s, so the loop is bounded by len s |
| 48 | fn parse s view uchar * to ulong |
| 49 | n = ulong 0 |
| 50 | v = long 0 |
| 51 | sg = long 1 |
| 52 | dig = false |
| 53 | for i ulong len s |
| 54 | c = get s i |
| 55 | if eq c 45 |
| 56 | sg = long -1 |
| 57 | else |
| 58 | if and ge c 48 le c 57 |
| 59 | v = add mul v 10 long sub c 48 |
| 60 | dig = true |
| 61 | else |
| 62 | if dig |
| 63 | put vals n mul sg v |
| 64 | n = add n 1 |
| 65 | end |
| 66 | v = long 0 |
| 67 | sg = long 1 |
| 68 | dig = false |
| 69 | end |
| 70 | end |
| 71 | end |
| 72 | if dig |
| 73 | put vals n mul sg v |
| 74 | n = add n 1 |
| 75 | end |
| 76 | ret n |
| 77 | end |
| 78 | note show writes vals[0..n-1] separated by spaces, then a newline |
| 79 | fn show n ulong to void |
| 80 | for i ulong n |
| 81 | if gt i 0 |
| 82 | emit sp |
| 83 | end |
| 84 | emiti get vals i |
| 85 | end |
| 86 | emit nl |
| 87 | end |
| 88 | note sift moves vals[s] down the max-heap vals[0..n-1]; r at least doubles each pass, so 16 passes suffice |
| 89 | fn sift s ulong n ulong to void |
| 90 | r = s |
| 91 | while lt add mul r 2 1 n bound 16 |
| 92 | c = add mul r 2 1 |
| 93 | if lt add c 1 n |
| 94 | if lt get vals c get vals add c 1 |
| 95 | c = add c 1 |
| 96 | end |
| 97 | end |
| 98 | if ge get vals r get vals c |
| 99 | ret |
| 100 | end |
| 101 | t = get vals r |
| 102 | put vals r get vals c |
| 103 | put vals c t |
| 104 | r = c |
| 105 | end |
| 106 | end |
| 107 | note hsort heapifies bottom-up, then swaps the maximum to the end n-1 times |
| 108 | fn hsort n ulong to void |
| 109 | s = shr n 1 |
| 110 | while gt s 0 bound 16 |
| 111 | s = sub s 1 |
| 112 | sift s n |
| 113 | end |
| 114 | e = n |
| 115 | while gt e 1 bound 16 |
| 116 | e = sub e 1 |
| 117 | t = get vals 0 |
| 118 | put vals 0 get vals e |
| 119 | put vals e t |
| 120 | sift 0 e |
| 121 | end |
| 122 | end |
| 123 | fn main to int |
| 124 | n = parse data |
| 125 | show n |
| 126 | hsort n |
| 127 | show n |
| 128 | ret 0 |
| 129 | end |
| 130 | </syntaxhighlight> |
| 131 | {{out}} |
| 132 | <pre> |
| 133 | 12 -4 7 88 0 31 -4 56 23 9 -70 45 2 |
| 134 | -70 -4 -4 0 2 7 9 12 23 31 45 56 88 |
| 135 | </pre> |