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/merge-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 / merge-sort.wiki` 137 lines ยท 2556 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 "38 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8" 10 global vals long 16 11 global tmp 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 msort sorts vals[lo..hi-1]: sort both halves, merge them into tmp, copy back; the merge writes 90 note one element per pass, so hi-lo <= 16 passes; the halves are strictly smaller, so recursion ends 91 fn msort lo ulong hi ulong to void 92 if le sub hi lo 1 93 ret 94 end 95 m = add lo shr sub hi lo 1 96 msort lo m 97 msort m hi 98 i = lo 99 j = m 100 k = lo 101 while lt k hi bound 16 102 left = false 103 if ge j hi 104 left = true 105 else 106 if lt i m 107 if le get vals i get vals j 108 left = true 109 end 110 end 111 end 112 if left 113 put tmp k get vals i 114 i = add i 1 115 else 116 put tmp k get vals j 117 j = add j 1 118 end 119 k = add k 1 120 end 121 for x ulong sub hi lo 122 put vals add lo x get tmp add lo x 123 end 124 end 125 fn main to int 126 n = parse data 127 show n 128 msort 0 n 129 show n 130 ret 0 131 end 132 133 {{out}} 134

135 38 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8

136 -19 -5 0 3 3 8 9 10 14 27 38 43 50 61 77 82

137