Sorting algorithms/Bubble sort: Difference between revisions

m
→‎{{header|Standard ML}}: fix syntax highlighting
(Added Oberon-02)
m (→‎{{header|Standard ML}}: fix syntax highlighting)
 
(37 intermediate revisions by 14 users not shown)
Line 5:
 
A   '''bubble'''   sort is also known as a   '''sinking'''   sort.
 
 
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
Line 1,142 ⟶ 1,141:
 
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
 
{{trans|Java}}
Assume numbers are in a DIM of size "size" called "nums".
<syntaxhighlight lang="qbasic">
DO
changed = 0
FOR I = 1 to size -1
IF nums(I) > nums(I + 1) THEN
tmp = nums(I)
nums(I) = nums(I + 1)
nums(I + 1) = tmp
changed = 1
END IF
NEXT
LOOP WHILE(NOT changed)</syntaxhighlight>
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="basic">0 GOSUB 7 : IC = I%(0)
Line 1,171 ⟶ 1,153:
9 FOR I = 1 TO I%(0) : PRINT I%(I)" "; : NEXT I : PRINT : RETURN</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASICBaCon}}===
Numeric example:
Works with the 1k RAM model. For simplicity, and to make it easy to animate the sort as it is going on, this implementation sorts a string of eight-bit unsigned integers which can be treated as character codes; it could easily be amended to sort an array of numbers or an array of strings, but the array would need to be dimensioned at the start.
<syntaxhighlight lang="basicbacon">LOCAL t[] = { 5, 7, 1, 3, 10, LET2, S$="FIRE9, BURN4, AND8, CAULDRON6 BUBBLE"}
total = 10
20 PRINT S$
WHILE total > 1
30 LET L=LEN S$-1
FOR x = 0 TO total-1
40 LET C=0
IF t[x] > t[x+1] THEN SWAP t[x], t[x+1]
50 FOR I=1 TO L
NEXT
60 IF S$(I)<=S$(I+1) THEN GOTO 120
DECR total
70 LET T$=S$(I)
WEND
80 LET S$(I)=S$(I+1)
PRINT COIL$(10, STR$(t[_-1]))</syntaxhighlight>
90 LET S$(I+1)=T$
100 PRINT AT 0,I-1;S$(I TO I+1)
110 LET C=1
120 NEXT I
130 LET L=L-1
140 IF C THEN GOTO 40</syntaxhighlight>
{{out}}
<pre>1 2 3 4 AABBBBCDDEEFILLNNNORRRUUU5 6 7 8 9 10</pre>
String example:
<syntaxhighlight lang="bacon">t$ = "Kiev Amsterdam Lima Moscow Warschau Vienna Paris Madrid Bonn Bern Rome"
total = AMOUNT(t$)
WHILE total > 1
FOR x = 1 TO total-1
IF TOKEN$(t$, x) > TOKEN$(t$, x+1) THEN t$ = EXCHANGE$(t$, x, x+1)
NEXT
DECR total
WEND
PRINT t$</syntaxhighlight>
{{out}}
<pre>Amsterdam Bern Bonn Kiev Lima Madrid Moscow Paris Rome Vienna Warschau</pre>
 
==={{header|BASIC256}}===
Line 1,255 ⟶ 1,244:
</pre>
 
