Sorting algorithms/Bubble sort: Difference between revisions

added FreeBasic example
(→‎{{header|Scratch}}: A new entry for Scratch)
(added FreeBasic example)
Line 1,405:
END DO
END SUBROUTINE Bubble_Sort</lang>
=={{header|FreeBASIC}}==
Per task pseudo code:
<lang FreeBASIC>' version 21-06-2015
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
 
Sub bubblesort(bs() As Integer)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Integer lb = LBound(bs)
Dim As Integer ub = UBound(bs)
Dim As Integer 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
Loop Until done = 0
 
End Sub
 
' ------=< MAIN >=------
 
Dim As Integer i, array(-7 To 7)
 
Dim As Integer 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(Rnd * b)
Next
 
Print "unsort ";
For i = a To b : Print Using "####"; array(i); : Next : Print
bubblesort(array()) ' sort the array
Print " sort ";
For i = a To b : Print Using "####"; array(i); : Next : Print
 
' empty keyboard buffer
While InKey <> "" : Var _key_ = InKey : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>unsort 6 2 4 7 1 5 -6 -2 3 -4 -7 -3 -1 -5 0
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>
 
=={{header|Go}}==
457

edits