Sorting algorithms/Bubble sort: Difference between revisions

m
m (→‎{{header|FreeBASIC}}: removed redundant code)
m (→‎{{header|FreeBASIC}}: minor change)
Line 1,410:
=={{header|FreeBASIC}}==
Per task pseudo code:
<lang FreeBASIC>' version 21-0610-20152016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
 
Sub bubblesort(bs() As IntegerLong)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As IntegerLong lb = LBound(bs)
Dim As IntegerLong ub = UBound(bs) -1
Dim As IntegerLong done, i
 
Do
done = 0
For i = lb To ub -1
' replace "<" with ">" for downwards sort
If bs(i) > bs(i + 1) Then
Swap bs(i), bs(i + 1)
done = 1
End If
Next
ub = ub -1
Loop Until done = 0
 
Line 1,437 ⟶ 1,436:
' ------=< MAIN >=------
 
Dim As IntegerLong i, array(-7 To 7)
 
Dim As IntegerLong a = LBound(array), b = UBound(array)
 
Randomize Timer
For i = a To b : array(i) = i : Next
For i = a To b ' little shuffle
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
 
Line 1,459 ⟶ 1,458:
End</lang>
{{out}}
<pre>unsort -7 6 2 3 -4 -6 7 4 -1 -2 5 -62 -2 7 3 0 -4 -75 -3 1 -13 -5 06
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>
 
457

edits