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/pancake-sort/pancake-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 / pancake-sort / pancake-sort.om` 127 lines ยท 2335 bytes Raw Blame History 1 note Rosetta Code task "Sorting algorithms/Pancake sort": https://rosettacode.org/wiki/Sorting_algorithms/Pancake_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 "23 -7 61 4 0 38 15 -7 90 2 11" 8 text tflips "flips: " 9 global vals long 16 10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 11 fn emit a view uchar * to void 12 o = ulong 0 13 while lt o len a bound 4096 14 k = write 1 a o sub len a o 15 if le k 0 16 exit 1 17 end 18 o = add o ulong k 19 end 20 end 21 note emitu writes n in decimal; 20 digits hold every ulong 22 fn emitu n ulong to void 23 d = new uchar 20 0 24 i = ulong 20 25 v = n 26 while true bound 20 27 i = sub i 1 28 put d i uchar add rem v 10 48 29 v = div v 10 30 if eq v 0 31 break 32 end 33 end 34 emit part d i sub 20 i 35 end 36 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 37 fn emiti n long to void 38 if lt n 0 39 emit minus 40 emitu add bitcast ulong not n 1 41 else 42 emitu ulong n 43 end 44 end 45 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 46 note returns how many; one pass over s, so the loop is bounded by len s 47 fn parse s view uchar * to ulong 48 n = ulong 0 49 v = long 0 50 sg = long 1 51 dig = false 52 for i ulong len s 53 c = get s i 54 if eq c 45 55 sg = long -1 56 else 57 if and ge c 48 le c 57 58 v = add mul v 10 long sub c 48 59 dig = true 60 else 61 if dig 62 put vals n mul sg v 63 n = add n 1 64 end 65 v = long 0 66 sg = long 1 67 dig = false 68 end 69 end 70 end 71 if dig 72 put vals n mul sg v 73 n = add n 1 74 end 75 ret n 76 end 77 note show writes vals[0..n-1] separated by spaces, then a newline 78 fn show n ulong to void 79 for i ulong n 80 if gt i 0 81 emit sp 82 end 83 emiti get vals i 84 end 85 emit nl 86 end 87 note flip reverses vals[0..k-1]; k/2 swaps 88 fn flip k ulong to void 89 for i ulong shr k 1 90 t = get vals i 91 put vals i get vals sub sub k i 1 92 put vals sub sub k i 1 t 93 end 94 end 95 note pksort brings the largest of vals[0..s-1] to the front, then flips it to position s-1, for s = n .. 2; 96 note it returns the number of flips made, at most 2*(n-1) 97 fn pksort n ulong to ulong 98 f = ulong 0 99 for x ulong sub n 1 100 s = sub n x 101 m = ulong 0 102 for i ulong s 103 if gt get vals i get vals m 104 m = i 105 end 106 end 107 if ne m sub s 1 108 if ne m 0 109 flip add m 1 110 f = add f 1 111 end 112 flip s 113 f = add f 1 114 end 115 end 116 ret f 117 end 118 fn main to int 119 n = parse data 120 show n 121 f = pksort n 122 show n 123 emit tflips 124 emitu f 125 emit nl 126 ret 0 127 end