==={{header|Chipmunk Basic}}===
The [[#Commodore_BASIC|Commodore BASIC]] solution works without any changes.
 
==={{header|Commodore BASIC}}===
Line 1,306 ⟶ 1,297:
920 DATA 64,34,25,12,22,11,90,13,59,47,19,89,10,17,31
</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">define sort = 0, index = 0, size = 10
define temp1 = 0, temp2 = 0
 
dim list[size]
 
gosub fill
gosub sort
gosub show
 
end
 
sub fill
 
for index = 0 to size - 1
 
let list[index] = int(rnd * 100)
 
next index
 
return
 
sub sort
 
do
 
let sort = 0
for index = 0 to size - 2
 
let temp1 = index + 1
 
if list[index] > list[temp1] then
 
let temp2 = list[index]
let list[index] = list[temp1]
let list[temp1] = temp2
let sort = 1
 
endif
 
next index
 
wait
 
loop sort = 1
 
return
 
sub show
 
for index = 0 to size - 1
 
print index ," : ", list[index]
 
next index
 
return</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Per task pseudo code:
<syntaxhighlight lang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
 
Sub bubblesort(bs() As Long)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long lb = LBound(bs)
Dim As Long ub = UBound(bs)
Dim As Long 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 Long i, array(-7 To 7)
 
Dim As Long 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
 
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 <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<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>
 
==={{header|FTCBASIC}}===
<syntaxhighlight lang="basic">rem bubble sort benchmark example
rem compile with FTCBASIC
 
use time.inc
use random.inc
 
define const size = 32000
 
dim list[size]
 
define sorting = 0, index = 0, elements = 0
define timestamp = 0, sorttime = 0
define temp1 = 0, temp2 = 0
 
cls
 
print "Bubble sort benchmark test"
 
do
 
print "How many elements to generate and sort (max " \
print size \
print ")? " \
 
input elements
 
loop elements > size
 
gosub fill
gosub sort
 
print "done!"
print "sort time: " \
print sorttime
print "Press any key to view sorted data..."
 
pause
 
gosub output
 
pause
end
 
sub fill
 
print "filling..."
 
0 index
 
do
 
gosub generaterand
 
let @list[index] = rand
 
+1 index
 
loop index < elements
 
return
 
sub sort
 
print "sorting..."
 
gosub systemtime
let timestamp = loworder
 
do
 
0 sorting
 
0 index
 
do
 
let temp1 = index + 1
 
if @list[index] > @list[temp1] then
 
let temp2 = @list[index]
 
let @list[index] = @list[temp1]
let @list[temp1] = temp2
 
let sorting = 1
 
endif
 
+1 index
 
loop index < elements - 1
 
loop sorting = 1
 
gosub systemtime
let sorttime = ( loworder - timestamp ) / 18
 
return
 
sub output
 
print "printing..."
 
0 index
 
do
 
print @list[index]
 
+1 index
 
loop index < elements
 
return</syntaxhighlight>
 
==={{header|FutureBasic}}===
Bubble sorting is purely an academic exercise since there are much more efficient native sorting functions in FB.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn BubbleSort( array as CFMutableArrayRef ) as CFArrayRef
NSUInteger i, x, y, count = len(array)
BOOL swapped = YES
while (swapped)
swapped = NO
for i = 1 to count -1
x = fn NumberIntegerValue( array[i-1] )
y = fn NumberIntegerValue( array[i] )
if ( x > y )
MutableArrayExchangeObjects( array, (i-1), i )
swapped = YES
end if
next
wend
end fn = array
 
CFMutableArrayRef array
CFArrayRef unsortedArray, sortedArray
NSUInteger i
 
array = fn MutableArrayWithCapacity(0)
for i = 0 to 20
MutableArrayAddObject( array, fn NumberWithInteger( rnd(100) ) )
next
 
unsortedArray = fn ArrayWithArray( array )
sortedArray = fn BubbleSort( array )
 
NSLog( @"\n-----------------\nUnsorted : Sorted\n-----------------" )
for i = 0 to 20
NSLog( @"%8ld : %-8ld", fn NumberIntegerValue( unsortedArray[i] ), fn NumberIntegerValue( sortedArray[i] ) )
next
 
randomize
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
-----------------
Unsorted : Sorted
-----------------
97 : 7
91 : 8
13 : 13
39 : 17
50 : 20
48 : 28
7 : 28
61 : 30
30 : 30
20 : 33
69 : 39
86 : 42
33 : 48
65 : 50
28 : 50
50 : 61
28 : 65
8 : 69
17 : 86
42 : 91
30 : 97
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=ba84832d633cb92bbe6c2f54704819c3 Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim byToSort As Byte[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 147, 154, 46, 102, 183, 24,
120, 19, 123, 2, 17, 226, 11, 211, 25, 191, 205, 77]
Dim byCount As Byte
Dim bSorting As Boolean
 
Print "To sort: -"
ShowWorking(byToSort)
Print
Repeat
bSorting = False
For byCount = 0 To byToSort.Max - 1
If byToSort[byCount] > byToSort[byCount + 1] Then
Swap byToSort[byCount], byToSort[byCount + 1]
bSorting = True
Endif
Next
If bSorting Then ShowWorking(byToSort)
Until bSorting = False
End
'-----------------------------------------
Public Sub ShowWorking(byToSort As Byte[])
Dim byCount As Byte
 
For byCount = 0 To byToSort.Max
Print Str(byToSort[byCount]);
If byCount <> byToSort.Max Then Print ",";
Next
 
Print
 
End</syntaxhighlight>
Output:
<pre>
To sort: -
249,28,111,36,171,98,29,192,44,147,154,46,102,183,24,120,19,123,2,17,226,11,211,25,191,205,77
 
28,111,36,171,98,29,192,44,147,154,46,102,183,24,120,19,123,2,17,226,11,211,25,191,205,77,249
28,36,111,98,29,171,44,147,154,46,102,183,24,120,19,123,2,17,192,11,211,25,191,205,77,226,249
28,36,98,29,111,44,147,154,46,102,171,24,120,19,123,2,17,183,11,192,25,191,205,77,211,226,249
28,36,29,98,44,111,147,46,102,154,24,120,19,123,2,17,171,11,183,25,191,192,77,205,211,226,249
28,29,36,44,98,111,46,102,147,24,120,19,123,2,17,154,11,171,25,183,191,77,192,205,211,226,249
28,29,36,44,98,46,102,111,24,120,19,123,2,17,147,11,154,25,171,183,77,191,192,205,211,226,249
28,29,36,44,46,98,102,24,111,19,120,2,17,123,11,147,25,154,171,77,183,191,192,205,211,226,249
28,29,36,44,46,98,24,102,19,111,2,17,120,11,123,25,147,154,77,171,183,191,192,205,211,226,249
28,29,36,44,46,24,98,19,102,2,17,111,11,120,25,123,147,77,154,171,183,191,192,205,211,226,249
28,29,36,44,24,46,19,98,2,17,102,11,111,25,120,123,77,147,154,171,183,191,192,205,211,226,249
28,29,36,24,44,19,46,2,17,98,11,102,25,111,120,77,123,147,154,171,183,191,192,205,211,226,249
28,29,24,36,19,44,2,17,46,11,98,25,102,111,77,120,123,147,154,171,183,191,192,205,211,226,249
28,24,29,19,36,2,17,44,11,46,25,98,102,77,111,120,123,147,154,171,183,191,192,205,211,226,249
24,28,19,29,2,17,36,11,44,25,46,98,77,102,111,120,123,147,154,171,183,191,192,205,211,226,249
24,19,28,2,17,29,11,36,25,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
19,24,2,17,28,11,29,25,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
19,2,17,24,11,28,25,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
2,17,19,11,24,25,28,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
2,17,11,19,24,25,28,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
2,11,17,19,24,25,28,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
</pre>
 
==={{header|GW-BASIC}}===
Line 1,376 ⟶ 1,727:
350 END DEF</syntaxhighlight>
 
==={{header|BaConLiberty BASIC}}===
{{works with|Just BASIC}}
Numeric example:
<syntaxhighlight lang="baconlb">LOCAL t[] = { 5, 7, 1, 3, 10, 2, 9, 4, 8, 6 }
totalitemCount = 1020
dim item(itemCount)
WHILE total > 1
for FOR xi = 01 TOto total-1itemCount
item(i) = int(rnd(1) * 100)
IF t[x] > t[x+1] THEN SWAP t[x], t[x+1]
next i
NEXT
print "Before Sort"
DECR total
for i = 1 to itemCount
WEND
print item(i)
PRINT COIL$(10, STR$(t[_-1]))</syntaxhighlight>
next i
print: print
counter = itemCount
do
hasChanged = 0
for i = 1 to counter - 1
if item(i) > item(i + 1) then
temp = item(i)
item(i) = item(i + 1)
item(i + 1) = temp
hasChanged = 1
end if
next i
counter =counter -1
loop while hasChanged = 1
print "After Sort"
for i = 1 to itemCount
print item(i)
next i
end
</syntaxhighlight>
 
==={{header|microA BASIC}}===
{{works with|microA }}
<syntaxhighlight lang="basic">
'bubble sort in micro(A) BASIC
wcolor 0,0,0 : fcolor 150,180,240
var start,endtime,time
var sort,index,size,temp1,temp2,x,xl,y
size = 1000 : x = 10 : y = 20 : xl = 40
var list[size]
index = 1
GETTICK start
'---------------------------------------------
label do1
list[index] = rand(100)
index = index + 1
if index < size : goto do1 : endif
'---------------------------------------------
label do2
sort = 0
index = 1
label do3
'---------------------------------------------
temp1 = index + 1
if list[index] > list[temp1]
temp2 = list[index]
list[index] = list[temp1]
list[temp1] = temp2
sort = 1
endif
index = index + 1
if index < size - 1 : goto do3 : endif
'-----------------------------------------------
if sort = 1 : goto do2 : endif
'-----------------------------------------------
index = 1
GETTICK endtime
time = (endTime - start) / 1000
fcolor 230,140,120: print 300,10,time : swap
index = 970
'check sort /////////////////////////////////////
label do4
print x,y ,index : print xl,y, list[index]
y = y + 20 : swap
index = index + 1
swap
if index < 1000 : goto do4 :endif
'////////////////////////////////////////////////
</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|QuickBASIC}}
{{works with|Bywater BASIC|2.61}}
<syntaxhighlight lang="basic">
100 REM Sorting algorithms/Bubble sort
110 REM Prepare data
120 REM N - size; A - array of nums
130 LET N = 10
140 OPTION BASE 1
150 DIM A(10)
160 RANDOMIZE
170 PRINT "Before: ";
180 FOR I = 1 TO N
190 LET A(I) = INT(RND*100)
200 PRINT A(I);
210 NEXT I
220 PRINT
230 REM Sort
240 REM C - counter; H - has changed
250 LET C = N
260 LET H = 0
270 FOR I = 1 TO C-1
280 IF A(I) <= A(I+1) THEN 330
290 LET T = A(I)
300 LET A(I) = A(I+1)
310 LET A(I+1) = T
320 LET H = 1
330 NEXT I
340 LET C = C-1
350 IF H = 1 THEN 260
360 REM Display result
370 PRINT "After: ";
380 FOR I = 1 TO N
390 PRINT A(I);
400 NEXT I
410 PRINT
420 END
</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure bubbleSort(Array a(1))
Protected i, itemCount, hasChanged
itemCount = ArraySize(a())
Repeat
hasChanged = #False
itemCount - 1
For i = 0 To itemCount
If a(i) > a(i + 1)
Swap a(i), a(i + 1)
hasChanged = #True
EndIf
Next
Until hasChanged = #False
EndProcedure</syntaxhighlight>
 
