Sorting algorithms/Selection sort: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added/changed comments and whitespace, added a subroutine to create the array. |
Added Wren |
||
Line 3,083: | Line 3,083: | ||
3,2,5,4,1 1,2,3,4,5 |
3,2,5,4,1 1,2,3,4,5 |
||
c,e,b,a,d a,b,c,d,e |
c,e,b,a,d a,b,c,d,e |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
<lang ecmascript>var selectionSort = Fn.new { |a| |
|||
var last = a.count - 1 |
|||
for (i in 0...last) { |
|||
var aMin = a[i] |
|||
var iMin = i |
|||
for (j in i+1..last) { |
|||
if (a[j] < aMin) { |
|||
aMin = a[j] |
|||
iMin = j |
|||
} |
|||
} |
|||
var t = a[i] |
|||
a[i] = aMin |
|||
a[iMin] = t |
|||
} |
|||
} |
|||
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)") |
|||
selectionSort.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> |
</pre> |
||