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/tasks/permutation-sort/permutation-sort.om # 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 / tasks / permutation-sort / permutation-sort.om` 156 lines ยท 2854 bytes Raw Blame History 1 note Rosetta Code task "Sorting algorithms/Permutation sort": https://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text data "8 -2 15 3 0 6" 8 text ttried "permutations tried: " 9 global vals long 8 10 global perm ulong 8 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 sorted says whether vals taken in the order perm[0..n-1] is non-decreasing 89 fn sorted n ulong to bool 90 for i ulong sub n 1 91 if gt get vals get perm i get vals get perm add i 1 92 ret false 93 end 94 end 95 ret true 96 end 97 note nextp steps perm to the next permutation in lexicographic order; false after the last one 98 fn nextp n ulong to bool 99 i = sub n 1 100 while gt i 0 bound 8 101 if lt get perm sub i 1 get perm i 102 break 103 end 104 i = sub i 1 105 end 106 if eq i 0 107 ret false 108 end 109 j = sub n 1 110 while le get perm j get perm sub i 1 bound 8 111 j = sub j 1 112 end 113 t = get perm sub i 1 114 put perm sub i 1 get perm j 115 put perm j t 116 k = sub n 1 117 while lt i k bound 8 118 t2 = get perm i 119 put perm i get perm k 120 put perm k t2 121 i = add i 1 122 k = sub k 1 123 end 124 ret true 125 end 126 note pmsort tries the index permutations in lexicographic order (at most 6! = 720) until one is sorted 127 fn pmsort n ulong to ulong 128 for i ulong n 129 put perm i i 130 end 131 c = ulong 1 132 while not sorted n bound 720 133 if not nextp n 134 trap 9 135 end 136 c = add c 1 137 end 138 t = new long 8 0 139 for i ulong n 140 put t i get vals get perm i 141 end 142 for i ulong n 143 put vals i get t i 144 end 145 ret c 146 end 147 fn main to int 148 n = parse data 149 show n 150 c = pmsort n 151 show n 152 emit ttried 153 emitu c 154 emit nl 155 ret 0 156 end