==={{header|QuickBASIC}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">
' Sorting algorithms/Bubble sort
' Prepare data
size = 10
OPTION BASE 1
DIM nums(size)
RANDOMIZE TIMER
PRINT "Before:";
FOR I = 1 TO size
nums(I) = INT(RND * 100)
PRINT USING " ##"; nums(I);
NEXT I
PRINT
 
' Sort
counter = size
DO
changed = 0
FOR I = 1 TO counter - 1
IF nums(I) > nums(I + 1) THEN
tmp = nums(I)
nums(I) = nums(I + 1)
nums(I + 1) = tmp
changed = 1
END IF
NEXT I
counter = counter - 1
LOOP WHILE (changed)
 
' Display result
PRINT "After: ";
FOR I = 1 TO 10
PRINT USING " ##"; nums(I);
NEXT I
PRINT
END</syntaxhighlight>
{{out}} (2 samples)
<pre>Before: 91 97 3 62 17 48 89 7 2 66
After: 2 3 7 17 48 62 66 89 91 97</pre>
<pre>Before: 22 60 45 44 54 93 84 27 21 64
After: 21 22 27 44 45 54 60 64 84 93</pre>
 
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">100 rem Sorting algorithms/Bubble sort
110 LET n = 10
120 array a
130 GOSUB 310
140 PRINT "unsort ";
150 GOSUB 360
160 rem Sort the array
170 GOSUB 210
180 PRINT " sort ";
190 GOSUB 360
200 END
210 rem Bubble sort the list A of length N
220 FOR i = 1 TO n-1
230 FOR j = 1 TO n-i
240 IF a[j] <= a[j+1] THEN GOTO 280
250 LET x = a[j]
260 LET a[j] = a[j+1]
270 LET a[j+1] = x
280 NEXT j
290 NEXT i
300 RETURN
310 rem Create a RANDOM list of N integers
320 FOR i = 1 TO n
330 LET a[i] = FLOOR(RAND(100))
340 NEXT i
350 RETURN
360 rem Print the list a
370 FOR i = 1 TO n
380 PRINT a[i];" ";
390 NEXT i
400 PRINT
410 RETURN</syntaxhighlight>
{{out}}
<pre>1unsort 2 319 478 539 654 763 868 966 52 94 2 10</pre>
sort 2 19 39 52 54 63 66 68 78 94 </pre>
String example:
 
<syntaxhighlight lang="bacon">t$ = "Kiev Amsterdam Lima Moscow Warschau Vienna Paris Madrid Bonn Bern Rome"
==={{header|RapidQ}}===
total = AMOUNT(t$)
{{trans|QuickBASIC}}
WHILE total > 1
<syntaxhighlight lang="basic">
FOR x = 1 TO total-1
' Sorting algorithms/Bubble sort
IF TOKEN$(t$, x) > TOKEN$(t$, x+1) THEN t$ = EXCHANGE$(t$, x, x+1)
' Prepare data
size = 10
DIM nums(1 TO size)
RANDOMIZE TIMER
PRINT "Before:";
FOR I = 1 TO size
nums(I) = INT(RND * 100)
PRINT FORMAT$(" %2d", nums(I));
NEXT I
PRINT
 
' Sort
counter = size
DO
changed = 0
FOR I = 1 TO counter - 1
IF nums(I) > nums(I + 1) THEN
tmp = nums(I)
nums(I) = nums(I + 1)
nums(I + 1) = tmp
changed = -1
END IF
NEXT I
counter = counter - 1
LOOP UNTIL NOT changed
 
' Display result
PRINT "After: ";
FOR I = 1 TO 10
PRINT FORMAT$(" %2d", nums(I));
NEXT I
PRINT
END</syntaxhighlight>
{{out}} (2 samples)
<pre>Before: 82 34 57 44 48 71 19 33 73 62
After: 19 33 34 44 48 57 62 71 73 82</pre>
<pre>Before: 4 15 96 93 27 24 9 80 10 21
After: 4 9 10 15 21 24 27 80 93 96</pre>
 
==={{header|REALbasic}}===
Sorts an array of Integers.
<syntaxhighlight lang="vb">
Dim sortable() As Integer = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sortable.Shuffle() ' sortable is now randomized
Dim swapped As Boolean
Do
Dim index, bound As Integer
bound = sortable.Ubound
 
While index < bound
If sortable(index) > sortable(index + 1) Then
Dim s As Integer = sortable(index)
sortable.Remove(index)
sortable.Insert(index + 1, s)
swapped = True
End If
index = index + 1
Wend
Loop Until Not swapped
'sortable is now sorted
</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|QBasic|1.1}}
{{works with|Just BASIC}}
<syntaxhighlight lang="runbasic">siz = 100
dim data$(siz)
unSorted = 1
 
WHILE unSorted
unSorted = 0
FOR i = 1 TO siz -1
IF data$(i) > data$(i +1) THEN
tmp$ = data$(i)
data$(i) = data$(i +1)
data$(i + 1) = tmp$
unSorted = 1
END IF
NEXT
WEND</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Works with the 1k RAM model. For simplicity, and to make it easy to animate the sort as it is going on, this implementation sorts a string of eight-bit unsigned integers which can be treated as character codes; it could easily be amended to sort an array of numbers or an array of strings, but the array would need to be dimensioned at the start.
<syntaxhighlight lang="basic"> 10 LET S$="FIRE BURN AND CAULDRON BUBBLE"
20 PRINT S$
30 LET L=LEN S$-1
40 LET C=0
50 FOR I=1 TO L
60 IF S$(I)<=S$(I+1) THEN GOTO 120
70 LET T$=S$(I)
80 LET S$(I)=S$(I+1)
90 LET S$(I+1)=T$
100 PRINT AT 0,I-1;S$(I TO I+1)
110 LET C=1
120 NEXT I
130 LET L=L-1
140 IF C THEN GOTO 40</syntaxhighlight>
{{out}}
<pre> AABBBBCDDEEFILLNNNORRRUUU</pre>
 
==={{header|TI-83 BASIC}}===
Input your data into L<sub>1</sub> and run this program to organize it.
:L<sub>1</sub>→L<sub>2</sub>
:1+dim(L<sub>2</sub>)→N
:For(D,1,dim(L<sub>2</sub>))
:N-1→N
:0→I
:For(C,1,dim(L<sub>2</sub>)-2)
:For(A,dim(L<sub>2</sub>)-N+1,dim(L<sub>2</sub>)-1)
:If L<sub>2</sub>(A)&gt;L<sub>2</sub>(A+1)
:Then
:1→I
:L<sub>2</sub>(A)→B
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
:B→L<sub>2</sub>(A+1)
:End
:End
:End
:If I=0
:Goto C
:End
:Lbl C
:If L<sub>2</sub>(1)&gt;L<sub>2</sub>(2)
:Then
:L<sub>2</sub>(1)→B
:L<sub>2</sub>(2)→L<sub>2</sub>(1)
:B→L<sub>2</sub>(2)
:End
:DelVar A
:DelVar B
:DelVar C
:DelVar D
:DelVar N
:DelVar I
:Return
 
