Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
m (→‎{{header|FreeBASIC}}: removed redundant code)
m (→‎{{header|FreeBASIC}}: minor change)
Line 1,410: Line 1,410:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
Per task pseudo code:
Per task pseudo code:
<lang FreeBASIC>' version 21-06-2015
<lang FreeBASIC>' version 21-10-2016
' compile with: fbc -s console
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
' for boundry checks on array's compile with: fbc -s console -exx


Sub bubblesort(bs() As Integer)
Sub bubblesort(bs() As Long)
' sort from lower bound to the highter bound
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
' array's can have subscript range from -2147483648 to +2147483647
Dim As Integer lb = LBound(bs)
Dim As Long lb = LBound(bs)
Dim As Integer ub = UBound(bs) -1
Dim As Long ub = UBound(bs)
Dim As Integer done, i
Dim As Long done, i


Do
Do
done = 0
done = 0
For i = lb To ub
For i = lb To ub -1
' replace "<" with ">" for downwards sort
' replace "<" with ">" for downwards sort
If bs(i) > bs(i + 1) Then
If bs(i) > bs(i +1) Then
Swap bs(i), bs(i + 1)
Swap bs(i), bs(i +1)
done = 1
done = 1
End If
End If
Next
Next
ub = ub -1
Loop Until done = 0
Loop Until done = 0


Line 1,437: Line 1,436:
' ------=< MAIN >=------
' ------=< MAIN >=------


Dim As Integer i, array(-7 To 7)
Dim As Long i, array(-7 To 7)


Dim As Integer a = LBound(array), b = UBound(array)
Dim As Long a = LBound(array), b = UBound(array)


Randomize Timer
Randomize Timer
For i = a To b : array(i) = i : Next
For i = a To b : array(i) = i : Next
For i = a To b ' little shuffle
For i = a To b ' little shuffle
Swap array(i), array(Rnd * b)
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
Next


Line 1,459: Line 1,458:
End</lang>
End</lang>
{{out}}
{{out}}
<pre>unsort 6 2 4 7 1 5 -6 -2 3 -4 -7 -3 -1 -5 0
<pre>unsort -7 3 -4 -6 4 -1 -2 2 7 0 5 1 -3 -5 6
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>