Sorting algorithms/Bubble sort: Difference between revisions

Added Wren
No edit summary
(Added Wren)
Line 5,887:
<pre>Input: [3, 5, 2, 1, 4]
Output: [1, 2, 3, 4, 5]</pre>
 
=={{header|Wren}}==
Based on the pseudo-code in the Wikipedia article.
<lang ecmascript>var bubbleSort = Fn.new { |a|
var n = a.count
if (n < 2) return
while (true) {
var swapped = false
for (i in 1..n-1) {
if (a[i-1] > a[i]) {
var t = a[i-1]
a[i-1] = a[i]
a[i] = t
swapped = true
}
}
if (!swapped) return
}
}
 
var as = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
for (a in as) {
System.print("Before: %(a)")
bubbleSort.call(a)
System.print("After : %(a)")
System.print()
}</lang>
 
{{out}}
<pre>
Before: [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
After : [-31, 0, 1, 2, 2, 4, 65, 83, 99, 782]
 
Before: [7, 5, 2, 6, 1, 4, 2, 6, 3]
After : [1, 2, 2, 3, 4, 5, 6, 6, 7]
</pre>
 
=={{header|X86 Assembly}}==
9,485

edits