[[wp:Odd-even sort|Odd-Even Bubble Sort]] (same IO):
:"ODD-EVEN"
:L<sub>1</sub>→L<sub>2</sub>(
:1+dim(L<sub>2</sub>)→N
:For(D,1,dim(L<sub>2</sub>))
:N-1→N
:0→O
:For(C,1,dim(L<sub>2</sub>)-2)
:For(A,dim(L<sub>2</sub>)-N+2,dim(L<sub>2</sub>)-1,2)
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1)
:Then
:1→O
:L<sub>2</sub>(A)→B
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
:B→L<sub>2</sub>(A+1)
:End
:End
:For(A,dim(L<sub>2</sub>)-N+1,dim(L<sub>2</sub>)-1,2)
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1)
:Then
:1→O
:L<sub>2</sub>(A)→B
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
:B→L<sub>2</sub>(A+1)
:End
:End
:End
:If O=0
:Goto C
:End
:Lbl C
:If L<sub>2</sub>(1)>L<sub>2</sub>(2)
:Then
:L<sub>2</sub>(1)→B
:L<sub>2</sub>(2)→L<sub>2</sub>(1)
:B→L<sub>2</sub>(2)
:End
:DelVar A
:DelVar B
:DelVar C
:DelVar D
:DelVar N
:DelVar O
:Return
 
Implementation of the pseudo code given at the top of the page. Place data to be sorted in L<sub>1</sub>
:dim(L<sub>1</sub>)→D
:Repeat C=0
:0→C
:D–1→D
:For(I,1,D)
:If L<sub>1</sub>(I)>L<sub>1</sub>(I+1):Then
:L<sub>1</sub>(I)→C
:L<sub>1</sub>(I+1)→L<sub>1</sub>(I)
:C→L<sub>1</sub>(I+1)
:1→C
:End
:End
:End
:L<sub>1</sub>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">OPTION BASE 1
LET size = 10
DIM nums(0)
MAT REDIM nums(size)
RANDOMIZE
PRINT "Before:";
FOR i = 1 TO size
LET nums(i) = INT(RND*100)
PRINT USING " ##": nums(i);
NEXT i
PRINT
 
! Sort
LET counter = size
DO
LET changed = 0
FOR i = 1 TO counter-1
IF nums(i) > nums(i+1) THEN
LET tmp = nums(i)
LET nums(i) = nums(i+1)
LET nums(i+1) = tmp
LET changed = 1
END IF
NEXT i
LET counter = counter-1
LOOP WHILE (changed<>0)
 
! Display result
PRINT "After: ";
FOR i = 1 TO 10
PRINT USING " ##": nums(i);
NEXT i
PRINT
END</syntaxhighlight>
{{out}}
<pre>Similar as QuickBASIC entry.</pre>
 
==={{header|uBasic/4tH}}===
<syntaxhighlight lang="text">PRINT "Bubble sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
PROC _Bubblesort (n)
PROC _ShowArray (n)
PRINT
END
_Bubblesort PARAM(1) ' Bubble sort
LOCAL (2)
 
DO
b@ = 0
FOR c@ = 1 TO a@-1
IF @(c@-1) > @(c@) THEN PROC _Swap (c@, c@-1) : b@ = c@
NEXT
DECRa@ total= b@
UNTIL b@ = 0
WEND
LOOP
PRINT t$</syntaxhighlight>
 
RETURN
_Swap PARAM(2) ' Swap two array elements
PUSH @(a@)
@(a@) = @(b@)
@(b@) = POP()
RETURN
_InitArray ' Init example array
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
FOR i = 0 TO 9
@(i) = POP()
NEXT
RETURN (i)
_ShowArray PARAM (1) ' Show array subroutine
FOR i = 0 TO a@-1
PRINT @(i),
NEXT
PRINT
RETURN</syntaxhighlight>
 
==={{header|VBA}}===
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function bubble_sort(s As Variant) As Variant
Dim tmp As Variant
Dim changed As Boolean
For j = UBound(s) To 1 Step -1
changed = False
For i = 1 To j - 1
If s(i) > s(i + 1) Then
tmp = s(i)
s(i) = s(i + 1)
s(i + 1) = tmp
changed = True
End If
Next i
If Not changed Then
Exit For
End If
Next j
bubble_sort = s
End Function
Public Sub main()
s = [{4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}]
Debug.Print "Before: "
Debug.Print Join(s, ", ")
Debug.Print "After: "
Debug.Print Join(bubble_sort(s), ", ")
End Sub</syntaxhighlight>{{out}}
<pre>Before:
4, 15, delta, 2, -31, 0, alfa, 19, gamma, 2, 13, beta, 782, 1
After:
-31, 0, 1, 2, 2, 4, 13, 15, 19, 782, alfa, beta, delta, gamma</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=====
<syntaxhighlight 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
</syntaxhighlight>
 
=====Invocation=====
<syntaxhighlight lang="vb">
dim a
a = array( "great eastern", "roe", "stirling", "albany", "leach")
wscript.echo join(a,", ")
bubbleSort a
wscript.echo join(a,", ")
</syntaxhighlight>
 
{{out}}
<pre>
<pre>Amsterdam Bern Bonn Kiev Lima Madrid Moscow Paris Rome Vienna Warschau</pre>
great eastern, roe, stirling, albany, leach
albany, great eastern, leach, roe, stirling
</pre>
 
==={{header|Visual Basic .NET}}===
'''Platform:''' [[.NET]]
 
{{works with|Visual Basic .NET|9.0+}}
<syntaxhighlight lang="vbnet">Do Until NoMoreSwaps = True
NoMoreSwaps = True
For Counter = 1 To (NumberOfItems - 1)
If List(Counter) > List(Counter + 1) Then
NoMoreSwaps = False
Temp = List(Counter)
List(Counter) = List(Counter + 1)
List(Counter + 1) = Temp
End If
Next
NumberOfItems = NumberOfItems - 1
Loop</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">// Animated sort.
// Original idea by William Tang, obtained from MicroHobby 25 Years (https://microhobby.speccy.cz/zxsf/MH-25Years.pdf)
 
clear screen
 
n=15 : m=18 : y=9 : t$=chr$(17)+chr$(205)+chr$(205)
dim p(n), p$(n)
 
for x=1 TO n
p(x)=ran(15)+1
p$(x)=str$(p(x),"##.######")
print at(0,x) p$(x)
next x
 
for j=1 to n-1
for i=j+1 to n
l=n+j-i+1
if p(j) > p(l) then
print color("yellow","red") at(0,j) p$(j)
if l<>m then
for x=m to l step sig(l-m): print at(18,x) t$ : print at (18,x+sig(m-l)) " " : pause .02 : next x
end if
for x=17 TO y step -1 : print at(x,l) t$+" " : pause .02 : next x
for x=0 TO 10 : print at(x,l) " "+p$(l)+t$ : pause .02 : next x
for x=l TO j STEP -1 : print at(11,x) p$(l)+t$ : print at(11,x+1) " " : pause .02 : next x
print at(0,j) " "
for x=j+1 TO l-1 : print color("yellow","red") at(0,x) p$(j) : pause .02 : print at(0,x) p$(x) : pause .02 : next x
print at(0,l) p$(j)
for x=10 TO 0 step -1 : print at(x,j) p$(l)+t$+" " : pause .02 : next x
for x=y TO 17 : print at(x,j) " "+t$ : pause .02 : next x
m=j
t=p(l) : tem$=p$(l)
p(l)=p(j) : p$(l)=p$(j)
p(j)=t : p$(j)=tem$
end if
pause .02
next i
next j
 
