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/permutation-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 / permutation-sort.wiki` 165 lines ยท 2996 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 "8 -2 15 3 0 6" 10 text ttried "permutations tried: " 11 global vals long 8 12 global perm ulong 8 13 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 14 fn emit a view uchar * to void 15 o = ulong 0 16 while lt o len a bound 4096 17 k = write 1 a o sub len a o 18 if le k 0 19 exit 1 20 end 21 o = add o ulong k 22 end 23 end 24 note emitu writes n in decimal; 20 digits hold every ulong 25 fn emitu n ulong to void 26 d = new uchar 20 0 27 i = ulong 20 28 v = n 29 while true bound 20 30 i = sub i 1 31 put d i uchar add rem v 10 48 32 v = div v 10 33 if eq v 0 34 break 35 end 36 end 37 emit part d i sub 20 i 38 end 39 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 40 fn emiti n long to void 41 if lt n 0 42 emit minus 43 emitu add bitcast ulong not n 1 44 else 45 emitu ulong n 46 end 47 end 48 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 49 note returns how many; one pass over s, so the loop is bounded by len s 50 fn parse s view uchar * to ulong 51 n = ulong 0 52 v = long 0 53 sg = long 1 54 dig = false 55 for i ulong len s 56 c = get s i 57 if eq c 45 58 sg = long -1 59 else 60 if and ge c 48 le c 57 61 v = add mul v 10 long sub c 48 62 dig = true 63 else 64 if dig 65 put vals n mul sg v 66 n = add n 1 67 end 68 v = long 0 69 sg = long 1 70 dig = false 71 end 72 end 73 end 74 if dig 75 put vals n mul sg v 76 n = add n 1 77 end 78 ret n 79 end 80 note show writes vals[0..n-1] separated by spaces, then a newline 81 fn show n ulong to void 82 for i ulong n 83 if gt i 0 84 emit sp 85 end 86 emiti get vals i 87 end 88 emit nl 89 end 90 note sorted says whether vals taken in the order perm[0..n-1] is non-decreasing 91 fn sorted n ulong to bool 92 for i ulong sub n 1 93 if gt get vals get perm i get vals get perm add i 1 94 ret false 95 end 96 end 97 ret true 98 end 99 note nextp steps perm to the next permutation in lexicographic order; false after the last one 100 fn nextp n ulong to bool 101 i = sub n 1 102 while gt i 0 bound 8 103 if lt get perm sub i 1 get perm i 104 break 105 end 106 i = sub i 1 107 end 108 if eq i 0 109 ret false 110 end 111 j = sub n 1 112 while le get perm j get perm sub i 1 bound 8 113 j = sub j 1 114 end 115 t = get perm sub i 1 116 put perm sub i 1 get perm j 117 put perm j t 118 k = sub n 1 119 while lt i k bound 8 120 t2 = get perm i 121 put perm i get perm k 122 put perm k t2 123 i = add i 1 124 k = sub k 1 125 end 126 ret true 127 end 128 note pmsort tries the index permutations in lexicographic order (at most 6! = 720) until one is sorted 129 fn pmsort n ulong to ulong 130 for i ulong n 131 put perm i i 132 end 133 c = ulong 1 134 while not sorted n bound 720 135 if not nextp n 136 trap 9 137 end 138 c = add c 1 139 end 140 t = new long 8 0 141 for i ulong n 142 put t i get vals get perm i 143 end 144 for i ulong n 145 put vals i get t i 146 end 147 ret c 148 end 149 fn main to int 150 n = parse data 151 show n 152 c = pmsort n 153 show n 154 emit ttried 155 emitu c 156 emit nl 157 ret 0 158 end 159 160 {{out}} 161

162 8 -2 15 3 0 6

163 -2 0 3 6 8 15

164 permutations tried: 209

165