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/bogosort.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 / bogosort.wiki` 141 lines ยท 2868 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 "3 -1 4 1 5 9 2" 10 text tshuf "shuffles: " 11 global vals long 8 12 global seed ulong 1 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 rnd is Knuth's MMIX linear congruential generator, x = x*`6364136223846793005` + `1442695040888963407` 91 note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 92 fn rnd to ulong 93 x = add mul get seed 0 `6364136223846793005` `1442695040888963407` 94 put seed 0 x 95 ret shr x 33 96 end 97 note shuffle is Fisher-Yates: for i = n-1 .. 1 swap vals[i] with vals[rnd mod (i+1)] 98 fn shuffle n ulong to void 99 for x ulong sub n 1 100 i = sub sub n x 1 101 j = rem rnd add i 1 102 t = get vals i 103 put vals i get vals j 104 put vals j t 105 end 106 end 107 fn inorder n ulong to bool 108 for i ulong sub n 1 109 if gt get vals i get vals add i 1 110 ret false 111 end 112 end 113 ret true 114 end 115 note bgsort shuffles until sorted; with this generator and input it takes the count printed, well under 116 note the bound of 200000 (7! = 5040 orders, so a working generator lands far inside it) 117 fn bgsort n ulong to ulong 118 c = ulong 0 119 while not inorder n bound 200000 120 shuffle n 121 c = add c 1 122 end 123 ret c 124 end 125 fn main to int 126 n = parse data 127 show n 128 c = bgsort n 129 show n 130 emit tshuf 131 emitu c 132 emit nl 133 ret 0 134 end 135 136 {{out}} 137

138 3 -1 4 1 5 9 2

139 -1 1 2 3 4 5 9

140 shuffles: 2314

141