for x=m TO 18 : print at(18,x-1) " " : print at(18,x) t$ : pause .02 : next x
</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang="zxbasic">5000 CLS
5002 LET a$="": FOR f=1 TO 64: LET a$=a$+CHR$ (32+INT (RND*96)): NEXT f
5004 PRINT a$; AT 10,0;"ZigZag BubbleSORT"
5010 LET la=LEN a$
5011 LET i=1: LET u=0
5020 LET d=0: LET p=(u=0)-(u=1)
5021 LET l=(i AND u=0)+(la-i+u AND u=1)
5030 IF u=0 THEN IF a$(l+1)>=a$(l) THEN GO TO 5050
5031 IF u=1 THEN IF a$(l-1)<=a$(l) THEN GO TO 5050
5040 LET d=1
5042 LET t$=a$(l+p)
5043 LET a$(l+p)=a$(l)
5044 LET a$(l)=t$
5050 LET l=l+p
5051 PRINT AT 10,21;a$(l);AT 12,0;a$
5055 IF l<=la-i AND l>=i THEN GO TO 5023
5061 LET i=i+NOT u
5063 LET u=NOT u
5064 IF d AND i<la THEN GO TO 5020
5072 PRINT AT 12,0;a$
9000 STOP </syntaxhighlight>
 
The traditional solution:
 
<syntaxhighlight lang="zxbasic"> 10 LET siz=32
20 DIM d$(siz)
30 REM Populate d$
40 FOR n=1 TO siz: LET d$(n)=CHR$ (48+INT (RND*75)): NEXT n
50 PRINT d$
60 LET unSorted=0
70 FOR i=1 TO siz-1
80 IF d$(i)>d$(i+1) THEN LET t$=d$(i): LET d$(i)=d$(i+1): LET d$(i+1)=t$: LET unSorted=1
90 NEXT i
100 IF unSorted THEN LET siz=siz-1: GO TO 60
110 PRINT d$</syntaxhighlight>
 
=={{header|BCPL}}==
Line 1,428 ⟶ 2,448:
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10</pre>
 
=={{header|Befunge}}==
 
