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