Order by pair comparisons: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 1,226:
After some Yes/No clicks you should get:
<pre>{"red", "orange", "yellow", "green", "blue", "indigo", "violet"}</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
insertSort = function(arr, item)
lo = 0
hi = arr.len
while lo < hi
mid = floor((lo + hi) / 2)
ans = input("Is " + item + " less than " + arr[mid] + "? y/n: ")
ans = ans[0].lower
if ans == "y" then
hi = mid
else
lo = mid + 1
end if
end while
arr.insert(lo, item)
end function
 
items = "violet red green indigo blue yellow orange".split
ordered = []
for item in items
insertSort(ordered, item)
end for
print ordered
</syntaxhighlight>
{{out}}
<pre>
Is red less than violet? y/n: y
Is green less than violet? y/n: y
Is green less than red? y/n: n
Is indigo less than green? y/n: n
Is indigo less than violet? y/n: y
Is blue less than indigo? y/n: y
Is blue less than green? y/n: n
Is yellow less than blue? y/n: y
Is yellow less than green? y/n: y
Is yellow less than red? y/n: n
Is orange less than blue? y/n: y
Is orange less than yellow? y/n: y
Is orange less than red? y/n: n
["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
</pre>
 
=={{header|Nim}}==
Line 1,722 ⟶ 1,765:
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./ioutil" for Input
import "./fmt" for Fmt
 
// Inserts item x in list a, and keeps it sorted assuming a is already sorted.
Line 1,777 ⟶ 1,820:
The colors of the rainbow, in sorted order, are:
[red, orange, yellow, green, blue, indigo, violet]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
int Items, Size, I, J, Gap, JG, T, C, Count;
[Items:= ["violet", "red", "green", "indigo", "blue", "yellow", "orange"];
Size:= 7;
Count:= 0;
Gap:= Size>>1;
while Gap > 0 do
[for I:= Gap to Size-1 do
[J:= I - Gap;
loop [JG:= J + Gap;
Count:= Count+1;
Print("%2d: Is %6s less than %6s (y/n)? ",
Count, Items(J), Items(JG));
repeat OpenI(1);
C:= ChIn(1);
until C=^y or C=^n;
ChOut(0, C); CrLf(0);
if C = ^y then quit;
T:= Items(J); Items(J):= Items(JG); Items(JG):= T;
J:= J - Gap;
if J < 0 then quit;
];
];
Gap:= Gap>>1;
];
Print("The colors of the rainbow, in sorted order, are:\n");
for I:= 0 to Size-2 do Print("%s, ", Items(I));
Print("%s\n", Items(I));
]</syntaxhighlight>
{{out}}
<pre>
1: Is violet less than indigo (y/n)? n
2: Is red less than blue (y/n)? y
3: Is green less than yellow (y/n)? n
4: Is violet less than orange (y/n)? n
5: Is indigo less than orange (y/n)? n
6: Is orange less than red (y/n)? n
7: Is orange less than yellow (y/n)? y
8: Is yellow less than indigo (y/n)? y
9: Is indigo less than blue (y/n)? n
10: Is yellow less than blue (y/n)? y
11: Is indigo less than green (y/n)? n
12: Is blue less than green (y/n)? n
13: Is yellow less than green (y/n)? y
14: Is indigo less than violet (y/n)? y
The colors of the rainbow, in sorted order, are:
red, orange, yellow, green, blue, indigo, violet
</pre>
9,479

edits