<syntaxhighlight lang="befunge">
000p&: v >20g:7g\1+7g2v
v00p7g00 _10p0>20p20g:7g\1+7g`|0p7+1g02p7g0<
>g1+00p&:^ |-\g00:+1g02 <
0 >$10gv
|-\g00:+1 <
>1->:7g\:#v_$>:#._25*,@
^ <
</syntaxhighlight>
 
=={{header|C}}==
Line 2,184 ⟶ 3,216:
 
(Uses the primitive __loop directly because it happens to map to the termination test for this algorithm well.)
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
proc bubbleSort . a[] .
repeat
changed = 0
for i = 1 to len a[] - 1
if a[i] > a[i + 1]
swap a[i] a[i + 1]
changed = 1
.
.
until changed = 0
.
.
array[] = [ 5 1 19 25 12 1 14 7 ]
bubbleSort array[]
print array[]
</syntaxhighlight>
{{out}}
<pre>[ 1 1 5 7 12 14 19 25 ]</pre>
 
=={{header|EchoLisp}}==
Line 2,472 ⟶ 3,525:
=={{header|Elena}}==
{{trans|C#}}
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 2,488 ⟶ 3,541:
madeChanges := false;
itemCount -= 1;
for(int i := 0,; i < itemCount,; i += 1)
{
if (list[i] > list[i + 1])
Line 2,846 ⟶ 3,899:
END DO
END SUBROUTINE Bubble_Sort</syntaxhighlight>
 
=={{header|FreeBASIC}}==
Per task pseudo code:
<syntaxhighlight lang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
 
Sub bubblesort(bs() As Long)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long lb = LBound(bs)
Dim As Long ub = UBound(bs)
Dim As Long 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 Long i, array(-7 To 7)
 
Dim As Long 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
 
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 <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<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>
 
 
 
=={{header|FutureBasic}}==
Bubble sorting is purely an academic exercise since there are much more efficient native sorting functions in FB.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn BubbleSort( array as CFMutableArrayRef ) as CFArrayRef
NSUInteger i, x, y, count = len(array)
BOOL swapped = YES
while (swapped)
swapped = NO
for i = 1 to count -1
x = fn NumberIntegerValue( array[i-1] )
y = fn NumberIntegerValue( array[i] )
if ( x > y )
MutableArrayExchangeObjects( array, (i-1), i )
swapped = YES
end if
next
wend
end fn = array
 
CFMutableArrayRef array
CFArrayRef unsortedArray, sortedArray
NSUInteger i
 
array = fn MutableArrayWithCapacity(0)
for i = 0 to 20
MutableArrayAddObject( array, fn NumberWithInteger( rnd(100) ) )
next
 
unsortedArray = fn ArrayWithArray( array )
sortedArray = fn BubbleSort( array )
 
NSLog( @"\n-----------------\nUnsorted : Sorted\n-----------------" )
for i = 0 to 20
NSLog( @"%8ld : %-8ld", fn NumberIntegerValue( unsortedArray[i] ), fn NumberIntegerValue( sortedArray[i] ) )
next
 
randomize
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
-----------------
Unsorted : Sorted
-----------------
97 : 7
91 : 8
13 : 13
39 : 17
50 : 20
48 : 28
7 : 28
61 : 30
30 : 30
20 : 33
69 : 39
86 : 42
33 : 48
65 : 50
28 : 50
50 : 61
28 : 65
8 : 69
17 : 86
42 : 91
30 : 97
</pre>
 
=={{header|g-fu}}==
Line 2,992 ⟶ 3,919:
</syntaxhighlight>
 
=={{header|GambasGDScript}}==
Here is an implementation of Bubble Sort algorithm in GDScript for array of primitive types:
'''[https://gambas-playground.proko.eu/?gist=ba84832d633cb92bbe6c2f54704819c3 Click this link to run this code]'''
<syntaxhighlight lang="gambasgdscript">Public Sub Main()
extends Node2D
Dim byToSort As Byte[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 147, 154, 46, 102, 183, 24,
120, 19, 123, 2, 17, 226, 11, 211, 25, 191, 205, 77]
Dim byCount As Byte
Dim bSorting As Boolean
 
Print "To sort: -"
ShowWorking(byToSort)
Print
Repeat
bSorting = False
For byCount = 0 To byToSort.Max - 1
If byToSort[byCount] > byToSort[byCount + 1] Then
Swap byToSort[byCount], byToSort[byCount + 1]
bSorting = True
Endif
Next
If bSorting Then ShowWorking(byToSort)
Until bSorting = False
End
'-----------------------------------------
Public Sub ShowWorking(byToSort As Byte[])
Dim byCount As Byte
 
func BubbleSort(_array : Array) -> void:
For byCount = 0 To byToSort.Max
for i in range(_array.size() - 1):
Print Str(byToSort[byCount]);
var swapped : bool = false
If byCount <> byToSort.Max Then Print ",";
for j in range(_array.size() - i - 1):
Next
if _array[j] > _array[j + 1]:
var tmp = _array[j]
_array[j] = _array[j + 1]
_array[j + 1] = tmp
swapped = true
if not swapped:
break
return
 
Print
 
func _ready() -> void:
End</syntaxhighlight>
var array : Array = range(-10, 10)
Output:
array.shuffle()
 
print("Initial array:")
print(array)
 
BubbleSort(array)
 
print("Sorted array:")
print(array)
return
</syntaxhighlight>
 
Possible output:
{{out}}
<pre>
Initial array:
To sort: -
[-7, -6, 2, -8, 4, -1, -3, -5, 5, -10, -4, 7, 3, 8, 0, -9, 6, 9, -2, 1]
249,28,111,36,171,98,29,192,44,147,154,46,102,183,24,120,19,123,2,17,226,11,211,25,191,205,77
Sorted array:
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
 
If you need to sort objects, this can be done in way like this:
28,111,36,171,98,29,192,44,147,154,46,102,183,24,120,19,123,2,17,226,11,211,25,191,205,77,249
<syntaxhighlight lang="gdscript">
28,36,111,98,29,171,44,147,154,46,102,183,24,120,19,123,2,17,192,11,211,25,191,205,77,226,249
class Comparable:
28,36,98,29,111,44,147,154,46,102,171,24,120,19,123,2,17,183,11,192,25,191,205,77,211,226,249
var Value
28,36,29,98,44,111,147,46,102,154,24,120,19,123,2,17,171,11,183,25,191,192,77,205,211,226,249
 
28,29,36,44,98,111,46,102,147,24,120,19,123,2,17,154,11,171,25,183,191,77,192,205,211,226,249
func _init(_val):
28,29,36,44,98,46,102,111,24,120,19,123,2,17,147,11,154,25,171,183,77,191,192,205,211,226,249
self.Value = _val
28,29,36,44,46,98,102,24,111,19,120,2,17,123,11,147,25,154,171,77,183,191,192,205,211,226,249
 
28,29,36,44,46,98,24,102,19,111,2,17,120,11,123,25,147,154,77,171,183,191,192,205,211,226,249
func compare(_other : Comparable) -> int:
28,29,36,44,46,24,98,19,102,2,17,111,11,120,25,123,147,77,154,171,183,191,192,205,211,226,249
# Here is the simple implementation of compare
28,29,36,44,24,46,19,98,2,17,102,11,111,25,120,123,77,147,154,171,183,191,192,205,211,226,249
# for primitive type wrapper.
28,29,36,24,44,19,46,2,17,98,11,102,25,111,120,77,123,147,154,171,183,191,192,205,211,226,249
return self.Value - _other.Value
28,29,24,36,19,44,2,17,46,11,98,25,102,111,77,120,123,147,154,171,183,191,192,205,211,226,249
 
28,24,29,19,36,2,17,44,11,46,25,98,102,77,111,120,123,147,154,171,183,191,192,205,211,226,249
 
24,28,19,29,2,17,36,11,44,25,46,98,77,102,111,120,123,147,154,171,183,191,192,205,211,226,249
func BubbleSortObjects(_array : Array) -> void:
24,19,28,2,17,29,11,36,25,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
for i in range(_array.size() - 1):
19,24,2,17,28,11,29,25,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
var swapped : bool = false
19,2,17,24,11,28,25,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
for j in range(_array.size() - i - 1):
2,17,19,11,24,25,28,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
if _array[j].compare(_array[j + 1]) > 0:
2,17,11,19,24,25,28,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
var tmp = _array[j]
2,11,17,19,24,25,28,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
_array[j] = _array[j + 1]
</pre>
_array[j + 1] = tmp
swapped = true
if not swapped:
break
return
 
</syntaxhighlight>
This approach is slow though. To sort array of primitive types use Array.sort() method instead.
To sort array of objects you can use Array.sort_custom(func : Callable) method.
 
=={{header|Go}}==
Line 3,371 ⟶ 4,312:
 
For the most part, bubble sort works against J's strengths. However, once a single pass has been implemented as a list operation, <code>^:_</code> tells J to repeat this until the result stops changing.
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn bubble_sort<T>(anon mut array: [T]) {
mut item_count = array.size()
mut has_changed = true
while item_count > 1 and has_changed {
has_changed = true
item_count--
for i in 0..item_count {
if array[i] > array[i + 1] {
let temp = array[i]
array[i] = array[i + 1]
array[i + 1] = temp
has_changed = true
}
}
}
}
 
 
fn main() {
mut array = [7, 8, 9, 6, 5, 3, 1, 10, 4, 2]
println("{}", array)
bubble_sort(array)
println("{}", array)
}
</syntaxhighlight>
 
=={{header|Janet}}==
Line 3,583 ⟶ 4,552:
{bubblesort {A.new 0 3 86 20 27 67 31 16 37 42 8 47 7 84 5 29}}
-> [0,3,5,7,8,16,20,27,29,31,37,42,47,67,84,86]
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
itemCount = 20
dim item(itemCount)
for i = 1 to itemCount
item(i) = int(rnd(1) * 100)
next i
print "Before Sort"
for i = 1 to itemCount
print item(i)
next i
print: print
counter = itemCount
do
hasChanged = 0
for i = 1 to counter - 1
if item(i) > item(i + 1) then
temp = item(i)
item(i) = item(i + 1)
item(i + 1) = temp
hasChanged = 1
end if
next i
counter =counter -1
loop while hasChanged = 1
print "After Sort"
for i = 1 to itemCount
print item(i)
next i
end
</syntaxhighlight>
 
Line 3,891 ⟶ 4,828:
 
1 2 3 4 5 6 7 8 9</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
bubble_sort(u) := block(
[n: length(u), swapped: true, temp],
while swapped do (
swapped: false,
for i: 1 thru n - 1 do (
if u[i] > u[i + 1] then (
temp: u[i],
u[i]: u[i + 1],
u[i + 1]: temp,
swapped: true
)
)
),
u
);
/* Example */
/* sample:[3,65,6,24,24,89,2,59,6]$
bubble_sort(%);
[2,3,6,6,24,24,59,65,89]
*/
</syntaxhighlight>
 
=={{header|MAXScript}}==
Line 4,429 ⟶ 5,390:
END Bubble.
</syntaxhighlight>
 
{{Out}}
<pre>Before sorting:
1 10 2 5 -1 5 -19 4 23 0
After sorting:
-19 -1 0 1 2 4 5 5 10 23
</pre>
 
=={{header|Objeck}}==
Line 4,872 ⟶ 5,840:
// ...
bubble_sort(list);</syntaxhighlight>
 
=={{header|Pebble}}==
<syntaxhighlight lang="pebble">;bubble sort example for x86 DOS
;compiles to 549 bytes com file
 
program examples\bubble
 
data
 
int sorting[0]
int index[0]
int size[10]
int temp1[0]
int temp2[0]
 
int@ list[10]
 
begin
 
call fill
call sort
call output
 
pause
kill
 
end
 
sub fill
 
0 [index]
 
label loopfill
 
echo "Enter value #" \
echo [index]
echo ": " \
 
input @list{[index]}
 
+1 [index]
 
if [index] < [size] then loopfill
 
ret
 
sub sort
 
label loopsort
 
0 [sorting]
 
0 [index]
 
label process
 
[temp1] = [index] + 1
 
if @list{[index]} > @list{[temp1]} then
 
[temp2] = @list{[index]}
 
@list{[index]} = @list{[temp1]}
@list{[temp1]} = [temp2]
 
[sorting] = 1
 
endif
 
+1 [index]
 
if [index] < [size] - 1 then process
 
if [sorting] = 1 then loopsort
 
ret
 
sub output
 
0 [index]
 
label loopoutput
 
echo [index]
echo " : " \
echo @list{[index]}
crlf
 
+1 [index]
 
if [index] < [size] then loopoutput
 
ret</syntaxhighlight>
 
=={{header|Perl}}==
Line 5,094 ⟶ 6,155:
[1,3,2,4,5,4,6,8,9]
[1,2,3,4,4,5,6,8,9]</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure bubbleSort(Array a(1))
Protected i, itemCount, hasChanged
itemCount = ArraySize(a())
Repeat
hasChanged = #False
itemCount - 1
For i = 0 To itemCount
If a(i) > a(i + 1)
Swap a(i), a(i + 1)
hasChanged = #True
EndIf
Next
Until hasChanged = #False
EndProcedure</syntaxhighlight>
 
=={{header|Python}}==
Line 5,293 ⟶ 6,337:
}
}</syntaxhighlight>
 
=={{header|REALbasic}}==
 
Sorts an array of Integers
 
<syntaxhighlight lang="vb">
Dim sortable() As Integer = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sortable.Shuffle() ' sortable is now randomized
Dim swapped As Boolean
Do
Dim index, bound As Integer
bound = sortable.Ubound
 
While index < bound
If sortable(index) > sortable(index + 1) Then
Dim s As Integer = sortable(index)
sortable.Remove(index)
sortable.Insert(index + 1, s)
swapped = True
End If
index = index + 1
Wend
Loop Until Not swapped
'sortable is now sorted
</syntaxhighlight>
 
=={{header|REXX}}==
Line 5,661 ⟶ 6,679:
end
</syntaxhighlight>
 
=={{header|RPL}}==
The bubble algorithm is slow by construction, but since its RPL implementation uses basic stack handling, it is actually quicker than algorithms that work directly on the array.
{{works with|Halcyon Calc|4.2.7}}
≪ '''IF''' DUP SIZE 2 ≥ '''THEN'''
LIST→ → len
≪ len 1 '''FOR''' n
1 n 1 - '''START'''
'''IF''' DUP2 > '''THEN''' SWAP '''END'''
n ROLLD
'''NEXT'''
n ROLLD
-1 '''STEP'''
len →LIST
'''END''' ≫
 
=={{header|Ruby}}==
Line 5,689 ⟶ 6,723:
p ary
# => [3, 4, 6, 6, 8, 23, 78]</syntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">siz = 100
dim data$(siz)
unSorted = 1
 
WHILE unSorted
unSorted = 0
FOR i = 1 TO siz -1
IF data$(i) > data$(i + 1) THEN
tmp = data$(i)
data$(i) = data$(i + 1)
data$(i + 1) = tmp
unSorted = 1
END IF
NEXT
WEND</syntaxhighlight>
 
=={{header|Rust}}==
Line 6,441 ⟶ 7,458:
=={{header|Standard ML}}==
Assumes a list of integers.
<syntaxhighlight lang="sml">
<pre>
fun bubble_select [] = []
| bubble_select [a] = [a]
Line 6,449 ⟶ 7,466:
fun bubblesort [] = []
| bubblesort (x::xs) =bubble_select (x::(bubblesort xs))
</syntaxhighlight>
</pre>
 
=={{header|Stata}}==
Line 6,587 ⟶ 7,604:
 
Idiomatic code uses the builtin <code>lsort</code> instead, which is a stable O(''n'' log ''n'') sort.
 
=={{header|TI-83 BASIC}}==
Input your data into L<sub>1</sub> and run this program to organize it.
:L<sub>1</sub>→L<sub>2</sub>
:1+dim(L<sub>2</sub>)→N
:For(D,1,dim(L<sub>2</sub>))
:N-1→N
:0→I
:For(C,1,dim(L<sub>2</sub>)-2)
:For(A,dim(L<sub>2</sub>)-N+1,dim(L<sub>2</sub>)-1)
:If L<sub>2</sub>(A)&gt;L<sub>2</sub>(A+1)
:Then
:1→I
:L<sub>2</sub>(A)→B
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
:B→L<sub>2</sub>(A+1)
:End
:End
:End
:If I=0
:Goto C
:End
:Lbl C
:If L<sub>2</sub>(1)&gt;L<sub>2</sub>(2)
:Then
:L<sub>2</sub>(1)→B
:L<sub>2</sub>(2)→L<sub>2</sub>(1)
:B→L<sub>2</sub>(2)
:End
:DelVar A
:DelVar B
:DelVar C
:DelVar D
:DelVar N
:DelVar I
:Return
 
[[wp:Odd-even sort|Odd-Even Bubble Sort]] (same IO):
:"ODD-EVEN"
:L<sub>1</sub>→L<sub>2</sub>(
:1+dim(L<sub>2</sub>)→N
:For(D,1,dim(L<sub>2</sub>))
:N-1→N
:0→O
:For(C,1,dim(L<sub>2</sub>)-2)
:For(A,dim(L<sub>2</sub>)-N+2,dim(L<sub>2</sub>)-1,2)
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1)
:Then
:1→O
:L<sub>2</sub>(A)→B
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
:B→L<sub>2</sub>(A+1)
:End
:End
:For(A,dim(L<sub>2</sub>)-N+1,dim(L<sub>2</sub>)-1,2)
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1)
:Then
:1→O
:L<sub>2</sub>(A)→B
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
:B→L<sub>2</sub>(A+1)
:End
:End
:End
:If O=0
:Goto C
:End
:Lbl C
:If L<sub>2</sub>(1)>L<sub>2</sub>(2)
:Then
:L<sub>2</sub>(1)→B
:L<sub>2</sub>(2)→L<sub>2</sub>(1)
:B→L<sub>2</sub>(2)
:End
:DelVar A
:DelVar B
:DelVar C
:DelVar D
:DelVar N
:DelVar O
:Return
 
Implementation of the pseudo code given at the top of the page. Place data to be sorted in L<sub>1</sub>
:dim(L<sub>1</sub>)→D
:Repeat C=0
:0→C
:D–1→D
:For(I,1,D)
:If L<sub>1</sub>(I)>L<sub>1</sub>(I+1):Then
:L<sub>1</sub>(I)→C
:L<sub>1</sub>(I+1)→L<sub>1</sub>(I)
:C→L<sub>1</sub>(I+1)
:1→C
:End
:End
:End
:L<sub>1</sub>
 
=={{header|Toka}}==
Line 6,747 ⟶ 7,667:
return %list;
}</syntaxhighlight>
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">PRINT "Bubble sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
PROC _Bubblesort (n)
PROC _ShowArray (n)
PRINT
END
_Bubblesort PARAM(1) ' Bubble sort
LOCAL (2)
 
DO
b@ = 0
FOR c@ = 1 TO a@-1
IF @(c@-1) > @(c@) THEN PROC _Swap (c@, c@-1) : b@ = c@
NEXT
a@ = b@
UNTIL b@ = 0
LOOP
 
RETURN
_Swap PARAM(2) ' Swap two array elements
PUSH @(a@)
@(a@) = @(b@)
@(b@) = POP()
RETURN
_InitArray ' Init example array
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
FOR i = 0 TO 9
@(i) = POP()
NEXT
RETURN (i)
_ShowArray PARAM (1) ' Show array subroutine
FOR i = 0 TO a@-1
PRINT @(i),
NEXT
PRINT
RETURN</syntaxhighlight>
 
=={{header|Unicon}}==
Line 6,872 ⟶ 7,742:
-4 -1 0 1 2 3 5 6 8 101
</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function bubble_sort(s As Variant) As Variant
Dim tmp As Variant
Dim changed As Boolean
For j = UBound(s) To 1 Step -1
changed = False
For i = 1 To j - 1
If s(i) > s(i + 1) Then
tmp = s(i)
s(i) = s(i + 1)
s(i + 1) = tmp
changed = True
End If
Next i
If Not changed Then
Exit For
End If
Next j
bubble_sort = s
End Function
Public Sub main()
s = [{4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}]
Debug.Print "Before: "
Debug.Print Join(s, ", ")
Debug.Print "After: "
Debug.Print Join(bubble_sort(s), ", ")
End Sub</syntaxhighlight>{{out}}
<pre>Before:
4, 15, delta, 2, -31, 0, alfa, 19, gamma, 2, 13, beta, 782, 1
After:
-31, 0, 1, 2, 2, 4, 13, 15, 19, 782, alfa, beta, delta, gamma</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=====
<syntaxhighlight 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
</syntaxhighlight>
 
=====Invocation=====
<syntaxhighlight lang="vb">
dim a
a = array( "great eastern", "roe", "stirling", "albany", "leach")
wscript.echo join(a,", ")
bubbleSort a
wscript.echo join(a,", ")
</syntaxhighlight>
 
{{out}}
<pre>
great eastern, roe, stirling, albany, leach
albany, great eastern, leach, roe, stirling
</pre>
 
=={{header|Visual Basic .NET}}==
'''Platform:''' [[.NET]]
 
{{works with|Visual Basic .NET|9.0+}}
<syntaxhighlight lang="vbnet">Do Until NoMoreSwaps = True
NoMoreSwaps = True
For Counter = 1 To (NumberOfItems - 1)
If List(Counter) > List(Counter + 1) Then
NoMoreSwaps = False
Temp = List(Counter)
List(Counter) = List(Counter + 1)
List(Counter + 1) = Temp
End If
Next
NumberOfItems = NumberOfItems - 1
Loop</syntaxhighlight>
 
=={{header|V (Vlang)}}==
Line 7,013 ⟶ 7,778:
=={{header|Wren}}==
Based on the pseudo-code in the Wikipedia article.
<syntaxhighlight lang="ecmascriptwren">var bubbleSort = Fn.new { |a|
var n = a.count
if (n < 2) return
Line 7,030 ⟶ 7,795:
}
 
var asarray = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
for (a in asarray) {
System.print("Before: %(a)")
bubbleSort.call(a)
Line 7,133 ⟶ 7,898:
" .Pabcdeefghiiijklmnoooqrstuuvwxyz"
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Animated sort.
// Original idea by William Tang, obtained from MicroHobby 25 Years (https://microhobby.speccy.cz/zxsf/MH-25Years.pdf)
 
clear screen
 
n=15 : m=18 : y=9 : t$=chr$(17)+chr$(205)+chr$(205)
dim p(n), p$(n)
 
for x=1 TO n
p(x)=ran(15)+1
p$(x)=str$(p(x),"##.######")
print at(0,x) p$(x)
next x
 
for j=1 to n-1
for i=j+1 to n
l=n+j-i+1
if p(j) > p(l) then
print color("yellow","red") at(0,j) p$(j)
if l<>m then
for x=m to l step sig(l-m): print at(18,x) t$ : print at (18,x+sig(m-l)) " " : pause .02 : next x
end if
for x=17 TO y step -1 : print at(x,l) t$+" " : pause .02 : next x
for x=0 TO 10 : print at(x,l) " "+p$(l)+t$ : pause .02 : next x
for x=l TO j STEP -1 : print at(11,x) p$(l)+t$ : print at(11,x+1) " " : pause .02 : next x
print at(0,j) " "
for x=j+1 TO l-1 : print color("yellow","red") at(0,x) p$(j) : pause .02 : print at(0,x) p$(x) : pause .02 : next x
print at(0,l) p$(j)
for x=10 TO 0 step -1 : print at(x,j) p$(l)+t$+" " : pause .02 : next x
for x=y TO 17 : print at(x,j) " "+t$ : pause .02 : next x
m=j
t=p(l) : tem$=p$(l)
p(l)=p(j) : p$(l)=p$(j)
p(j)=t : p$(j)=tem$
end if
pause .02
next i
next j
 
for x=m TO 18 : print at(18,x-1) " " : print at(18,x) t$ : pause .02 : next x
</syntaxhighlight>
 
=={{header|Yorick}}==
Line 7,216 ⟶ 7,938:
{{out}}
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre>
 
=={{header|ZX Spectrum Basic}}==
<syntaxhighlight lang="zxbasic">5000 CLS
5002 LET a$="": FOR f=1 TO 64: LET a$=a$+CHR$ (32+INT (RND*96)): NEXT f
5004 PRINT a$; AT 10,0;"ZigZag BubbleSORT"
5010 LET la=LEN a$
5011 LET i=1: LET u=0
5020 LET d=0: LET p=(u=0)-(u=1)
5021 LET l=(i AND u=0)+(la-i+u AND u=1)
5030 IF u=0 THEN IF a$(l+1)>=a$(l) THEN GO TO 5050
5031 IF u=1 THEN IF a$(l-1)<=a$(l) THEN GO TO 5050
5040 LET d=1
5042 LET t$=a$(l+p)
5043 LET a$(l+p)=a$(l)
5044 LET a$(l)=t$
5050 LET l=l+p
5051 PRINT AT 10,21;a$(l);AT 12,0;a$
5055 IF l<=la-i AND l>=i THEN GO TO 5023
5061 LET i=i+NOT u
5063 LET u=NOT u
5064 IF d AND i<la THEN GO TO 5020
5072 PRINT AT 12,0;a$
9000 STOP </syntaxhighlight>
 
The traditional solution:
 
<syntaxhighlight lang="zxbasic"> 10 LET siz=32
20 DIM d$(siz)
30 REM Populate d$
40 FOR n=1 TO siz: LET d$(n)=CHR$ (48+INT (RND*75)): NEXT n
50 PRINT d$
60 LET unSorted=0
70 FOR i=1 TO siz-1
80 IF d$(i)>d$(i+1) THEN LET t$=d$(i): LET d$(i)=d$(i+1): LET d$(i+1)=t$: LET unSorted=1
90 NEXT i
100 IF unSorted THEN LET siz=siz-1: GO TO 60
110 PRINT d$</syntaxhighlight>
 
{{omit from|GUISS}}
{{omit from|Tiny BASIC}}
23

edits