Order by pair comparisons: Difference between revisions

Added Wren
m (→‎{{header|Factor}}: rephrase again)
(Added Wren)
Line 203:
 
red orange yellow green blue indigo violet</pre>
 
=={{header|Wren}}==
{{trans|Python}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/ioutil" for Input
import "/fmt" for Fmt
 
// Inserts item x in list a, and keeps it sorted assuming a is already sorted.
// If x is already in a, inserts it to the right of the rightmost x.
var insortRight = Fn.new{ |a, x, q|
var lo = 0
var hi = a.count
while (lo < hi) {
var mid = ((lo + hi)/2).floor
q = q + 1
var prompt = Fmt.swrite("$2d: Is $6s less than $6s ? y/n: ", q, x, a[mid])
var less = Input.option(prompt, "yn") == "y"
if (less) {
hi = mid
} else {
lo = mid + 1
}
}
a.insert(lo, x)
return q
}
 
var order = Fn.new { |items|
var ordered = []
var q = 0
for (item in items) {
q = insortRight.call(ordered, item, q)
}
return ordered
}
 
var items = "violet red green indigo blue yellow orange".split(" ")
var ordered = order.call(items)
System.print("\nThe colors of the rainbow, in sorted order, are:")
System.print(ordered)</lang>
 
{{out}}
<pre>
1: Is red less than violet ? y/n: y
2: Is green less than violet ? y/n: y
3: Is green less than red ? y/n: n
4: Is indigo less than green ? y/n: n
5: Is indigo less than violet ? y/n: y
6: Is blue less than indigo ? y/n: y
7: Is blue less than green ? y/n: n
8: Is yellow less than blue ? y/n: y
9: Is yellow less than green ? y/n: y
10: Is yellow less than red ? y/n: n
11: Is orange less than blue ? y/n: y
12: Is orange less than yellow ? y/n: y
13: Is orange less than red ? y/n: n
 
The colors of the rainbow, in sorted order, are:
[red, orange, yellow, green, blue, indigo, violet]
</pre>
9,483

edits