Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
m (moved Bubble Sort to Sorting algorithms/Bubble sort) |
(VBScript version) |
||
Line 1,352: | Line 1,352: | ||
output: |
output: |
||
<pre><140,212,247,270,315,362,449,532,567,677></pre> |
<pre><140,212,247,270,315,362,449,532,567,677></pre> |
||
=={{header|VBScript}}== |
|||
Doing the decr and incr thing is superfluous, really. I just had stumbled over the byref thing for <code>swap</code> and wanted to see where else it would work. |
|||
For those unfamiliar with Perth, WA Australia, the five strings being sorted are names of highways. |
|||
=====Implementation===== |
|||
<lang vb> |
|||
sub decr( byref n ) |
|||
n = n - 1 |
|||
end sub |
|||
sub incr( byref n ) |
|||
n = n + 1 |
|||
end sub |
|||
sub swap( byref a, byref b) |
|||
dim tmp |
|||
tmp = a |
|||
a = b |
|||
b = tmp |
|||
end sub |
|||
function bubbleSort( a ) |
|||
dim changed |
|||
dim itemCount |
|||
itemCount = ubound(a) |
|||
do |
|||
changed = false |
|||
decr itemCount |
|||
for i = 0 to itemCount |
|||
if a(i) > a(i+1) then |
|||
swap a(i), a(i+1) |
|||
changed = true |
|||
end if |
|||
next |
|||
loop until not changed |
|||
bubbleSort = a |
|||
end function |
|||
</lang> |
|||
=====Invocation===== |
|||
<lang vb> |
|||
dim a |
|||
a = array( "great eastern", "roe", "stirling", "albany", "leach") |
|||
wscript.echo join(a,", ") |
|||
bubbleSort a |
|||
wscript.echo join(a,", ") |
|||
</lang> |
|||
=====Output===== |
|||
<pre> |
|||
great eastern, roe, stirling, albany, leach |
|||
albany, great eastern, leach, roe, stirling |
|||
</pre> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |