Sorting algorithms/Bubble sort: Difference between revisions

m
→‎{{header|Standard ML}}: fix syntax highlighting
m (→‎{{header|Standard ML}}: fix syntax highlighting)
 
(387 intermediate revisions by more than 100 users not shown)
Line 1:
{{task|Sorting Algorithms}}{{Sorting Algorithm}}
{{Sorting Algorithm}}
In this task, the goal is to sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
 
TheA &nbsp; '''bubble''' &nbsp; sort is generally considered to be the simplest sorting algorithm. Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses. Because of its abysmal O(n<sup>2</sup>) performance, it is not used often for large (or even medium-sized) datasets.
 
A &nbsp; '''bubble''' &nbsp; sort is also known as a &nbsp; '''sinking''' &nbsp; sort.
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them). Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass. A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
 
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
This can be expressed in pseudocode as follows (assuming 1-based indexing):
 
Because of its abysmal O(n<sup>2</sup>) performance, it is not used often for large (or even medium-sized) datasets.
 
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. &nbsp; If the first value is greater than the second, their positions are switched. &nbsp; Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them). &nbsp;
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass. &nbsp;
A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
 
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
'''repeat'''
'''if''' itemCount <= 1
'''return'''
hasChanged := false
'''decrement''' itemCount
Line 16 ⟶ 26:
'''until''' hasChanged = '''false'''
 
 
For more information see the article on [[wp:Bubble_sort|Wikipedia]].
;Task:
Sort an array of elements using the bubble sort algorithm. &nbsp; The elements must have a total order and the index of the array can be of any discrete type. &nbsp; For languages where this is not possible, sort an array of integers.
 
 
;References:
* The article on [[wp:Bubble_sort|Wikipedia]].
* Dance [http://www.youtube.com/watch?v=lyZQPjUT5B4&feature=youtu.be interpretation].
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F bubble_sort(&seq)
V changed = 1B
L changed == 1B
changed = 0B
L(i) 0 .< seq.len - 1
I seq[i] > seq[i + 1]
swap(&seq[i], &seq[i + 1])
changed = 1B
 
V testset = Array(0.<100)
V testcase = copy(testset)
random:shuffle(&testcase)
assert(testcase != testset)
bubble_sort(&testcase)
assert(testcase == testset)</syntaxhighlight>
 
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
The program uses also HLASM structured macros (DO,ENDDO,IF,ELSE,ENDIF) for readability and two ASSIST/360 macros (XDECO,XPRNT) to keep the code as short as possible.
<syntaxhighlight lang="360 assembly">* Bubble Sort 01/11/2014 & 23/06/2016
BUBBLE CSECT
USING BUBBLE,R13,R12 establish base registers
SAVEAREA B STM-SAVEAREA(R15) skip savearea
DC 17F'0' my savearea
STM STM R14,R12,12(R13) save calling context
ST R13,4(R15) link mySA->prevSA
ST R15,8(R13) link prevSA->mySA
LR R13,R15 set mySA & set 4K addessability
LA R12,2048(R13) .
LA R12,2048(R12) set 8K addessability
L RN,N n
BCTR RN,0 n-1
DO UNTIL=(LTR,RM,Z,RM) repeat ------------------------+
LA RM,0 more=false |
LA R1,A @a(i) |
LA R2,4(R1) @a(i+1) |
LA RI,1 i=1 |
DO WHILE=(CR,RI,LE,RN) for i=1 to n-1 ------------+ |
L R3,0(R1) a(i) | |
IF C,R3,GT,0(R2) if a(i)>a(i+1) then ---+ | |
L R9,0(R1) r9=a(i) | | |
L R3,0(R2) r3=a(i+1) | | |
ST R3,0(R1) a(i)=r3 | | |
ST R9,0(R2) a(i+1)=r9 | | |
LA RM,1 more=true | | |
ENDIF , end if <---------------+ | |
LA RI,1(RI) i=i+1 | |
LA R1,4(R1) next a(i) | |
LA R2,4(R2) next a(i+1) | |
ENDDO , end for <------------------+ |
ENDDO , until not more <---------------+
LA R3,PG pgi=0
LA RI,1 i=1
DO WHILE=(C,RI,LE,N) do i=1 to n -------+
LR R1,RI i |
SLA R1,2 . |
L R2,A-4(R1) a(i) |
XDECO R2,XDEC edit a(i) |
MVC 0(4,R3),XDEC+8 output a(i) |
LA R3,4(R3) pgi=pgi+4 |
LA RI,1(RI) i=i+1 |
ENDDO , end do <-----------+
XPRNT PG,L'PG print buffer
L R13,4(0,R13) restore caller savearea
LM R14,R12,12(R13) restore context
XR R15,R15 set return code to 0
BR R14 return to caller
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1'
DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74'
N DC A((N-A)/L'A) number of items of a *
PG DC CL80' '
XDEC DS CL12
LTORG
REGEQU
RI EQU 6 i
RN EQU 7 n-1
RM EQU 8 more
END BUBBLE</syntaxhighlight>
{{out}}
<pre>
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
</pre>
=={{header|6502 Assembly}}==
Code can be copied and pasted into Easy6502. Make sure you set the monitor to $1200 and check the check box to view it in action. Bubble Sort's infamous reputation is very well-deserved as this is going to take a few minutes to finish. This example takes a reverse identity table (where the 0th entry is $FF, the first is $FE, and so on) and sorts them in ascending order. Slowly. And the program might not run unless its tab is active in your browser. I'd play Game Boy to pass the time. ;)
<syntaxhighlight lang="6502asm">define z_HL $00
define z_L $00
define z_H $01
define temp $02
define temp2 $03
 
set_table:
dex
txa
sta $1200,y
iny
bne set_table ;stores $ff at $1200, $fe at $1201, etc.
 
lda #$12
sta z_H
lda #$00
sta z_L
 
lda #0
tax
tay ;clear regs
 
JSR BUBBLESORT
BRK
 
BUBBLESORT:
lda (z_HL),y
sta temp
iny ;look at the next item
lda (z_HL),y
dey ;go back 1 to the "current item"
sta temp2
cmp temp
bcs doNothing
 
;we had to re-arrange an item.
lda temp
iny
sta (z_HL),y ;store the higher value at base+y+1
inx ;sort count. If zero at the end, we're done.
dey
lda temp2
sta (z_HL),y ;store the lower value at base+y
 
doNothing:
iny
cpy #$ff
bne BUBBLESORT
ldy #0
txa ;check the value of the counter
beq DoneSorting
ldx #0 ;reset the counter
jmp BUBBLESORT
DoneSorting:
rts</syntaxhighlight>
{{out}}
<pre>
1200: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
1210: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
1220: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
1230: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
1240: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
1250: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
1260: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
1270: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
1280: 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
1290: 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
12a0: a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
12b0: b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
12c0: c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
12d0: d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
12e0: e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
12f0: f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
</pre>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program bubbleSort64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeConstantesARM64.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
TableNumber: .quad 1,3,6,2,5,9,10,8,4,7
#TableNumber: .quad 10,9,8,7,6,5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 8
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrTableNumber // address number table
mov x1,0
mov x2,NBELEMENTS // number of élements
bl bubbleSort
ldr x0,qAdrTableNumber // address number table
bl displayTable
ldr x0,qAdrTableNumber // address number table
mov x1,NBELEMENTS // number of élements
bl isSorted // control sort
cmp x0,1 // sorted ?
beq 1f
ldr x0,qAdrszMessSortNok // no !! error sort
bl affichageMess
b 100f
1: // yes
ldr x0,qAdrszMessSortOk
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrTableNumber: .quad TableNumber
qAdrszMessSortOk: .quad szMessSortOk
qAdrszMessSortNok: .quad szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the number of elements > 0 */
/* x0 return 0 if not sorted 1 if sorted */
isSorted:
stp x2,lr,[sp,-16]! // save registers
stp x3,x4,[sp,-16]! // save registers
mov x2,0
ldr x4,[x0,x2,lsl 3]
1:
add x2,x2,1
cmp x2,x1
bge 99f
ldr x3,[x0,x2, lsl 3]
cmp x3,x4
blt 98f
mov x4,x3
b 1b
98:
mov x0,0 // not sorted
b 100f
99:
mov x0,1 // sorted
100:
ldp x3,x4,[sp],16 // restaur 2 registers
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* bubble sort */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the first element */
/* x2 contains the number of element */
bubbleSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
sub x2,x2,1 // compute i = n - 1
add x8,x1,1
1: // start loop 1
mov x3,x1 // start index
mov x9,0
sub x7,x2,1
2: // start loop 2
add x4,x3,1
ldr x5,[x0,x3,lsl 3] // load value A[j]
ldr x6,[x0,x4,lsl 3] // load value A[j+1]
cmp x6,x5 // compare value
bge 3f
str x6,[x0,x3,lsl 3] // if smaller inversion
str x5,[x0,x4,lsl 3]
mov x9,1 // top table not sorted
3:
add x3,x3,1 // increment index j
cmp x3,x7 // end ?
ble 2b // no -> loop 2
cmp x9,0 // table sorted ?
beq 100f // yes -> end
 
sub x2,x2,1 // decrement i
cmp x2,x8 // end ?
bge 1b // no -> loop 1
100:
ldp x8,x9,[sp],16 // restaur 2 registers
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* x0 contains the address of table */
displayTable:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x2,x0 // table address
mov x3,0
1: // loop display table
ldr x0,[x2,x3,lsl 3]
ldr x1,qAdrsZoneConv
bl conversion10 // décimal conversion
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
add x3,x3,1
cmp x3,NBELEMENTS - 1
ble 1b
ldr x0,qAdrszCarriageReturn
bl affichageMess
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 
</syntaxhighlight>
=={{header|ACL2}}==
<syntaxhighlight lang="lisp">(defun bubble (xs)
(if (endp (rest xs))
(mv nil xs)
(let ((x1 (first xs))
(x2 (second xs)))
(if (> x1 x2)
(mv-let (_ ys)
(bubble (cons x1 (rest (rest xs))))
(declare (ignore _))
(mv t (cons x2 ys)))
(mv-let (has-changed ys)
(bubble (rest xs))
(mv has-changed (cons x1 ys)))))))
 
(defun bsort-r (xs limit)
(declare (xargs :measure (nfix limit)))
(if (zp limit)
xs
(mv-let (has-changed ys)
(bubble xs)
(if has-changed
(bsort-r ys (1- limit))
ys))))
 
(defun bsort (xs)
(bsort-r xs (len xs)))</syntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
PrintI(a(i))
OD
Put(']) PutE()
RETURN
 
PROC BubbleSort(INT ARRAY a INT size)
INT count,changed,i,tmp
 
count=size
IF count<=1 THEN RETURN FI
 
DO
changed=0
count==-1
FOR i=0 TO count-1
DO
IF a(i)>a(i+1) THEN
tmp=a(i) a(i)=a(i+1) a(i+1)=tmp
changed=1
FI
OD
UNTIL changed=0
OD
RETURN
 
PROC Test(INT ARRAY a INT size)
PrintE("Array before sort:")
PrintArray(a,size)
BubbleSort(a,size)
PrintE("Array after sort:")
PrintArray(a,size)
PutE()
RETURN
 
PROC Main()
INT ARRAY
a(10)=[1 4 65535 0 3 7 4 8 20 65530],
b(21)=[10 9 8 7 6 5 4 3 2 1 0
65535 65534 65533 65532 65531
65530 65529 65528 65527 65526],
c(8)=[101 102 103 104 105 106 107 108],
d(12)=[1 65535 1 65535 1 65535 1
65535 1 65535 1 65535]
Test(a,10)
Test(b,21)
Test(c,8)
Test(d,12)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bubble_sort.png Screenshot from Atari 8-bit computer]
<pre>
Array before sort:
[1 4 -1 0 3 7 4 8 20 -6]
Array after sort:
[-6 -1 0 1 3 4 4 7 8 20]
 
Array before sort:
[10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]
Array after sort:
[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
 
Array before sort:
[101 102 103 104 105 106 107 108]
Array after sort:
[101 102 103 104 105 106 107 108]
 
Array before sort:
[1 -1 1 -1 1 -1 1 -1 1 -1 1 -1]
Array after sort:
[-1 -1 -1 -1 -1 -1 1 1 1 1 1 1]
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">public function bubbleSort(toSort:Array):Array
{
var changed:Boolean = false;
Line 41 ⟶ 503:
return toSort;
}</langsyntaxhighlight>
 
=={{header|Ada}}==
{{works with|GCC|4.1.2}}
 
<langsyntaxhighlight lang="ada">generic
type Element is private;
with function "=" (E1, E2 : Element) return Boolean is <>;
Line 89 ⟶ 551:
end loop;
New_Line;
end Main;</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|A60}}
<syntaxhighlight lang="algol60">begin
comment Sorting algorithms/Bubble sort - Algol 60;
integer nA;
nA:=20;
begin
integer array A[1:20];
procedure bubblesort(lb,ub);
value lb,ub; integer lb,ub;
begin
integer i;
boolean swapped;
swapped :=true;
for i:=1 while swapped do begin
swapped:=false;
for i:=lb step 1 until ub-1 do if A[i]>A[i+1] then begin
integer temp;
temp :=A[i];
A[i] :=A[i+1];
A[i+1]:=temp;
swapped:=true
end
end
end bubblesort;
procedure inittable(lb,ub);
value lb,ub; integer lb,ub;
begin
integer i;
for i:=lb step 1 until ub do A[i]:=entier(rand*100)
end inittable;
procedure writetable(lb,ub);
value lb,ub; integer lb,ub;
begin
integer i;
for i:=lb step 1 until ub do outinteger(1,A[i]);
outstring(1,"\n")
end writetable;
 
inittable(1,nA);
writetable(1,nA);
bubblesort(1,nA);
writetable(1,nA)
end
end </syntaxhighlight>
{{out}}
<pre>
70 92 61 64 74 5 89 52 38 91 59 57 51 34 81 27 57 51 32 74
5 27 32 34 38 51 51 52 57 57 59 61 64 70 74 74 81 89 91 92
</pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">MODE DATA = INT;
PROC swap = (REF[]DATA slice)VOID:
(
Line 123 ⟶ 641:
sort(random);
printf(($"After: "10(g(3))l$,random))
)</langsyntaxhighlight>
{{out}}
Output:
<pre>
Before: +1 +6 +3 +5 +2 +9 +8 +4 +7 +0
After: +0 +1 +2 +3 +4 +5 +6 +7 +8 +9
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
% As algol W does not allow overloading, we have to have type-specific %
% sorting procedures - this bubble sorts an integer array %
% as there is no way for the procedure to determine the array bounds, we %
% pass the lower and upper bounds in lb and ub %
procedure bubbleSortIntegers( integer array item( * )
; integer value lb
; integer value ub
) ;
begin
integer lower, upper;
 
lower := lb;
upper := ub;
 
while
begin
logical swapped;
upper := upper - 1;
swapped := false;
for i := lower until upper
do begin
if item( i ) > item( i + 1 )
then begin
integer val;
val := item( i );
item( i ) := item( i + 1 );
item( i + 1 ) := val;
swapped := true;
end if_must_swap ;
end for_i ;
swapped
end
do begin end;
end bubbleSortIntegers ;
 
begin % test the bubble sort %
integer array data( 1 :: 10 );
 
procedure writeData ;
begin
write( data( 1 ) );
for i := 2 until 10 do writeon( data( i ) );
end writeData ;
 
% initialise data to unsorted values %
integer dPos;
dPos := 1;
for i := 16, 2, -6, 9, 90, 14, 0, 23, 8, 9
do begin
data( dPos ) := i;
dPos := dPos + 1;
end for_i ;
 
i_w := 3; s_w := 1; % set output format %
writeData;
bubbleSortIntegers( data, 1, 10 );
writeData;
end test
end.</syntaxhighlight>
{{out}}
<pre>
16 2 -6 9 90 14 0 23 8 9
-6 0 2 8 9 9 14 16 23 90
</pre>
 
=={{header|AppleScript}}==
 
In AppleScript, the time taken to set and check a "has changed" boolean repeatedly over the greater part of the sort generally matches or exceeds any time it may save. A more effective optimisation, since the greater value in any pair also takes part in the following comparison, is to keep the greater value in a variable and only fetch one value from the list for the next comparison.
 
<syntaxhighlight lang="applescript">-- In-place bubble sort.
on bubbleSort(theList, l, r) -- Sort items l thru r of theList.
set listLen to (count theList)
if (listLen < 2) then return
-- Convert negative and/or transposed range indices.
if (l < 0) then set l to listLen + l + 1
if (r < 0) then set r to listLen + r + 1
if (l > r) then set {l, r} to {r, l}
-- The list as a script property to allow faster references to its items.
script o
property lst : theList
end script
set lPlus1 to l + 1
repeat with j from r to lPlus1 by -1
set lv to o's lst's item l
-- Hereafter lv is only set when necessary and from rv rather than from the list.
repeat with i from lPlus1 to j
set rv to o's lst's item i
if (lv > rv) then
set o's lst's item (i - 1) to rv
set o's lst's item i to lv
else
set lv to rv
end if
end repeat
end repeat
return -- nothing.
end bubbleSort
property sort : bubbleSort
 
-- Demo:
local aList
set aList to {61, 23, 11, 55, 1, 94, 71, 98, 70, 33, 29, 77, 58, 95, 2, 52, 68, 29, 27, 37, 74, 38, 45, 73, 10}
sort(aList, 1, -1) -- Sort items 1 thru -1 of aList.
return aList</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{1, 2, 10, 11, 23, 27, 29, 29, 33, 37, 38, 45, 52, 55, 58, 61, 68, 70, 71, 73, 74, 77, 94, 95, 98}</syntaxhighlight>
 
=={{header|Arendelle}}==
 
A function that returns a sorted version of it's x input
 
<pre>&lt; x &gt; ( i , 0 )
 
( sjt , 1; 0; 0 ) // swapped:0 / j:1 / temp:2
 
[ @sjt = 1 ,
 
( sjt , 0 )
( sjt[ 1 ] , +1 )
 
( i , 0 )
 
[ @i &lt; @x? - @sjt[ 1 ],
 
{ @x[ @i ] &lt; @x[ @i + 1 ],
 
( sjt[ 2 ] , @x[ @i ] )
( x[ @i ] , @x[ @i + 1 ] )
( x[ @i + 1 ] , @sjt[ 2 ] )
( sjt , 1 )
}
 
( i , +1 )
]
]
 
( return , @x )</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
 
/* ARM assembly Raspberry PI */
/* program bubbleSort.s */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
#TableNumber: .int 1,3,6,2,5,9,10,8,4,7
TableNumber: .int 10,9,8,7,6,5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 4
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
1:
ldr r0,iAdrTableNumber @ address number table
mov r1,#0
mov r2,#NBELEMENTS @ number of élements
bl bubbleSort
ldr r0,iAdrTableNumber @ address number table
bl displayTable
ldr r0,iAdrTableNumber @ address number table
mov r1,#NBELEMENTS @ number of élements
bl isSorted @ control sort
cmp r0,#1 @ sorted ?
beq 2f
ldr r0,iAdrszMessSortNok @ no !! error sort
bl affichageMess
b 100f
2: @ yes
ldr r0,iAdrszMessSortOk
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResult: .int sMessResult
iAdrTableNumber: .int TableNumber
iAdrszMessSortOk: .int szMessSortOk
iAdrszMessSortNok: .int szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the number of elements > 0 */
/* r0 return 0 if not sorted 1 if sorted */
isSorted:
push {r2-r4,lr} @ save registers
mov r2,#0
ldr r4,[r0,r2,lsl #2]
1:
add r2,#1
cmp r2,r1
movge r0,#1
bge 100f
ldr r3,[r0,r2, lsl #2]
cmp r3,r4
movlt r0,#0
blt 100f
mov r4,r3
b 1b
100:
pop {r2-r4,lr}
bx lr @ return
/******************************************************************/
/* bubble sort */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the first element */
/* r2 contains the number of element */
bubbleSort:
push {r1-r9,lr} @ save registers
sub r2,r2,#1 @ compute i = n - 1
add r8,r1,#1
1: @ start loop 1
mov r3,r1 @ start index
mov r9,#0
sub r7,r2,#1
2: @ start loop 2
add r4,r3,#1
ldr r5,[r0,r3,lsl #2] @ load value A[j]
ldr r6,[r0,r4,lsl #2] @ load value A[j+1]
cmp r6,r5 @ compare value
strlt r6,[r0,r3,lsl #2] @ if smaller inversion
strlt r5,[r0,r4,lsl #2]
movlt r9,#1 @ top table not sorted
add r3,#1 @ increment index j
cmp r3,r7 @ end ?
ble 2b @ no -> loop 2
cmp r9,#0 @ table sorted ?
beq 100f @ yes -> end
 
sub r2,r2,#1 @ decrement i
cmp r2,r8 @ end ?
bge 1b @ no -> loop 1
100:
pop {r1-r9,lr}
bx lr @ return
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* r0 contains the address of table */
displayTable:
push {r0-r3,lr} @ save registers
mov r2,r0 @ table address
mov r3,#0
1: @ loop display table
ldr r0,[r2,r3,lsl #2]
ldr r1,iAdrsZoneConv @
bl conversion10 @ décimal conversion
ldr r0,iAdrsMessResult
ldr r1,iAdrsZoneConv @ insert conversion
bl strInsertAtCharInc
bl affichageMess @ display message
add r3,#1
cmp r3,#NBELEMENTS - 1
ble 1b
ldr r0,iAdrszCarriageReturn
bl affichageMess
100:
pop {r0-r3,lr}
bx lr
iAdrsZoneConv: .int sZoneConv
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">bubbleSort: function [items][
len: size items
loop len [j][
i: 1
while [i =< len-j] [
if items\[i] < items\[i-1] [
tmp: items\[i]
items\[i]: items\[i-1]
items\[i-1]: tmp
]
i: i + 1
]
]
items
]
 
print bubbleSort [3 1 2 8 5 7 9 4 6]</syntaxhighlight>
 
{{out}}
 
<pre>1 2 3 4 5 6 7 8 9</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">var =
(
dog
Line 163 ⟶ 1,011:
sorted .= array%A_Index% . "`n"
Return sorted
}</langsyntaxhighlight>
 
=={{header|AWK}}==
Sort the standard input and print it to standard output.
<langsyntaxhighlight lang="awk">{ # read every line into an array
line[NR] = $0
}
Line 186 ⟶ 1,034:
print line[i]
}
}</langsyntaxhighlight>
 
GNU awk contains built in functions for sorting, but POSIX
Awk doesn't. Here is a generic bubble sort() implementation that you
can copy/paste to your
Awk programs. Adapted from the above example. Note that it
is not possible to return arrays from Awk functions so the
array is "edited in place". The extra parameters passed in
function's argument list is a well known trick to define local
variables.
 
<syntaxhighlight lang="awk">
# Test this example file from command line with:
#
# awk -f file.awk /dev/null
#
# Code by Jari Aalto <jari.aalto A T cante net>
# Licensed and released under GPL-2+, see http://spdx.org/licenses
 
function alen(array, dummy, len) {
for (dummy in array)
len++;
 
return len;
}
 
function sort(array, haschanged, len, tmp, i)
{
len = alen(array)
haschanged = 1
 
while ( haschanged == 1 )
{
haschanged = 0
 
for (i = 1; i <= len - 1; i++)
{
if (array[i] > array[i+1])
{
tmp = array[i]
array[i] = array[i + 1]
array[i + 1] = tmp
haschanged = 1
}
}
}
}
 
# An Example. Sorts array to order: b, c, z
{
array[1] = "c"
array[2] = "z"
array[3] = "b"
sort(array)
print array[1] " " array[2] " " array[3]
exit
}
</syntaxhighlight>
 
=={{header|bash}}==
 
I hope to see vastly improved versions of bubble_sort.
 
<syntaxhighlight lang="bash">
$ function bubble_sort() {
local a=("$@")
local n
local i
local j
local t
ft=(false true)
n=${#a[@]} # array length
i=n
while ${ft[$(( 0 < i ))]}
do
j=0
while ${ft[$(( j+1 < i ))]}
do
if ${ft[$(( a[j+1] < a[j] ))]}
then
t=${a[j+1]}
a[j+1]=${a[j]}
a[j]=$t
fi
t=$(( ++j ))
done
t=$(( --i ))
done
echo ${a[@]}
}
 
> > > > > > > > > > > > > > > > > > > > > > > > > $ # this line output from bash
$ bubble_sort 3 2 8
2 3 8
$ # create an array variable
$ a=(2 45 83 89 1 82 69 88 112 99 0 82 58 65 782 74 -31 104 4 2)
$ bubble_sort ${a[@]}
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
$ b=($( bubble_sort ${a[@]} ) )
$ echo ${#b[@]}
20
$ echo ${b[@]}
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
$
</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="basic">0 GOSUB 7 : IC = I%(0)
1 FOR HC = -1 TO 0
2 LET IC = IC - 1
3 FOR I = 1 TO IC
4 IF I%(I) > I%(I + 1) THEN H = I%(I) : I%(I) = I%(I + 1) : I%(I + 1) = H : HC = -2 * (IC > 1)
5 NEXT I, HC
6 GOSUB 9 : END
7 DIM I%(18000) : I%(0) = 50
8 FOR I = 1 TO I%(0) : I%(I) = INT (RND(1) * 65535) - 32767 : NEXT
9 FOR I = 1 TO I%(0) : PRINT I%(I)" "; : NEXT I : PRINT : RETURN</syntaxhighlight>
 
==={{header|BaCon}}===
Numeric example:
<syntaxhighlight lang="bacon">LOCAL t[] = { 5, 7, 1, 3, 10, 2, 9, 4, 8, 6 }
total = 10
WHILE total > 1
FOR x = 0 TO total-1
IF t[x] > t[x+1] THEN SWAP t[x], t[x+1]
NEXT
DECR total
WEND
PRINT COIL$(10, STR$(t[_-1]))</syntaxhighlight>
{{out}}
<pre>1 2 3 4 5 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}}===
{{works with|BASIC256 }}
<syntaxhighlight lang="basic256">
Dim a(11): ordered=false
print "Original set"
For n = 0 to 9
a[n]=int(rand*20+1)
print a[n]+", ";
next n
#algorithm
while ordered=false
ordered=true
For n = 0 to 9
if a[n]> a[n+1] then
x=a[n]
a[n]=a[n+1]
a[n+1]=x
ordered=false
end if
next n
end while
 
print
print "Ordered set"
For n = 1 to 10
print a[n]+", ";
next n
</syntaxhighlight>
{{out}}(example)
<pre>
Original set
2, 10, 17, 13, 20, 14, 3, 17, 16, 16,
Ordered set
2, 3, 10, 13, 14, 16, 16, 17, 17, 20,
</pre>
 
==={{header|BBC BASIC}}===
The Bubble sort is very inefficient for 99% of cases. This routine uses a couple of 'tricks' to try and mitigate the inefficiency to a limited extent. Note that the array index is assumed to start at zero.
<syntaxhighlight lang="bbcbasic"> DIM test(9)
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
PROCbubblesort(test(), 10)
FOR i% = 0 TO 9
PRINT test(i%) ;
NEXT
PRINT
END
DEF PROCbubblesort(a(), n%)
LOCAL i%, l%
REPEAT
l% = 0
FOR i% = 1 TO n%-1
IF a(i%-1) > a(i%) THEN
SWAP a(i%-1),a(i%)
l% = i%
ENDIF
NEXT
n% = l%
UNTIL l% = 0
ENDPROC</syntaxhighlight>
{{out}}
<pre>
-31 0 1 2 2 4 65 83 99 782
</pre>
 
==={{header|Chipmunk Basic}}===
The [[#Commodore_BASIC|Commodore BASIC]] solution works without any changes.
 
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="commodore">
5 REM ===============================================
10 REM HTTP://ROSETTACODE.ORG/
20 REM TASK=SORTING ALGORITHMS/BUBBLE SORT
30 REM LANGUAGE=COMMODORE 64 BASIC V2
40 REM DATE=2020-08-17
50 REM CODING BY=ALVALONGO
60 REM FILE=BUBLE.PRG
70 REM ============================================
 
100 PRINT "SORTING ALGORITHMS/BUBBLE SORT"
110 GOSUB 700
120 GOSUB 800
130 PRINT "RESULT:"
140 GOSUB 600
150 END
 
600 REM DISPLAY DATA================================
610 FOR K=1 TO N
620 PRINT A(K);
630 NEXT K
640 PRINT
650 RETURN
 
700 REM LOAD DATA ==================================
720 READ N
730 DIM A(N)
740 FOR I=1 TO N
750 READ A(I)
760 NEXT I
770 RETURN
 
800 REM BUBBLE SORT ================================
810 FOR I=1 TO N
815 PRINT "I=";I
820 GOSUB 600
830 SW=-1
840 FOR J=1 TO N-I
850 IF A(J)>A(J+1) THEN T=A(J):A(J)=A(J+1):A(J+1)=T:SW=0
860 NEXT J
865 PRINT "SW=";SW
870 IF SW THEN I=N
880 NEXT I
890 RETURN
 
900 REM DATA==========================================
910 DATA 15
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}}===
<syntaxhighlight lang="gwbasic">10 REM GENERATE A RANDOM BUNCH OF INTEGERS
20 DIM ARR(20)
30 RANDOMIZE TIMER
40 FOR I=0 TO 19
50 ARR(I)=INT(RND*100)
60 NEXT I
70 REM bubble sort the list, printing it at the end of every pass
80 MX = 19
90 CHANGED = 0
100 FOR I = 0 TO 19
110 PRINT ARR(I);
120 NEXT I
130 PRINT
140 FOR I = 0 TO MX-1
150 IF ARR(I)>ARR(I+1) THEN GOSUB 1000
160 NEXT I
170 MX = MX - 1
180 IF CHANGED*MX = 0 THEN END
190 GOTO 90
1000 TEMP = ARR(I)
1010 ARR(I) = ARR(I+1)
1020 ARR(I+1) = TEMP
1030 CHANGED = 1
1040 RETURN</syntaxhighlight>
{{out}}<pre>
20 59 42 9 5 91 6 64 21 28 65 96 20 66 66 70 91 98 63 31
20 42 9 5 59 6 64 21 28 65 91 20 66 66 70 91 96 63 31 98
20 9 5 42 6 59 21 28 64 65 20 66 66 70 91 91 63 31 96 98
9 5 20 6 42 21 28 59 64 20 65 66 66 70 91 63 31 91 96 98
5 9 6 20 21 28 42 59 20 64 65 66 66 70 63 31 91 91 96 98
5 6 9 20 21 28 42 20 59 64 65 66 66 63 31 70 91 91 96 98
5 6 9 20 21 28 20 42 59 64 65 66 63 31 66 70 91 91 96 98
5 6 9 20 21 20 28 42 59 64 65 63 31 66 66 70 91 91 96 98
5 6 9 20 20 21 28 42 59 64 63 31 65 66 66 70 91 91 96 98
5 6 9 20 20 21 28 42 59 63 31 64 65 66 66 70 91 91 96 98
5 6 9 20 20 21 28 42 59 31 63 64 65 66 66 70 91 91 96 98
5 6 9 20 20 21 28 42 31 59 63 64 65 66 66 70 91 91 96 98
5 6 9 20 20 21 28 31 42 59 63 64 65 66 66 70 91 91 96 98
</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "BubblSrt.bas"
110 RANDOMIZE
120 NUMERIC ARRAY(-5 TO 9)
130 CALL INIT(ARRAY)
140 CALL WRITE(ARRAY)
150 CALL BUBBLESORT(ARRAY)
160 CALL WRITE(ARRAY)
170 DEF INIT(REF A)
180 FOR I=LBOUND(A) TO UBOUND(A)
190 LET A(I)=RND(98)+1
200 NEXT
210 END DEF
220 DEF WRITE(REF A)
230 FOR I=LBOUND(A) TO UBOUND(A)
240 PRINT A(I);
250 NEXT
260 PRINT
270 END DEF
280 DEF BUBBLESORT(REF A)
290 DO
300 LET CH=0
310 FOR I=LBOUND(A) TO UBOUND(A)-1
320 IF A(I)>A(I+1) THEN LET T=A(I):LET A(I)=A(I+1):LET A(I+1)=T:LET CH=1
330 NEXT
340 LOOP WHILE CH
350 END DEF</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just 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>
 
==={{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
{{trans|Java}}
counter = size
Assume numbers are in a DIM of size "size" called "nums".
DO
<lang qbasic>DO
changed = 0
for FOR I = 1 toTO sizecounter - 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
LOOP WHILE(NOT changed)</lang>
counter = counter - 1
LOOP WHILE (changed)
 
' Display result
=={{header|BBC BASIC}}==
PRINT "After: ";
The Bubble sort is very inefficient for 99% of cases. This subroutine uses a couple of 'tricks' to try and mitigate the inefficiency to a limited extent.
FOR I = 1 TO 10
<lang bbcbasic>DEF PROC_BubbleSort(Size%)
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}}===
I%=Size%+1
<syntaxhighlight lang="qbasic">100 rem Sorting algorithms/Bubble sort
REPEAT
110 LET n = 10
I%-=1
120 array a
LastChange%=2
130 GOSUB 310
FOR J% = 2 TO I%
140 PRINT "unsort ";
IF data%(J%-1) > data%(J%) THEN
150 GOSUB 360
SWAP data%(J%-1),data%(J%)
160 rem Sort the array
LastChange%=J%
170 GOSUB 210
ENDIF
180 PRINT " sort ";
NEXT J%
190 GOSUB 360
I%=LastChange%
200 END
UNTIL I% = 2
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>unsort 19 78 39 54 63 68 66 52 94 2
sort 2 19 39 52 54 63 66 68 78 94 </pre>
 
==={{header|RapidQ}}===
ENDPROC</lang>
{{trans|QuickBASIC}}
<syntaxhighlight lang="basic">
' Sorting algorithms/Bubble sort
' 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
=={{header|C}}==
counter = size
<lang c>
DO
/* Tanloi3004 */
changed = 0
void swap(int *p)
FOR I = 1 TO counter - 1
{
IF nums(I) > nums(I + 1) THEN
int t = p[0];
p[0] tmp = p[1];nums(I)
nums(I) = nums(I + 1)
p[1] = t;
nums(I + 1) = tmp
}
changed = -1
END IF
NEXT I
counter = counter - 1
LOOP UNTIL NOT changed
 
' Display result
void sort(int *a, int size)
PRINT "After: ";
{
FOR I = 1 TO 10
int i,sorted;
PRINT FORMAT$(" %2d", nums(I));
do {
NEXT I
sorted = 1;
PRINT
--size;
END</syntaxhighlight>
for (i=0; i<size; i++)
{{out}} (2 samples)
if (a[i+1] < a[i])
<pre>Before: 82 34 57 44 48 71 19 33 73 62
{
After: 19 33 34 44 48 57 62 71 73 82</pre>
swap(a+i);
<pre>Before: 4 15 96 93 27 24 sorted =9 0;80 10 21
After: 4 9 10 15 21 24 27 80 93 96</pre>
}
} while (!sorted);
}</lang>
 
==={{header|C++REALbasic}}===
Sorts an array of Integers.
{{works with|g++|4.0.2}}
<syntaxhighlight lang="vb">
<lang cpp>#include <iostream>
Dim sortable() As Integer = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
#include <algorithm>
sortable.Shuffle() ' sortable is now randomized
Dim swapped As Boolean
Do
Dim index, bound As Integer
bound = sortable.Ubound
 
While index < bound
template< typename ARRAY_TYPE, typename INDEX_TYPE >
If sortable(index) > sortable(index + 1) Then
void
Dim s As Integer = sortable(index)
bubble_sort( ARRAY_TYPE array[], INDEX_TYPE size )
sortable.Remove(index)
{
sortable.Insert(index + 1, s)
bool done = false ;
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
while( !done )
{
_Bubblesort PARAM(1) ' Bubble sort
done = true ;
LOCAL (2)
for( INDEX_TYPE i = 0 ; i < size-1 ; i++ )
{
if( array[i] > array[i+1] )
{
done = false ;
std::swap(array[i], array[i+1]);
}
}
}
}
 
DO
template< typename TYPE >
b@ = 0
void
FOR c@ = 1 TO a@-1
print( TYPE val )
IF @(c@-1) > @(c@) THEN PROC _Swap (c@, c@-1) : b@ = c@
{
NEXT
std::cout << val << " " ;
a@ = b@
}
UNTIL b@ = 0
LOOP
 
RETURN
int
main()
{
int array[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } ;
bubble_sort( array, 10 ) ;
std::for_each( &array[0], &array[10], print<int> ) ;
std::cout << std::endl ;
//But in real life...
_Swap PARAM(2) ' Swap two array elements
int data[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } ;
PUSH @(a@)
std::sort( data, data+10 ) ;
@(a@) = @(b@)
std::for_each( data, data+10, print<int> ) ;
@(b@) = POP()
std::cout << std::endl ;
RETURN
}</lang>
_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>
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}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let bubblesort(v, length) be
$( let sorted = false
until sorted
$( sorted := true
length := length - 1
for i=0 to length-1
if v!i > v!(i+1)
$( let x = v!i
v!i := v!(i+1)
v!(i+1) := x
sorted := false
$)
$)
$)
 
let start() be
$( let v = table 10,8,6,4,2,1,3,5,7,9
bubblesort(v, 10)
for i=0 to 9 do writef("%N ", v!i)
wrch('*N')
$)</syntaxhighlight>
{{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}}==
<syntaxhighlight lang="c">#include <stdio.h>
 
void bubble_sort (int *a, int n) {
int i, t, j = n, s = 1;
while (s) {
s = 0;
for (i = 1; i < j; i++) {
if (a[i] < a[i - 1]) {
t = a[i];
a[i] = a[i - 1];
a[i - 1] = t;
s = 1;
}
}
j--;
}
}
 
int main () {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
int i;
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
bubble_sort(a, n);
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
4 65 2 -31 0 99 2 83 782 1
-31 0 1 2 2 4 65 83 99 782
</pre>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|3.0+}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 340 ⟶ 2,543:
}
}
}</langsyntaxhighlight>
 
=={{header|CleanC++}}==
Uses C++11. Compile with
Bubble sorting an array in-situ (using destructive updates), using Clean's uniqueness typing. We specified the type of <tt>sweep</tt> using strictness annotations to improve performance.
g++ -std=c++11 bubble.cpp
<lang clean>import StdEnv
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
 
template <typename RandomAccessIterator>
bsort :: *(a e) -> *(a e) | Array a e & < e
void bubble_sort(RandomAccessIterator begin, RandomAccessIterator end) {
bsort array
bool swapped = true;
# (done, array) = sweep 1 True array
while (begin != end-- && swapped) {
= if done array (bsort array)
swapped = false;
where
for (auto i = begin; i != end; ++i) {
sweep :: !Int !Bool !*(a e) -> (!Bool, !*(a e)) | Array a e & < e
sweep if (*(i done+ 1) < *i) array{
|std::iter_swap(i, i >= size array = (done,+ array1);
# (e1, array)swapped = array![i - 1]true;
}
(e2, array) = array![i]
}
| e1 > e2 = sweep (i + 1) False {array & [i - 1] = e2, [i] = e1}
}
= sweep (i + 1) done array</lang>
}
Using it to sort an array of a hundred numbers:
 
<lang clean>Start :: {Int}
int main() {
Start = bsort {x \\ x <- [100,99..1]}</lang>
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
bubble_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}</syntaxhighlight>
{{out}}
<pre>
-199 -52 2 3 33 56 99 100 177 200
</pre>
 
=={{header|Clojure}}==
Bubble sorts a Java ArrayList in place. Uses 'doseq' iteration construct with a short-circuit when a pass didn't produce any change, and within the pass, an atomic 'changed' variable that gets reset whenever a change occurs.
<langsyntaxhighlight lang="clojure">(ns bubblesort
(:import java.util.ArrayList))
Line 391 ⟶ 2,606:
arr)))
 
(println (bubble-sort (ArrayList. [10 9 8 7 6 5 4 3 2 1])))</langsyntaxhighlight>
 
Purely functional version working on Clojure sequences:
<langsyntaxhighlight lang="clojure">(defn- bubble-step
"was-changed: whether any elements prior to the current first element
were swapped;
Line 420 ⟶ 2,635:
(recur less? result)))))
(println (bubble-sort [10 9 8 7 6 5 4 3 2 1]))</langsyntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Bubble-sort an array in place.
bubble_sort = proc [T: type] (a: array[T])
where T has lt: proctype (T,T) returns (bool)
 
bound_lo: int := array[T]$low(a)
bound_hi: int := array[T]$high(a)
for hi: int in int$from_to_by(bound_hi, bound_lo, -1) do
for i: int in int$from_to(bound_lo, hi-1) do
if a[hi] < a[i] then
temp: T := a[i]
a[i] := a[hi]
a[hi] := temp
end
end
end
end bubble_sort
 
% Print an array
print_arr = proc [T: type] (a: array[T], w: int, s: stream)
where T has unparse: proctype (T) returns (string)
for el: T in array[T]$elements(a) do
stream$putright(s, T$unparse(el), w)
end
stream$putc(s, '\n')
end print_arr
 
start_up = proc ()
ai = array[int]
po: stream := stream$primary_output()
test: ai := ai$[7, -5, 0, 2, 99, 16, 4, 20, 47, 19]
stream$puts(po, "Before: ") print_arr[int](test, 3, po)
bubble_sort[int](test)
stream$puts(po, "After: ") print_arr[int](test, 3, po)
end start_up</syntaxhighlight>
{{out}}
<pre>Before: 7 -5 0 2 99 16 4 20 47 19
After: -5 0 2 4 7 16 19 20 47 99</pre>
 
=={{header|CMake}}==
Only for lists of integers.
 
<syntaxhighlight lang="cmake"># bubble_sort(var [value1 value2...]) sorts a list of integers.
function(bubble_sort var)
math(EXPR last "${ARGC} - 1") # Prepare to sort ARGV[1]..ARGV[last].
set(again YES)
while(again)
set(again NO)
math(EXPR last "${last} - 1") # Decrement last index.
foreach(index RANGE 1 ${last}) # Loop for each index.
math(EXPR index_plus_1 "${index} + 1")
set(a "${ARGV${index}}") # a = ARGV[index]
set(b "${ARGV${index_plus_1}}") # b = ARGV[index + 1]
if(a GREATER "${b}") # If a > b...
set(ARGV${index} "${b}") # ...then swap a, b
set(ARGV${index_plus_1} "${a}") # inside ARGV.
set(again YES)
endif()
endforeach(index)
endwhile()
 
set(answer)
math(EXPR last "${ARGC} - 1")
foreach(index RANGE 1 "${last}")
list(APPEND answer "${ARGV${index}}")
endforeach(index)
set("${var}" "${answer}" PARENT_SCOPE)
endfunction(bubble_sort)</syntaxhighlight>
 
<syntaxhighlight lang="cmake">bubble_sort(result 33 11 44 22 66 55)
message(STATUS "${result}")</syntaxhighlight>
 
<pre>-- 11;22;33;44;55;66</pre>
 
=={{header|COBOL}}==
This is a complete program that readsdemonstrates inthe abubble filesort ofalgorithm integers and sortsin themCOBOL.
<br/>This version is for COBOL-74 which does not have in-line performs, nor END-IF and related constructs.
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. BUBBLESORT.
AUTHOR. DAVE STRATFORD.
DATE-WRITTEN. MARCH 2010.
INSTALLATION. HEXAGON SYSTEMS LIMITED.
 
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. ICL VME.
OBJECT-COMPUTER. ICL VME.
 
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FA-INPUT-FILE ASSIGN FL01.
SELECT FB-OUTPUT-FILE ASSIGN FL02.
 
DATA DIVISION.
FILE SECTION.
 
FD FA-INPUT-FILE.
01 FA-INPUT-REC.
03 FA-DATA PIC S9(6).
 
FD FB-OUTPUT-FILE.
01 FB-OUTPUT-REC PIC S9(6).
 
WORKING-STORAGE SECTION.
01 WA-IDENTITY.
03 WA-PROGNAME PIC X(10) VALUE "BUBBLESORT".
03 WA-VERSION PIC X(6) VALUE "000001".
 
01 WB-TABLE.
03 WB-ENTRY PIC 9(8) COMP SYNC OCCURS 100000
INDEXED BY WB-IX-1.
 
01 WC-VARS.
03 WC-SIZE PIC S9(8) COMP SYNC.
Line 464 ⟶ 2,757:
03 WC-END PIC S9(8) COMP SYNC.
03 WC-LAST-CHANGE PIC S9(8) COMP SYNC.
 
01 WF-CONDITION-FLAGS.
03 WF-EOF-FLAG PIC X.
Line 470 ⟶ 2,763:
03 WF-EMPTY-FILE-FLAG PIC X.
88 EMPTY-FILE VALUE "Y".
 
PROCEDURE DIVISION.
A-MAIN SECTION.
Line 478 ⟶ 2,771:
PERFORM C-SORT.
PERFORM D-FINISH.
 
A-999.
STOP RUN.
 
B-INITIALISE SECTION.
B-000.
DISPLAY "*** " WA-PROGNAME " VERSION "
WA-VERSION " STARTING ***".
 
MOVE ALL "N" TO WF-CONDITION-FLAGS.
OPEN INPUT FA-INPUT-FILE.
SET WB-IX-1 TO 0.
 
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG
WF-EMPTY-FILE-FLAG.
 
PERFORM BA-READ-INPUT UNTIL END-OF-FILE.
 
CLOSE FA-INPUT-FILE.
 
SET WC-SIZE TO WB-IX-1.
 
B-999.
EXIT.
 
BA-READ-INPUT SECTION.
BA-000.
SET WB-IX-1 UP BY 1.
MOVE FA-DATA TO WB-ENTRY(WB-IX-1).
 
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG.
 
BA-999.
EXIT.
 
C-SORT SECTION.
C-000.
DISPLAY "SORT STARTING".
 
MOVE WC-SIZE TO WC-END.
PERFORM E-BUBBLE UNTIL WC-END = 1.
 
DISPLAY "SORT FINISHED".
 
C-999.
EXIT.
 
D-FINISH SECTION.
D-000.
OPEN OUTPUT FB-OUTPUT-FILE.
SET WB-IX-1 TO 1.
 
PERFORM DA-WRITE-OUTPUT UNTIL WB-IX-1 > WC-SIZE.
 
CLOSE FB-OUTPUT-FILE.
 
DISPLAY "*** " WA-PROGNAME " FINISHED ***".
 
D-999.
EXIT.
 
DA-WRITE-OUTPUT SECTION.
DA-000.
WRITE FB-OUTPUT-REC FROM WB-ENTRY(WB-IX-1).
SET WB-IX-1 UP BY 1.
 
DA-999.
EXIT.
 
E-BUBBLE SECTION.
E-000.
MOVE 1 TO WC-LAST-CHANGE.
 
PERFORM F-PASS VARYING WB-IX-1 FROM 1 BY 1
UNTIL WB-IX-1 = WC-END.
 
MOVE WC-LAST-CHANGE TO WC-END.
 
E-999.
EXIT.
 
F-PASS SECTION.
F-000.
Line 566 ⟶ 2,859:
MOVE WB-ENTRY(WB-IX-1 + 1) TO WB-ENTRY(WB-IX-1)
MOVE WC-TEMP TO WB-ENTRY(WB-IX-1 + 1).
 
F-999.
EXIT.</lang>
</syntaxhighlight>
 
A more modern version of COBOL.
<syntaxhighlight lang="cobol">
identification division.
program-id. BUBBLSRT.
data division.
working-storage section.
01 changed-flag pic x.
88 hasChanged value 'Y'.
88 hasNOTChanged value 'N'.
01 itemCount pic 99.
01 tempItem pic 99.
01 itemArray.
03 itemArrayCount pic 99.
03 item pic 99 occurs 99 times
indexed by itemIndex.
*
procedure division.
main.
* place the values to sort into itemArray
move 10 to itemArrayCount
move 28 to item (1)
move 44 to item (2)
move 46 to item (3)
move 24 to item (4)
move 19 to item (5)
move 2 to item (6)
move 17 to item (7)
move 11 to item (8)
move 24 to item (9)
move 4 to item (10)
* store the starting count in itemCount and perform the sort
move itemArrayCount to itemCount
perform bubble-sort
* output the results
perform varying itemIndex from 1 by 1
until itemIndex > itemArrayCount
display item (itemIndex) ';' with no advancing
end-perform
* thats it!
stop run.
*
bubble-sort.
perform with test after until hasNOTchanged
set hasNOTChanged to true
subtract 1 from itemCount
perform varying itemIndex from 1 by 1
until itemIndex > itemCount
if item (itemIndex) > item (itemIndex + 1)
move item (itemIndex) to tempItem
move item (itemIndex + 1) to item (itemIndex)
move tempItem to item (itemIndex + 1)
set hasChanged to true
end-if
end-perform
end-perform
.
</syntaxhighlight>
 
{{out}}
<pre> Output: 02;04;11;17;19;24;24;28;44;46; </pre>
 
=={{header|Common Lisp}}==
Bubble sort an sequence in-place, using the < operator for comparison if no comaprison function is provided
<langsyntaxhighlight lang="lisp">(defun bubble-sort (sequence &optional (compare #'<))
"sort a sequence (array or list) with an optional comparison function (cl:< is the default)"
(loop with sorted = nil until sorted do
Line 581 ⟶ 2,936:
(rotatef (elt sequence a)
(elt sequence (1+ a)))
(setf sorted nil)))))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="lisp">(bubble-sort (list 5 4 3 2 1))</langsyntaxhighlight>
 
<code>elt</code> has linear access time for lists, making the prior implementation of bubble-sort very expensive (although very clear, and straightforward to code. Here is an implementation that works efficiently for both vectors and lists. For lists it also has the nice property that the input list and the sorted list begin with the same <code>cons</code> cell.
 
<langsyntaxhighlight lang="lisp">(defun bubble-sort-vector (vector predicate &aux (len (1- (length vector))))
(do ((swapped t)) ((not swapped) vector)
(setf swapped nil)
Line 606 ⟶ 2,961:
(etypecase sequence
(list (bubble-sort-list sequence predicate))
(vector (bubble-sort-vector sequence predicate))))</langsyntaxhighlight>
 
=={{header|Cowgol}}==
 
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Comparator interface, on the model of C, i.e:
# foo < bar => -1, foo == bar => 0, foo > bar => 1
typedef CompRslt is int(-1, 1);
interface Comparator(foo: intptr, bar: intptr): (rslt: CompRslt);
 
# Bubble sort an array of pointer-sized integers given a comparator function
# (This is the closest you can get to polymorphism in Cowgol).
sub bubbleSort(A: [intptr], len: intptr, comp: Comparator) is
loop
var swapped: uint8 := 0;
var i: intptr := 1;
var a := @next A;
while i < len loop
if comp([@prev a], [a]) == 1 then
var t := [a];
[a] := [@prev a];
[@prev a] := t;
swapped := 1;
end if;
a := @next a;
i := i + 1;
end loop;
if swapped == 0 then
return;
end if;
end loop;
end sub;
 
# Test: sort a list of numbers
sub NumComp implements Comparator is
# Compare the inputs as numbers
if foo < bar then rslt := -1;
elseif foo > bar then rslt := 1;
else rslt := 0;
end if;
end sub;
 
# Numbers
var numbers: intptr[] := {
65,13,4,84,29,5,96,73,5,11,17,76,38,26,44,20,36,12,44,51,79,8,99,7,19,95,26
};
 
# Sort the numbers in place
bubbleSort(&numbers as [intptr], @sizeof numbers, NumComp);
 
# Print the numbers (hopefully in order)
var i: @indexof numbers := 0;
while i < @sizeof numbers loop
print_i32(numbers[i] as uint32);
print_char(' ');
i := i + 1;
end loop;
print_nl();</syntaxhighlight>
 
{{out}}
 
<pre>4 5 5 7 8 11 12 13 17 19 20 26 26 29 36 38 44 44 51 65 73 76 79 84 95 96 99</pre>
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio, std.algorithm : swap;
{{works with|DMD|1.025}}
<lang d>import std.stdio;
 
voidT[] bubbleSort(T)(T[] arraydata) {pure nothrow
{
int itemCount = array.length;
foreach_reverse (n; 0 .. data.length)
bool hasChanged;
do {
hasChangedbool = falseswapped;
itemCount--foreach (i; 0 .. n)
for (int index = 0;if index(data[i] <> itemCount;data[i index++ 1]) {
if swap(arraydata[indexi], > arraydata[indexi + 1]) {;
T tempswapped = array[index]true;
array[index] = array[index + 1];}
if array[index + 1] = temp;(!swapped)
hasChanged = truebreak;
}
return data;
}
 
 
void main()
{
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
writeln(array.bubbleSort());
}</syntaxhighlight>
{{out}}
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
 
=={{header|Dart}}==
<pre>
List<num> bubbleSort(List<num> list) {
var retList = new List<num>.from(list);
var tmp;
var swapped = false;
do {
swapped = false;
for(var i = 1; i < retList.length; i++) {
if(retList[i - 1] > retList[i]) {
tmp = retList[i - 1];
retList[i - 1] = retList[i];
retList[i] = tmp;
swapped = true;
}
}
} while(swapped);
return retList;
}
</pre>
 
=={{header|Delphi}}==
Dynamic array is a 0-based array of variable length
 
Static array is an arbitrary-based array of fixed length
<syntaxhighlight lang="delphi">program TestBubbleSort;
 
{$APPTYPE CONSOLE}
 
{.$DEFINE DYNARRAY} // remove '.' to compile with dynamic array
 
type
TItem = Integer; // declare ordinal type for array item
{$IFDEF DYNARRAY}
TArray = array of TItem; // dynamic array
{$ELSE}
TArray = array[0..15] of TItem; // static array
{$ENDIF}
 
procedure BubbleSort(var A: TArray);
var
Item: TItem;
K, L, J: Integer;
 
begin
L:= Low(A) + 1;
repeat
K:= High(A);
for J:= High(A) downto L do begin
if A[J - 1] > A[J] then begin
Item:= A[J - 1];
A[J - 1]:= A[J];
A[J]:= Item;
K:= J;
end;
end;
L:= K + 1;
until L > High(A);
end;
 
var
A: TArray;
I: Integer;
 
begin
{$IFDEF DYNARRAY}
SetLength(A, 16);
{$ENDIF}
for I:= Low(A) to High(A) do
A[I]:= Random(100);
for I:= Low(A) to High(A) do
Write(A[I]:3);
Writeln;
BubbleSort(A);
for I:= Low(A) to High(A) do
Write(A[I]:3);
Writeln;
Readln;
end.</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* Bubble sort an array of integers */
proc nonrec bubblesort([*] int a) void:
bool sorted;
int i, temp;
sorted := false;
while not sorted do
sorted := true;
for i from 1 upto dim(a,1)-1 do
if a[i-1] > a[i] then
sorted := false;
temp := a[i-1];
a[i-1] := a[i];
a[i] := temp
fi
od
od
corp
 
/* Test */
proc nonrec main() void:
int i;
[10] int a = (9, -5, 3, 3, 24, -16, 3, -120, 250, 17);
write("Before sorting: ");
for i from 0 upto 9 do write(a[i]:5) od;
writeln();
bubblesort(a);
write("After sorting: ");
for i from 0 upto 9 do write(a[i]:5) od
corp</syntaxhighlight>
{{out}}
<pre>Before sorting: 9 -5 3 3 24 -16 3 -120 250 17
After sorting: -120 -16 -5 3 3 3 9 17 24 250</pre>
 
=={{header|Dyalect}}==
 
<syntaxhighlight lang="dyalect">func bubbleSort(list) {
var done = false
while !done {
done = true
for i in 1..(list.Length()-1) {
if list[i - 1] > list[i] {
var x = list[i]
list[i] = list[i - 1]
list[i - 1] = x
done = false
}
}
}
} while (hasChanged);
}
var xs = [3,1,5,4,2,6]
bubbleSort(xs)
print(xs)</syntaxhighlight>
 
{{out}}
void main() {
auto array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].dup;
 
<pre>[1, 2, 3, 4, 5, 6]</pre>
// member function invocation syntax for arrays
array.bubbleSort();
foreach (index, value; array)
writefln("array[%d] = %d", index, value);
}</lang>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">def bubbleSort(target) {
__loop(fn {
var changed := false
Line 651 ⟶ 3,213:
changed
})
}</langsyntaxhighlight>
 
(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}}==
<syntaxhighlight lang="scheme">
;; sorts a vector of objects in place
;; proc is an user defined comparison procedure
 
(define (bubble-sort V proc)
(define length (vector-length V))
(for* ((i (in-range 0 (1- length))) (j (in-range (1+ i) length)))
(unless (proc (vector-ref V i) (vector-ref V j)) (vector-swap! V i j)))
V)
 
 
(define V #( albert antoinette elvis zen simon))
(define (sort/length a b) ;; sort by string length
(< (string-length a) (string-length b)))
 
(bubble-sort V sort/length)
→ #(zen simon elvis albert antoinette)
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
This demo of a bubble sort on the EDSAC shows how awkward it was to deal with arrays
in the absence of an index register (one was added in 1953).
To refer to an array element at a given index, the programmer had to manufacture an
EDSAC order referring to the correct address, then plant that order in the code.
 
To clarify the EDSAC program, an equivalent Pascal program is added as a comment.
<syntaxhighlight lang="edsac">
[Bubble sort demo for Rosetta Code website]
[EDSAC program. Initial Orders 2]
 
[Sorts a list of double-word integers.
List must be loaded at an even address.
First item gives number of items to follow.
Address of list is placed in location 49.
List can then be referred to with code letter L.]
T49K
P300F [<---------- address of list here]
 
[Subroutine R2, reads positive integers during input of orders.
Items separated by F; list ends with #TZ.]
GKT20FVDL8FA40DUDTFI40FA40FS39FG@S2FG23FA5@T5@E4@E13Z
 
[Tell R2 where to store integers it reads from tape.]
T #L ['T m D' in documentation, but this also works]
 
[Lists of integers, comment out all except one]
[10 integers from digits of pi]
10F314159F265358F979323F846264F338327F950288F419716F939937F510582F097494#TZ
 
[32 integers from digits of e ]
[32F
27182818F28459045F23536028F74713526F62497757F24709369F99595749F66967627F
72407663F03535475F94571382F17852516F64274274F66391932F00305992F18174135F
96629043F57290033F42952605F95630738F13232862F79434907F63233829F88075319F
52510190F11573834F18793070F21540891F49934884F16750924F47614606F68082264#TZ]
 
[Library subroutine P7, prints positive integer at 0D.
35 locations; load at aneven address.]
T 56 K
GKA3FT26@H28#@NDYFLDT4DS27@TFH8@S8@T1FV4DAFG31@SFLDUFOFFFSFL4F
T4DA1FA27@G11@XFT28#ZPFT27ZP1024FP610D@524D!FO30@SFL8FE22@
 
[The EDSAC code below implements the following Pascal program,
where the integers to be sorted are in a 1-based array x.
Since the assembler used (EdsacPC by Martin Campbell-Kelly)
doesn't allow square brackets inside comments, they are
replaced here by curly brackets.]
[
swapped := true;
j := n; // number of items
while (swapped and (j >= 2)) do begin
swapped := false;
for i := 1 to j - 1 do begin
// Using temp in the comparison makes the EDSAC code a bit simpler
temp := x{i};
if (x{i + 1} < temp) then begin
x{i} := x{i + 1};
x{i + 1} := temp;
swapped := true;
end;
end;
dec(j);
end;
]
[Main routine]
T 100 K
G K
[0] P F P F [double-word temporary store]
[2] P F [flag for swapped, >= 0 if true, < 0 if false]
[3] P F ['A' order for x{j}; implicitly defines j]
[4] P 2 F [to change list index by 1, i.e.change address by 2]
[5] A #L ['A' order for number of items]
[6] A 2#L ['A' order for x{1}]
[7] A 4#L ['A' order for x{2}]
[8] I2046 F [add to convert 'A' order to 'T' and dec address by 2]
[9] K4096 F [(1) minimum 17-bit value (2) teleprinter null]
[10] P D [constant 1, used in printing]
[11] # F [figure shift]
[12] & F [line feed]
[13] @ F [carriage return]
 
[Enter here with acc = 0]
[14] T 2 @ [swapped := true]
A L [get count, n in Pascal program above]
L 1 F [times 4 by shifting]
A 5 @ [make 'A' order for x{n}; initializes j := n]
 
[Start 'while' loop of Pascal program.
Here acc = 'A' order for x{j}]
[18] U 3 @ [update j]
S 7 @ [subtract 'A' order for x{2}]
G 56 @ [if j < 2 then done]
T F [acc := 0]
A 2 @ [test for swapped, acc >= 0 if so]
G 56 @ [if not swapped then done]
A 9 @ [change acc from >= 0 to < 0]
T 2 @ [swapped := false until swap occurs]
A 6 @ ['A' order for x{1}; initializes i := 1]
 
[Start 'for' loop of Pascal program.
Here acc = 'A' order for x{i}]
[27] U 36 @ [store order]
S 3 @ [subtract 'A' order for x{j}]
E 52 @ [out of 'for' loop if i >= j]
T F [clear acc]
A 36 @ [load 'A' order for x{i}]
A 4 @ [inc address by 2]
U 38 @ [plant 'A' order for x{i + 1}]
A 8 @ ['A' to 'T', and dec address by 2]
T 42 @ [plant 'T' order for x{i}]
[36] A #L [load x{i}; this order implicitly defines i]
T #@ [temp := x{i}]
[38] A #L [load x{i + 1}]
S #@ [acc := x{i + 1} - temp]
E 49 @ [don't swap if x{i + 1} >= temp]
 
[Here to swap x{i} and x{i + 1}]
A #@ [restore acc := x{i + 1} after test]
[42] T #L [x{i} := x{i + 1}]
A 42 @ [load 'T' order for x{i}]
A 4 @ [inc address by 2]
T 47 @ [plant 'T' order for x{i + 1}]
A #@ [load temp]
[47] T #L [to x{i + 1}]
T 2 @ [swapped := 0 (true)]
 
[49] T F [clear acc]
A 38 @ [load 'A' order for x{i + 1}]
G 27 @ [loop (unconditional) to inc i]
 
[52] T F
A 3 @ [load 'A' order for x{j}]
S 4 @ [dec address by 2]
G 18 @ [loop (unconditional) to dec j]
 
[Print the sorted list of integers]
[56] O 11 @ [figure shift]
T F [clear acc]
A 5 @ [load 'A' order for head of list]
T 65 @ [plant in code below]
S L [load negative number of items]
[61] T @ [use first word of temp store for count]
A 65 @ [load 'A' order for item]
A 4 @ [inc address by 2]
T 65 @ [store back]
[65] A #L [load next item in list]
T D [to 0D for printing]
[67] A 67 @ [for subroutine return]
G 56 F [print integer, clears acc]
O 13 @ [print CR]
O 12 @ [print LF]
A @ [negative count]
A 10 @ [add 1]
G 61 @ [loop back till count = 0]
[74] O 9 @ [null to flush teleprinter buffer]
Z F [stop]
E 14 Z [define entry point]
P F [acc = 0 on entry]
</syntaxhighlight>
{{out}}
<pre>
97494
265358
314159
338327
419716
510582
846264
939937
950288
979323
</pre>
 
=={{header|Eiffel}}==
Line 660 ⟶ 3,437:
This solution is presented in two classes. The first is a simple application that creates a set, an instance of <code lang="eiffel">MY_SORTED_SET</code>, and adds elements to the set in unsorted order. It iterates across the set printing the elements, then it sorts the set, and reprints the elements.
 
<langsyntaxhighlight lang="eiffel">class
APPLICATION
create
Line 689 ⟶ 3,466:
my_set: MY_SORTED_SET [INTEGER]
-- Set to be sorted
end</langsyntaxhighlight>
 
The second class is <code lang="eiffel">MY_SORTED_SET</code>.
 
<langsyntaxhighlight lang="eiffel">class
MY_SORTED_SET [G -> COMPARABLE]
inherit
Line 728 ⟶ 3,505:
end
end
end</langsyntaxhighlight>
 
This class inherits from the Eiffel library class <code lang="eiffel">TWO_WAY_SORTED_SET</code>, which implements sets whose elements are comparable. Therefore, the set can be ordered and in fact is kept so under normal circumstances.
Line 734 ⟶ 3,511:
<code lang="eiffel">MY_SORTED_SET</code> redefines only the routine <code lang="eiffel">sort</code> which contains the implementation of the sort algorithm. The implementation in the redefined version of <code lang="eiffel">sort</code> in <code lang="eiffel">MY_SORTED_SET</code> uses a bubble sort.
 
{{out}}
Output:
<pre>
Before: 7 10 4 8 9 3 5 1 6 2
Line 740 ⟶ 3,517:
</pre>
 
<code lang="eiffel">TWO_WAY_SORTED_SET</code> is implemented internally as a list.
<code lang="eiffel">TWO_WAY_SORTED_SET</code> is implemented internally as a list. For this example, we use the feature <code lang="eiffel">put_front</code> which explicitly adds each new element to the beginning of the list, allowing us to show that the elements are unordered until we sort them. It also causes, in the "Before" output, the elements to be printed in the reverse of the order in which they were added. Under normal circumstances, we would use the feature <code lang="eiffel">extend</code> (rather than <code lang="eiffel">put_front</code>) to add elements to the list. This would assure that the order was maintained even as elements were added.
For this example, we use the feature <code lang="eiffel">put_front</code> which explicitly adds each new element to the beginning of the list, allowing us to show that the elements are unordered until we sort them.
It also causes, in the "Before" output, the elements to be printed in the reverse of the order in which they were added.
Under normal circumstances, we would use the feature <code lang="eiffel">extend</code> (rather than <code lang="eiffel">put_front</code>) to add elements to the list.
This would assure that the order was maintained even as elements were added.
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 6.x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
extension op
{
bubbleSort()
{
var list := self.clone();
bool madeChanges := true;
int itemCount := list.Length;
while (madeChanges)
{
madeChanges := false;
itemCount -= 1;
for(int i := 0; i < itemCount; i += 1)
{
if (list[i] > list[i + 1])
{
list.exchange(i,i+1);
madeChanges := true
}
}
};
^ list
}
}
public program()
{
var list := new int[]{3, 7, 3, 2, 1, -4, 10, 12, 4};
console.printLine(list.bubbleSort().asEnumerable())
}</syntaxhighlight>
{{out}}
<pre>
-4,1,2,3,3,4,7,10,12
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Sort do
def bsort(list) when is_list(list) do
t = bsort_iter(list)
 
if t == list, do: t, else: bsort(t)
end
 
def bsort_iter([x, y | t]) when x > y, do: [y | bsort_iter([x | t])]
def bsort_iter([x, y | t]), do: [x | bsort_iter([y | t])]
def bsort_iter(list), do: list
end</syntaxhighlight>
 
=={{header|Erlang}}==
sort/3 copied from Stackoverflow.
<syntaxhighlight lang="erlang">
-module( bubble_sort ).
 
-export( [list/1, task/0] ).
 
list( To_be_sorted ) -> sort( To_be_sorted, [], true ).
 
task() ->
List = "asdqwe123",
Sorted = list( List ),
io:fwrite( "List ~p is sorted ~p~n", [List, Sorted] ).
 
 
sort( [], Acc, true ) -> lists:reverse( Acc );
sort( [], Acc, false ) -> sort( lists:reverse(Acc), [], true );
sort( [X, Y | T], Acc, _Done ) when X > Y -> sort( [X | T], [Y | Acc], false );
sort( [X | T], Acc, Done ) -> sort( T, [X | Acc], Done ).
</syntaxhighlight>
{{out}}
<pre>
7> bubble_sort:task().
List "asdqwe123" is sorted "123adeqsw"
</pre>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROGRAM BUBBLE_SORT
 
DIM FLIPS%,N,J
 
DIM A%[100]
 
BEGIN
 
! init random number generator
RANDOMIZE(TIMER)
! fills array A% with random data
FOR N=1 TO UBOUND(A%,1) DO
A%[N]=RND(1)*256
END FOR
! sort array
FLIPS%=TRUE
WHILE FLIPS% DO
FLIPS%=FALSE
FOR N=1 TO UBOUND(A%,1)-1 DO
IF A%[N]>A%[N+1] THEN
SWAP(A%[N],A%[N+1])
FLIPS%=TRUE
END IF
END FOR
END WHILE
! print sorted array
FOR N=1 TO UBOUND(A%,1) DO
PRINT(A%[N];)
END FOR
PRINT
END PROGRAM
</syntaxhighlight>
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">function bubble_sort(sequence s)
object tmp
integer changed
for j = length(s) to 1 by -1 do
changed = 0
for i = 1 to j-1 do
if compare(s[i], s[i+1]) > 0 then
tmp = s[i]
s[i] = s[i+1]
s[i+1] = tmp
changed = 1
end if
end for
if not changed then
exit
end if
end for
return s
end function
 
include misc.e
constant s = {4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}
 
puts(1,"Before: ")
pretty_print(1,s,{2})
puts(1,"\nAfter: ")
pretty_print(1,bubble_sort(s),{2})</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|Ezhil}}==
<syntaxhighlight lang="ezhil">
 
## இந்த நிரல் ஒரு பட்டியலில் உள்ள எண்களை Bubble Sort என்ற முறைப்படி ஏறுவரிசையிலும் பின்னர் அதையே இறங்குவரிசையிலும் அடுக்கித் தரும்
 
## மாதிரிக்கு நாம் ஏழு எண்களை எடுத்துக்கொள்வோம்
 
எண்கள் = [5, 1, 10, 8, 1, 21, 4, 2]
எண்கள்பிரதி = எண்கள்
 
பதிப்பி "ஆரம்பப் பட்டியல்:"
பதிப்பி எண்கள்
 
நீளம் = len(எண்கள்)
குறைநீளம் = நீளம் - 1
 
@(குறைநீளம் != -1) வரை
மாற்றம் = -1
@(எண் = 0, எண் < குறைநீளம், எண் = எண் + 1) ஆக
முதலெண் = எடு(எண்கள், எண்)
இரண்டாமெண் = எடு(எண்கள், எண் + 1)
@(முதலெண் > இரண்டாமெண்) ஆனால்
 
## பெரிய எண்களை ஒவ்வொன்றாகப் பின்னே நகர்த்துகிறோம்
 
வெளியேஎடு(எண்கள், எண்)
நுழைக்க(எண்கள், எண், இரண்டாமெண்)
வெளியேஎடு(எண்கள், எண் + 1)
நுழைக்க(எண்கள், எண் + 1, முதலெண்)
மாற்றம் = எண்
முடி
முடி
குறைநீளம் = மாற்றம்
முடி
 
பதிப்பி "ஏறு வரிசையில் அமைக்கப்பட்ட பட்டியல்:"
பதிப்பி எண்கள்
 
## இதனை இறங்குவரிசைக்கு மாற்றுவதற்கு எளிய வழி
 
தலைகீழ்(எண்கள்)
 
## இப்போது, நாம் ஏற்கெனவே எடுத்துவைத்த எண்களின் பிரதியை Bubble Sort முறைப்படி இறங்குவரிசைக்கு மாற்றுவோம்
 
நீளம் = len(எண்கள்பிரதி)
குறைநீளம் = நீளம் - 1
 
@(குறைநீளம் != -1) வரை
மாற்றம் = -1
@(எண் = 0, எண் < குறைநீளம், எண் = எண் + 1) ஆக
முதலெண் = எடு(எண்கள்பிரதி, எண்)
இரண்டாமெண் = எடு(எண்கள்பிரதி, எண் + 1)
@(முதலெண் < இரண்டாமெண்) ஆனால்
 
## சிறிய எண்களை ஒவ்வொன்றாகப் பின்னே நகர்த்துகிறோம்
 
வெளியேஎடு(எண்கள்பிரதி, எண்)
நுழைக்க(எண்கள்பிரதி, எண், இரண்டாமெண்)
வெளியேஎடு(எண்கள்பிரதி, எண் + 1)
நுழைக்க(எண்கள்பிரதி, எண் + 1, முதலெண்)
மாற்றம் = எண்
முடி
முடி
குறைநீளம் = மாற்றம்
முடி
 
பதிப்பி "இறங்கு வரிசையில் அமைக்கப்பட்ட பட்டியல்:"
பதிப்பி எண்கள்பிரதி
 
</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">let BubbleSort (lst : list<int>) =
let rec sort accum rev lst =
match lst, rev with
| [], true -> accum |> List.rev
| [], false -> accum |> List.rev |> sort [] true
| x::y::tail, _ when x > y -> sort (y::accum) false (x::tail)
| head::tail, _ -> sort (head::accum) rev tail
sort [] true lst
</syntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: fry kernel locals math math.order sequences
sequences.private ;
IN: rosetta.bubble
Line 765 ⟶ 3,807:
 
: natural-sort! ( seq -- )
[ <=> ] sort! ;</langsyntaxhighlight>
 
It is possible to pass your own comparison operator to <code>sort!</code>, so you can f.e. sort your sequence backwards with passing <code>[ >=< ]</code> into it.
 
<langsyntaxhighlight lang="factor">10 [ 10000 random ] replicate
[ "Before: " write . ]
[ "Natural: " write [ natural-sort! ] keep . ]
[ "Reverse: " write [ [ >=< ] sort! ] keep . ] tri</langsyntaxhighlight>
 
Before: { 3707 5045 4661 1489 3140 7195 8844 6506 6322 3199 }
Natural: { 1489 3140 3199 3707 4661 5045 6322 6506 7195 8844 }
Reverse: { 8844 7195 6506 6322 5045 4661 3707 3199 3140 1489 }
 
=={{header|Fish}}==
This is not a complete implementation of bubblesort: it doesn't keep a boolean flag whether to stop, so it goes on printing each stage of the sorting process ad infinitum.
 
<syntaxhighlight lang="fish">v Sorts the (pre-loaded) stack
with bubblesort.
v <
\l0=?;l&
>&:1=?v1-&2[$:{:{](?${
>~{ao ^
>~}l &{ v
o","{n:&-1^?=0:&<</syntaxhighlight>
 
=={{header|Forth}}==
Sorts the 'cnt' cells stored at 'addr' using the test stored in the deferred word 'bubble-test'. Uses forth local variables for clarity.
 
<langsyntaxhighlight lang="forth">defer bubble-test
' > is bubble-test
 
Line 789 ⟶ 3,843:
i 2@ bubble-test if i 2@ swap i 2! then
cell +loop
loop ;</langsyntaxhighlight>
 
This is the same algorithm done without the local variables:
 
<langsyntaxhighlight lang="forth">: bubble ( addr cnt -- )
dup 1 do
2dup i - cells bounds do
i 2@ bubble-test if i 2@ swap i 2! then
cell +loop
loop ;</langsyntaxhighlight>
 
Version with ''O(n)'' best case:
<langsyntaxhighlight lang="forth">: bubble ( addr len -- )
begin
1- 2dup true -rot ( sorted addr len-1 )
Line 810 ⟶ 3,864:
then
cell +loop ( sorted )
until 2drop ;</langsyntaxhighlight>
 
Test any version with this:
Line 826 ⟶ 3,880:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">SUBROUTINE Bubble_Sort(a)
REAL, INTENT(in out), DIMENSION(:) :: a
REAL :: temp
INTEGER :: i, j
LOGICAL :: swapped = .TRUE.
DO j = SIZE(a)-1, 1, -1
Line 844 ⟶ 3,898:
IF (.NOT. swapped) EXIT
END DO
END SUBROUTINE Bubble_Sort</langsyntaxhighlight>
 
=={{header|g-fu}}==
<syntaxhighlight lang="g-fu">
(fun bubbles (vs)
(let done? F n (len vs))
(while (not done?)
(set done? T n (- n 1))
(for (n i)
(let x (# vs i) j (+ i 1) y (# vs j))
(if (> x y) (set done? F (# vs i) y (# vs j) x))))
 
vs)
 
(bubbles '(2 1 3))
---
(1 2 3)
</syntaxhighlight>
 
=={{header|GDScript}}==
Here is an implementation of Bubble Sort algorithm in GDScript for array of primitive types:
<syntaxhighlight lang="gdscript">
extends Node2D
 
 
func BubbleSort(_array : Array) -> void:
for i in range(_array.size() - 1):
var swapped : bool = false
for j in range(_array.size() - i - 1):
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
 
 
func _ready() -> void:
var array : Array = range(-10, 10)
array.shuffle()
 
print("Initial array:")
print(array)
 
BubbleSort(array)
 
print("Sorted array:")
print(array)
return
</syntaxhighlight>
 
Possible output:
{{out}}
<pre>
Initial array:
[-7, -6, 2, -8, 4, -1, -3, -5, 5, -10, -4, 7, 3, 8, 0, -9, 6, 9, -2, 1]
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:
<syntaxhighlight lang="gdscript">
class Comparable:
var Value
 
func _init(_val):
self.Value = _val
 
func compare(_other : Comparable) -> int:
# Here is the simple implementation of compare
# for primitive type wrapper.
return self.Value - _other.Value
 
 
func BubbleSortObjects(_array : Array) -> void:
for i in range(_array.size() - 1):
var swapped : bool = false
for j in range(_array.size() - i - 1):
if _array[j].compare(_array[j + 1]) > 0:
var tmp = _array[j]
_array[j] = _array[j + 1]
_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}}==
Per task pseudocode:
<syntaxhighlight lang="go">package main
 
import "fmt"
 
func main() {
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
fmt.Println("unsorted:", list)
 
bubblesort(list)
fmt.Println("sorted! ", list)
}
 
func bubblesort(a []int) {
for itemCount := len(a) - 1; ; itemCount-- {
hasChanged := false
for index := 0; index < itemCount; index++ {
if a[index] > a[index+1] {
a[index], a[index+1] = a[index+1], a[index]
hasChanged = true
}
}
if hasChanged == false {
break
}
}
}</syntaxhighlight>
 
More generic version that can sort anything that implements <code>sort.Interface</code>:
<syntaxhighlight lang="go">package main
 
import (
"sort"
"fmt"
)
 
func main() {
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
fmt.Println("unsorted:", list)
 
bubblesort(sort.IntSlice(list))
fmt.Println("sorted! ", list)
}
 
func bubblesort(a sort.Interface) {
for itemCount := a.Len() - 1; ; itemCount-- {
hasChanged := false
for index := 0; index < itemCount; index++ {
if a.Less(index+1, index) {
a.Swap(index, index+1)
hasChanged = true
}
}
if !hasChanged {
break
}
}
}</syntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def bubbleSortmakeSwap = { lista, i, j = i+1 -> print "."; a[[i,j]] = a[[j,i]] }
 
def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find { it }.each { makeSwap(a, i, j) } }
 
def bubbleSort = { list ->
boolean swapped = true
while (swapped) { swapped = (1..<list.size()).any { checkSwap(list, it-1) } }
swapped = false
(1..<list.size()).each {
boolean doSwap = (list[it - 1] > list[it])
swapped |= doSwap
if (doSwap) { list[(it - 1)..it] = list[it..(it - 1)] }
}
}
list
}</langsyntaxhighlight>
 
Test Program:
<syntaxhighlight lang="groovy">println bubbleSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4])
<lang groovy>def list = [1,6,3,5,2,9,8,4,7,0]
println bubbleSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1])</syntaxhighlight>
println list
println bubbleSort(list)</lang>
 
{{out}}
Output:
<pre>..............................................................................................................................................[4, 12, 14, 23, 24, 24, 31, 35, 38, 46, 51, 57, 57, 58, 76, 78, 89, 92, 95, 97, 99]
<pre>[1, 6, 3, 5, 2, 9, 8, 4, 7, 0]
.........................................................................................................................................[0, 1, 4, 5, 7, 8, 12, 14, 18, 20, 31, 33, 44, 62, 70, 73, 75, 76, 78, 81, 82, 84, 88]</pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>
 
=={{header|Haskell}}==
This version checks for changes in a separate step for simplicity, because Haskell has no variables to track them with.
<langsyntaxhighlight lang="haskell">bsort :: Ord a => [a] -> [a]
bsort s = case _bsort s of
t | t == s -> t
Line 878 ⟶ 4,082:
where _bsort (x:x2:xs) | x > x2 = x2:(_bsort (x:xs))
| otherwise = x:(_bsort (x2:xs))
_bsort s = s</langsyntaxhighlight>
 
This version uses the polymorphic <tt>Maybe</tt> type to designate unchanged lists. (The type signature of <tt>_bsort</tt> is now <tt>Ord a => [a] -> Maybe [a]</tt>.) It is slightly faster than the previous one.
 
<langsyntaxhighlight lang="haskell">import Data.Maybe (fromMaybe)
import Control.Monad
 
Line 890 ⟶ 4,094:
then Just $ x2 : fromMaybe (x:xs) (_bsort $ x:xs)
else liftM (x:) $ _bsort (x2:xs)
_bsort _ = Nothing</langsyntaxhighlight>
 
This version is based on the above, but avoids sorting the whole list each time. To implement this without a counter and retain using pattern matching, inner sorting is reversed, and then the result is reversed back. Sorting is based on a predicate, e.g., (<) or (>).
 
<syntaxhighlight lang="haskell">import Data.Maybe (fromMaybe)
import Control.Monad
 
bubbleSortBy :: (a -> a -> Bool) -> [a] -> [a]
bubbleSortBy f as = case innerSort $ reverse as of
Nothing -> as
Just v -> let (x:xs) = reverse v
in x : bubbleSortBy f xs
where innerSort (a:b:cs) = if b `f` a
then liftM (a:) $ innerSort (b:cs)
else Just $ b : fromMaybe (a:cs)
(innerSort $ a:cs)
innerSort _ = Nothing
 
bsort :: Ord a => [a] -> [a]
bsort = bubbleSortBy (<)</syntaxhighlight>
 
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">class BubbleSort {
@:generic
public static function sort<T>(arr:Array<T>) {
var madeChanges = false;
var itemCount = arr.length;
do {
madeChanges = false;
itemCount--;
for (i in 0...itemCount) {
if (Reflect.compare(arr[i], arr[i + 1]) > 0) {
var temp = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = temp;
madeChanges = true;
}
}
} while (madeChanges);
}
}
 
class Main {
static function main() {
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
3.5, 0.0, -4.1, -9.5];
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
'be', 'self-evident', 'that', 'all',
'men', 'are', 'created', 'equal'];
Sys.println('Unsorted Integers: ' + integerArray);
BubbleSort.sort(integerArray);
Sys.println('Sorted Integers: ' + integerArray);
Sys.println('Unsorted Floats: ' + floatArray);
BubbleSort.sort(floatArray);
Sys.println('Sorted Floats: ' + floatArray);
Sys.println('Unsorted Strings: ' + stringArray);
BubbleSort.sort(stringArray);
Sys.println('Sorted Strings: ' + stringArray);
}
}</syntaxhighlight>
 
{{out}}
<pre>
Unsorted Integers: [1,10,2,5,-1,5,-19,4,23,0]
Sorted Integers: [-19,-1,0,1,2,4,5,5,10,23]
Unsorted Floats: [1,-3.2,5.2,10.8,-5.7,7.3,3.5,0,-4.1,-9.5]
Sorted Floats: [-9.5,-5.7,-4.1,-3.2,0,1,3.5,5.2,7.3,10.8]
Unsorted Strings: [We,hold,these,truths,to,be,self-evident,that,all,men,are,created,equal]
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths]
</pre>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="fortran">SUBROUTINE Bubble_Sort(a)
REAL :: a(1)
Line 908 ⟶ 4,182:
IF (swapped == 0) RETURN
ENDDO
END</langsyntaxhighlight>
 
==Icon and Unicon==
 
==={{header|Icon}}= and {{header|Unicon}}==
Icon/Unicon implementation of a bubble sort
<langsyntaxhighlight Iconlang="icon">procedure main() #: demonstrate various ways to sort a list and string
demosort(bubblesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 929 ⟶ 4,201:
X[i-1] :=: X[swapped := i]
return X
end</langsyntaxhighlight>
 
{{out}}
Sample output:
<pre>Sorting Demo using procedure bubblesort
on list : [ 3 14 1 5 9 2 6 3 ]
Line 949 ⟶ 4,221:
* 'demosort' can apply different sorting procedures and operators to lists and strings to show how this works. The routines 'displaysort' and 'writex' are helpers.
 
<langsyntaxhighlight lang="icon">invocable all # for op
 
procedure sortop(op,X) #: select how to sort
Line 1,007 ⟶ 4,279:
write()
return
end</langsyntaxhighlight>
 
==={{header|UniconIo}}===
<syntaxhighlight lang="io">
 
List do(
This Icon solution works in Unicon. A solution that uses Unicon extensions has not been provided.
bubblesort := method(
t := true
while( t,
t := false
for( j, 0, self size - 2,
if( self at( j ) start > self at( j+1 ) start,
self swapIndices( j,j+1 )
t := true
)
)
)
return( self )
)
)
</syntaxhighlight>
 
=={{header|J}}==
{{eff note|J|/:~ list}}
<langsyntaxhighlight lang="j">bubbleSort=: (([ (<. , >.) {.@]) , }.@])/^:_</langsyntaxhighlight>
 
Test program:
 
<langsyntaxhighlight lang="j"> ?. 10 $ 10
4 6 8 6 5 8 6 6 6 9
bubbleSort ?. 10 $ 10
4 5 6 6 6 6 6 8 8 9</langsyntaxhighlight>
 
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}}==
<syntaxhighlight lang="janet">
(defn bubble-sort!
[arr]
(def arr-len (length arr))
(when (< arr-len 2)
(break arr))
# at this point there are two or more elements
(loop [i :down-to [(dec arr-len) 0]]
(for j 0 i
(def left-elt (get arr j))
(def right-elt (get arr (inc j)))
(when (> left-elt right-elt)
(put arr j right-elt)
(put arr (inc j) left-elt))))
arr)
 
(comment
 
(let [n 100
arr (seq [i :range [0 n]]
(* n (math/random)))]
(deep= (bubble-sort! (array ;arr))
(sort (array ;arr))))
# => true
 
)
</syntaxhighlight>
 
=={{header|Java}}==
Bubble sorting (ascending) an array of any <tt>Comparable</tt> type:
<langsyntaxhighlight lang="java">public static <E extends Comparable<? super E>> void bubbleSort(E[] comparable) {
boolean changed = false;
do {
Line 1,041 ⟶ 4,385:
}
} while (changed);
}</langsyntaxhighlight>
 
For descending, simply switch the direction of comparison:
<langsyntaxhighlight lang="java">if (comparable[a].compareTo(comparable[b]) < 0){
//same swap code as before
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">Array.prototype.bubblesort = function() {
var done = false;
while (!done) {
Line 1,061 ⟶ 4,405:
}
return this;
}</langsyntaxhighlight>
 
{{works with|SEE|3.0}}
{{works with|OSSP js|1.6.20070208}}
<langsyntaxhighlight lang="javascript">Array.prototype.bubblesort = function() {
var done = false;
while (! done) {
Line 1,079 ⟶ 4,423:
}
return this;
}</langsyntaxhighlight>
 
Example:
<langsyntaxhighlight lang="javascript">var my_arr = ["G", "F", "C", "A", "B", "E", "D"];
my_arr.bubblesort();
print(my_arr);</langsyntaxhighlight>
 
{{out}}
Output:
<pre>
A,B,C,D,E,F,G
</pre>
 
webpage version (for the rest of us):
=={{header|Io}}==
<syntaxhighlight lang="javascript"><script>
<lang Io>
Array.prototype.bubblesort = function() {
List do(
var done = false;
bubblesort := method(
twhile :=(!done) true{
done = true;
while( t,
for (var i = 1; i<this.length; i++) {
t := false
for( j, 0, self size if (this[i-1] > this[i]) 2,{
if( self at( j ) start > self at(done j+1= ) start,false;
[this[i-1], this[i]] = [this[i], this[i-1]]
self swapIndices( j,j+1 )
t := true}
)}
)}
)return this;
}
return( self )
var my_arr = ["G", "F", "C", "A", "B", "E", "D"];
)
my_arr.bubblesort();
)
output='';
</lang>
for (var i = 0; i < my_arr.length; i++) {
output+=my_arr[i];
if (i < my_arr.length-1) output+=',';
}
document.write(output);
</script></syntaxhighlight>
 
=={{header|jq}}==
<syntaxhighlight lang="jq">def bubble_sort:
def swap(i;j): .[i] as $x | .[i]=.[j] | .[j]=$x;
 
# input/output: [changed, list]
reduce range(0; length) as $i
( [false, .];
if $i > 0 and (.[0]|not) then .
else reduce range(0; (.[1]|length) - $i - 1) as $j
(.[0] = false;
.[1] as $list
| if $list[$j] > $list[$j + 1] then [true, ($list|swap($j; $j+1))]
else .
end )
end ) | .[1] ;</syntaxhighlight>
'''Example''':
<syntaxhighlight lang="jq">(
[3,2,1],
[1,2,3],
["G", "F", "C", "A", "B", "E", "D"]
) | bubble_sort</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="sh">$ jq -c -n -f Bubble_sort.jq
[1,2,3]
[1,2,3]
["A","B","C","D","E","F","G"]</syntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">function bubblesort!(arr::AbstractVector)
for _ in 2:length(arr), j in 1:length(arr)-1
if arr[j] > arr[j+1]
arr[j], arr[j+1] = arr[j+1], arr[j]
end
end
return arr
end
 
v = rand(-10:10, 10)
println("# unordered: $v\n -> ordered: ", bubblesort!(v))</syntaxhighlight>
 
{{out}}
<pre># unordered: [7, 4, -1, -8, 8, -1, 5, 6, -3, -5]
-> ordered: [-8, -5, -3, -1, -1, 4, 5, 6, 7, 8]</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
 
<syntaxhighlight lang="scala">import java.util.Comparator
 
fun <T> bubbleSort(a: Array<T>, c: Comparator<T>) {
var changed: Boolean
do {
changed = false
for (i in 0..a.size - 2) {
if (c.compare(a[i], a[i + 1]) > 0) {
val tmp = a[i]
a[i] = a[i + 1]
a[i + 1] = tmp
changed = true
}
}
} while (changed)
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def bubblesort
{def bubblesort.swap!
{lambda {:a :n :i}
{if {> :i :n}
then :a
else {bubblesort.swap! {if {> {A.get :i :a} {A.get {+ :i 1} :a}}
then {A.set! :i {A.get {+ :i 1} :a}
{A.set! {+ :i 1} {A.get :i :a} :a}}
else :a}
:n
{+ :i 1}} }}}
{def bubblesort.r
{lambda {:a :n}
{if {<= :n 1}
then :a
else {bubblesort.r {bubblesort.swap! :a :n 0}
{- :n 1}} }}}
 
{lambda {:a}
{bubblesort.r :a {- {A.length :a} 1}}}}
-> bubblesort
 
{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|Lisaac}}==
<langsyntaxhighlight Lisaaclang="lisaac">Section Header
 
+ name := BUBBLE_SORT;
Line 1,151 ⟶ 4,597:
};
}.do_while {!sorted};
);</langsyntaxhighlight>
 
=={{header|Lua}}==
 
<syntaxhighlight lang="lua">
<lang Lua>
function bubbleSort(A)
local itemCount=#A
Line 1,170 ⟶ 4,616:
until hasChanged == false
end
</syntaxhighlight>
</lang>
 
Example:
<langsyntaxhighlight lang="lua">
list = { 5, 6, 1, 2, 9, 14, 2, 15, 6, 7, 8, 97 }
bubbleSort(list)
Line 1,179 ⟶ 4,625:
print(j)
end
</syntaxhighlight>
</lang>
 
=={{header|Lucid}}==
[http://i.csc.uvic.ca/home/hei/lup/06.html]
<langsyntaxhighlight lang="lucid">bsort(a) = if iseod(first a) then a else
follow(bsort(allbutlast(b)),last(b)) fi
where
Line 1,198 ⟶ 4,644:
last(x) = (x asa iseod next x) fby eod;
allbutlast(x) = if not iseod(next x) then x else eod fi;
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
'''A=(1,2,3,4)''' is a pointer to an auto array. We can read one item '''Array(A,0)=1''', or we can add 1 to all items '''A++''', or add 5 to all items '''A+=5'''. We can link to standard interface, '''Link A to A()''' so now '''A(1)++''' increment 2nd item by one. '''Print A''' print all items, one per column
 
'''A=Stack:=1,2,3,4''' is a pointer to a stack object. We can read any item using '''StackItem()''', from 1 (we can omit number 1 for first item, the top). Stack items can be move very fast, it is a linked list. To apply stack statements we have to make A as current stack (preserving current stack) using Stack A { }, so we can drop 2 items (1 and 2) using '''Stack A {Drop 2}'''. '''Print A''' print all items, one per column
 
 
<syntaxhighlight lang="m2000 interpreter">
Module Bubble {
function bubblesort {
dim a()
\\ [] is a stack object, interpreter pass current stack pointer, and set a new stack for current stack
\\ array( stackobject ) get a stack object and return an array
a()=array([])
itemcount=len(a())
repeat {
haschange=false
if itemcount>1 then {
for index=0 to itemcount-2 {
if a(index)>a(index+1) then swap a(index), a(index+1) : haschange=true
}
}
itemcount--
} until not haschange
=a()
}
\\ function can take parameters
Print bubblesort(5,3,2,7,6,1)
A=(2, 10, 17, 13, 20, 14, 3, 17, 16, 16)
\\ !A copy values from array A to function stack
B=bubblesort(!A)
Print Len(A)=10
Print B
\\ Print array in descending order
k=each(b, -1, 1)
While k {
Print Array(k),
}
\\ sort two arrays in one
Print BubbleSort(!A, !B)
\\ We can use a stack object, and values pop from object
Z=Stack:=2, 10, 17, 13, 20, 14, 3, 17, 16, 16
Print Len(Z)=10
Def GetStack(x)=Stack(x)
Z1=GetStack(BubbleSort(!Z))
Print Type$(Z1)="mStiva"
Print Z1
Print Len(Z1)
Print Len(Z)=0 ' now Z is empty
}
Bubble
</syntaxhighlight>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">divert(-1)
 
define(`randSeed',141592653)
Line 1,245 ⟶ 4,744:
show(`a')
bubblesort(`a')
show(`a')</langsyntaxhighlight>
 
{{out}}
Output:
<pre>17 63 80 55 90 88 25 9 71 38
 
9 17 25 38 55 63 71 80 88 90</pre>
 
=={{header|MathematicaMaple}}==
<syntaxhighlight lang="text">arr := Array([17,3,72,0,36,2,3,8,40,0]):
A rule-based solution is only one line, for large lists this method is not optimal, not so because of the method but because of the usage of patterns in a rule based solution:
len := numelems(arr):
<lang Mathematica>BubbleSort[input_] := input //. {a___, i_, j_, b___} /; OrderedQ[{j, i}] :> {a, j, i, b}</lang>
while(true) do
Example:
change := false:
<lang Mathematica>BubbleSort[{10, 3, 7, 1, 4, 3, 8, 13, 9}]</lang>
len--;
gives back:
for i from 1 to len do
<lang Mathematica>{1, 3, 3, 4, 7, 8, 9, 10, 13}</lang>
if (arr[i] > arr[i+1]) then
temp := arr[i]:
arr[i] := arr[i+1]:
arr[i+1] := temp:
change := true:
end if:
end do:
if (not change) then break end if:
end do:
arr;</syntaxhighlight>
{{Out|Output}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===Using pattern matching===
<syntaxhighlight lang="mathematica">bubbleSort[{w___, x_, y_, z___}] /; x > y := bubbleSort[{w, y, x, z}]
bubbleSort[sortedList_] := sortedList
bubbleSort[{10, 3, 7, 1, 4, 3, 8, 13, 9}]</syntaxhighlight>
{{out}}
<pre>{1, 3, 3, 4, 7, 8, 9, 10, 13}</pre>
===Classic version===
<syntaxhighlight lang="mathematica">ClearAll[BubbleSort]
BubbleSort[in_List] := Module[{x = in, l = Length[in], swapped},
swapped = True;
While[swapped,
swapped = False;
Do[
If[x[[i]] > x[[i + 1]],
x[[{i, i + 1}]] //= Reverse;
swapped = True;
]
,
{i, l - 1}
];
];
x
]
BubbleSort[{1, 12, 3, 7, 2, 8, 4, 7, 6}]</syntaxhighlight>
{{out}}
<pre>{1, 2, 3, 4, 6, 7, 7, 8, 12}</pre>
 
=={{header|MATLAB}}==
 
<langsyntaxhighlight MATLABlang="matlab">function list = bubbleSort(list)
 
hasChanged = true;
Line 1,281 ⟶ 4,820:
end %for
end %while
end %bubbleSort</langsyntaxhighlight>
 
{{out}}
Sample Output:
<lang MATLABpre>bubbleSort([5 3 8 4 9 7 6 2 1])
 
ans =
 
1 2 3 4 5 6 7 8 9</langpre>
 
=={{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}}==
<langsyntaxhighlight lang="maxscript">fn bubbleSort arr =
(
while true do
Line 1,307 ⟶ 4,870:
)
arr
)</langsyntaxhighlight>
-- Usage
<langsyntaxhighlight lang="maxscript">myArr = #(9, 8, 7, 6, 5, 4, 3, 2, 1)
myArr = bubbleSort myArr</langsyntaxhighlight>
 
=={{header|MMIX}}==
<langsyntaxhighlight lang="mmix">Ja IS $127
 
LOC Data_Segment
Line 1,392 ⟶ 4,955:
JMP 2B % loop
1H XOR $255,$255,$255
TRAP 0,Halt,0 % exit(0)</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">PROCEDURE BubbleSort(VAR a: ARRAY OF INTEGER);
VAR
changed: BOOLEAN;
Line 1,413 ⟶ 4,976:
END
UNTIL NOT changed
END BubbleSort;</langsyntaxhighlight>
 
=={{header|Modula-3}}==
{{incorrect|Modula-3|Enters infinite loop.}}
 
<langsyntaxhighlight lang="modula3">MODULE Bubble;
 
PROCEDURE Sort(VAR a: ARRAY OF INTEGER) =
Line 1,432 ⟶ 4,994:
a[i] := a[i + 1];
a[i + 1] := temp;
sorted := FALSE;
END;
sorted := FALSE;
END;
END;
END Sort;
END Bubble.</langsyntaxhighlight>
 
=={{header|N/t/roff}}==
 
This program may output to paper (Postscript/PDF or actual printout) or a line-printer/terminal depending on the device specification.
 
This implementation is not reverse-compatible classical TROFF from Bell Labs, as TROFF then was extremely limited in what it could do. It will work with GNU Troff, though. The classical version of TROFF could only do recursive macro calls, which is integral to the functioning of <code>.AREADLN</code>, but not <code>.while</code> looping constructs, which is integral to the functioning of <code>.ASORT</code>; it could also only support numerical registers with name consisting of two characters maximum, so a register named <code>A9</code> would be okay (2 characters), but not <code>A123</code> (4 characters).
 
Block comments start with <code>.ig</code> and end with <code>..</code>. Single-line comments begin with <code>\"</code>
 
{{works with|GROFF (GNU Troff)|1.22.2}}
 
<syntaxhighlight lang="n/t/roff">
.ig
Bubble sort algorithm in Troff
==============================
 
:For: Rosetta Code
:Author: Stephanie Björk (Katt)
:Date: December 1, 2017
..
.ig
Array implementation: \(*A
---------------------------
This is an array implementation that takes advantage of Troff's numerical
registers. Registers ``Ax``, where ``x`` is a base-10 Hindu-Arabic numeral and
0 < ``x`` < \(if, are used by array \(*A. The array counter which holds the
number of items in the array is stored in register ``Ac``. This array
implementation is one-indexed (array elements count from 1), though it could be
hardcoded again to become zero-indexed depending on what the programmer favours.
..
.nr Ac 0 1 \" Array counter
.
.de APUSH
.nr A\\n+(Ac \\$1
.. \" de APUSH
.
.de AREADLN
.APUSH \\$1
.if \\n(.$>1 \{ \
. shift
. AREADLN \\$*
\} \" if \\n(.$>1
.. \" de AREADLN
.
.de ASWAP
.nr tmp \\n[A\\$1]
.nr A\\$1 \\n[A\\$2]
.nr A\\$2 \\n[tmp]
.rm tmp
.. \" de ASWAP
.
.de ASORT
.nr swapped 1
.nr Ad \\n(Ac+1
.while \\n[swapped] \{ \
. nr swapped 0
. nr Ai 1
. nr Ad -1
. while \\n(Ai<\\n(Ad \{ \
. nr Aj \\n(Ai+1
. if \\n[A\\n(Ai]>\\n[A\\n(Aj] \{ \
. ASWAP \\n(Ai \\n(Aj
. nr swapped 1
\} \" if \\n[A\\n(Ai]>\\n[A\\n(Aj]
. nr Ai +1
\} \" while \\n(Ai<\\n(Ac
\} \" while \\n[swapped]
.. \" de ASORT
.
.ig
Begin Program
-------------
The program's procedural body. Here, we push all our potentially-unsorted
integer tokens sequentially, call a subroutine to sort them, and print all the
sorted items.
..
.AREADLN 12 87 23 77 0 66 45 92 3 0 2 1 9 9 5 4 4 4 \" Our input items to sort.
.ASORT \" Sort all items in the array.
.
.\" Output sorted items
.nr Ai 0 1
.while \n(Ai<\n(Ac \n[A\n+[Ai]]
</syntaxhighlight>
 
===Output===
<code>
0 0 1 2 3 4 4 4 5 9 9 12 23 45 66 77 87 92
</code>
 
=={{header|Nanoquery}}==
{{trans|Python}}
<syntaxhighlight lang="nanoquery">def bubble_sort(seq)
changed = true
 
while changed
changed = false
for i in range(0, len(seq) - 2)
if seq[i] > seq[i + 1]
temp = seq[i]
seq[i] = seq[i + 1]
seq[i + 1] = temp
changed = true
end
end
end
return seq
end
 
if main
import Nanoquery.Util; random = new(Random)
 
testset = list(range(0, 99))
testset = random.shuffle(testset)
println testset + "\n"
testset = bubble_sort(testset)
println testset
end</syntaxhighlight>
 
{{out}}
<pre>[97, 22, 91, 60, 79, 3, 1, 2, 18, 65, 85, 88, 40, 0, 56, 94, 67, 28, 17, 55, 71, 83, 77, 44, 80, 12, 13, 14, 54, 7, 4, 5, 59, 6, 43, 62, 21, 73, 45, 30, 66, 47, 10, 35, 11, 76, 34, 58, 96, 26, 15, 49, 84, 86, 29, 16, 69, 61, 9, 50, 25, 42, 23, 63, 99, 8, 51, 46, 53, 82, 48, 31, 36, 33, 19, 87, 37, 81, 39, 92, 24, 89, 41, 52, 93, 90, 72, 64, 70, 32, 27, 78, 68, 74, 38, 57, 98, 75, 20, 95]
 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]</pre>
 
=={{header|Nemerle}}==
===Functional===
<syntaxhighlight lang="nemerle">using System;
using System.Console;
 
module Bubblesort
{
Bubblesort[T] (x : list[T]) : list[T]
where T : IComparable
{
def isSorted(y)
{
|[_] => true
|y1::y2::ys => (y1.CompareTo(y2) < 0) && isSorted(y2::ys)
}
def sort(y)
{
|[y] => [y]
|y1::y2::ys => if (y1.CompareTo(y2) < 0) y1::sort(y2::ys)
else y2::sort(y1::ys)
}
def loop(y)
{
if (isSorted(y)) y else {def z = sort(y); loop(z)}
}
match(x)
{
|[] => []
|_ => loop(x)
}
}
Main() : void
{
def empty = [];
def single = [2];
def several = [2, 6, 1, 7, 3, 9, 4];
WriteLine(Bubblesort(empty));
WriteLine(Bubblesort(single));
WriteLine(Bubblesort(several));
}
}</syntaxhighlight>
===Imperative===
{{trans|C#}}
We use an array for this version so that we can update in place. We could use a C# style list (as in the C# example), but that seemed too easy to confuse with a Nemerle style list.
<syntaxhighlight lang="nemerle">using System;
using System.Console;
 
module Bubblesort
{
public static Bubblesort[T](this x : array[T]) : void
where T : IComparable
{
mutable changed = false;
def ln = x.Length;
do
{
changed = false;
foreach (i in [0 .. (ln - 2)])
{
when (x[i].CompareTo(x[i + 1]) > 0)
{
x[i] <-> x[i + 1];
changed = true;
}
}
} while (changed);
}
Main() : void
{
def several = array[2, 6, 1, 7, 3, 9, 4];
several.Bubblesort();
foreach (i in several)
Write($"$i ");
}
}</syntaxhighlight>
 
=={{header|NetRexx}}==
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
placesList = [String -
"UK London", "US New York" -
, "US Boston", "US Washington" -
, "UK Washington", "US Birmingham" -
, "UK Birmingham", "UK Boston" -
]
sortedList = bubbleSort(String[] Arrays.copyOf(placesList, placesList.length))
 
lists = [placesList, sortedList]
loop ln = 0 to lists.length - 1
cl = lists[ln]
loop ct = 0 to cl.length - 1
say cl[ct]
end ct
say
end ln
 
return
 
method bubbleSort(list = String[]) public constant binary returns String[]
 
listLen = list.length
loop i_ = 0 to listLen - 1
loop j_ = i_ + 1 to listLen - 1
if list[i_].compareTo(list[j_]) > 0 then do
tmpstor = list[j_]
list[j_] = list[i_]
list[i_] = tmpstor
end
end j_
end i_
 
return list
</syntaxhighlight>
{{out}}
<pre style="height: 20ex; overflow: scroll;">
UK London
US New York
US Boston
US Washington
UK Washington
US Birmingham
UK Birmingham
UK Boston
 
UK Birmingham
UK Boston
UK London
UK Washington
US Birmingham
US Boston
US New York
US Washington
 
</pre>
 
===Translation of Pseudocode===
This version is a direct implementation of this task's pseudocode.
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
placesList = [String -
"UK London", "US New York" -
, "US Boston", "US Washington" -
, "UK Washington", "US Birmingham" -
, "UK Birmingham", "UK Boston" -
]
sortedList = bubbleSort(String[] Arrays.copyOf(placesList, placesList.length))
 
lists = [placesList, sortedList]
loop ln = 0 to lists.length - 1
cl = lists[ln]
loop ct = 0 to cl.length - 1
say cl[ct]
end ct
say
end ln
 
return
 
method bubbleSort(item = String[]) public constant binary returns String[]
 
hasChanged = boolean
itemCount = item.length
loop label h_ until \hasChanged
hasChanged = isFalse
itemCount = itemCount - 1
loop index = 0 to itemCount - 1
if item[index].compareTo(item[index + 1]) > 0 then do
swap = item[index]
item[index] = item[index + 1]
item[index + 1] = swap
hasChanged = isTrue
end
end index
end h_
 
return item
 
method isTrue public constant binary returns boolean
return 1 == 1
 
method isFalse public constant binary returns boolean
return \isTrue
</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">proc bubbleSort[T](a: var openarray[T]) =
var t = true
for n in countdown(a.len-2, 0):
if not t: break
t = false
for j in 0..n:
if a[j] <= a[j+1]: continue
swap a[j], a[j+1]
t = true
 
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
bubbleSort a
echo a</syntaxhighlight>
{{out}}
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
 
=={{header|Oberon-2}}==
<syntaxhighlight lang="oberon2">MODULE Bubble;
 
IMPORT Out;
 
TYPE
TItem = INTEGER;
VAR
I:LONGINT;
A:ARRAY 10 OF TItem;
PROCEDURE Init(VAR A:ARRAY OF TItem);
BEGIN
A[0] := 1; A[1] := 10; A[2] := 2; A[3] := 5;
A[4] := -1; A[5] := 5; A[6] := -19; A[7] := 4;
A[8] := 23; A[9] := 0;
END Init;
 
PROCEDURE Swap(VAR A,B:TItem);
VAR
Temp:TItem;
BEGIN
Temp := A;
A := B;
B := Temp;
END Swap;
 
PROCEDURE BubbleSort(VAR A:ARRAY OF TItem);
VAR
N,Newn,I:LONGINT;
BEGIN
N := LEN(A)-1;
REPEAT
Newn := 0;
FOR I := 1 TO N DO
IF A[I-1] > A[I] THEN
Swap(A[I-1], A[I]);
Newn := I;
END;
END;
N := Newn;
UNTIL N = 0;
END BubbleSort;
 
BEGIN
Init(A);
Out.String("Before sorting: "); Out.Ln;
FOR I := 0 TO LEN(A)-1 DO Out.Int(A[I],0); Out.Char(' '); END;
Out.Ln;
BubbleSort(A);
Out.String("After sorting: "); Out.Ln;
FOR I := 0 TO LEN(A)-1 DO Out.Int(A[I],0); Out.Char(' '); END;
Out.Ln;
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}}==
{{trans|C}}
<langsyntaxhighlight lang="objeck">
function : Swap(p : Int[]) ~ Nil {
t := p[0];
Line 1,461 ⟶ 5,420:
while (sorted = false);
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
<syntaxhighlight lang="objc">- (NSArray *) bubbleSort:(NSMutableArray *)unsorted {
BOOL done = false;
while (!done) {
done = true;
for (int i = 1; i < unsorted.count; i++) {
if ( [[unsorted objectAtIndex:i-1] integerValue] > [[unsorted objectAtIndex:i] integerValue] ) {
[unsorted exchangeObjectAtIndex:i withObjectAtIndex:i-1];
done = false;
}
}
}
return unsorted;
}
</syntaxhighlight>
 
=={{header|OCaml}}==
Line 1,467 ⟶ 5,444:
 
This version checks for changes in a separate step for simplicity.
<langsyntaxhighlight lang="ocaml">let rec bsort s =
let rec _bsort = function
| x :: x2 :: xs when x > x2 ->
Line 1,477 ⟶ 5,454:
let t = _bsort s in
if t = s then t
else bsort t</langsyntaxhighlight>
 
This version uses the polymorphic <tt>option</tt> type to designate unchanged lists. (The type signature of <tt>_bsort</tt> is now <tt>'a list -> 'a list option</tt>.) It is slightly faster than the previous one.
<langsyntaxhighlight lang="ocaml">let rec bsort s =
let rec _bsort = function
| x :: x2 :: xs when x > x2 -> begin
Line 1,496 ⟶ 5,473:
match _bsort s with
| None -> s
| Some s2 -> bsort s2</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">function s = bubblesort(v)
itemCount = length(v);
do
Line 1,506 ⟶ 5,483:
for i = 1:itemCount
if ( v(i) > v(i+1) )
tv([i,i+1]) = v([i+1,i]); % swap
v(i) = v(i+1);
v(i+1) = t;
hasChanged = true;
endif
Line 1,514 ⟶ 5,489:
until(hasChanged == false)
s = v;
endfunction</langsyntaxhighlight>
 
<langsyntaxhighlight lang="octave">v = [9,8,7,3,1,100];
disp(bubblesort(v));</langsyntaxhighlight>
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(define (bubble-sort x ??)
(define (sort-step l)
(if (or (null? l) (null? (cdr l)))
l
(if (?? (car l) (cadr l))
(cons (cadr l) (sort-step (cons (car l) (cddr l))))
(cons (car l) (sort-step (cdr l))))))
(let loop ((i x))
(if (equal? i (sort-step i))
i
(loop (sort-step i)))))
 
(print
(bubble-sort (list 1 3 5 9 8 6 4 3 2) >))
(print
(bubble-sort (iota 100) >))
(print
(bubble-sort (iota 100) <))
</syntaxhighlight>
 
{{Out}}
<pre>
(1 2 3 3 4 5 6 8 9)
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99)
(99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0)
</pre>
 
=={{header|ooRexx}}==
===Reimplementation of [[#NetRexx|NetRexx]]===
{{trans|NetRexx}}
This version exploits the &quot;Collection Classes&quot; and some other features of the language that are only available in Open Object Rexx.
<syntaxhighlight lang="oorexx">/* Rexx */
Do
placesList = sampleData()
call show placesList
say
sortedList = bubbleSort(placesList)
call show sortedList
say
 
return
End
Exit
 
-- -----------------------------------------------------------------------------
bubbleSort:
procedure
Do
il = arg(1)
sl = il~copy
 
listLen = sl~size
loop i_ = 1 to listLen
loop j_ = i_ + 1 to listLen
cmpi = sl[i_]
cmpj = sl[j_]
if cmpi > cmpj then do
sl[i_] = cmpj
sl[j_] = cmpi
end
end j_
end i_
return sl
End
Exit
 
-- -----------------------------------------------------------------------------
show:
procedure
Do
al = arg(1)
 
loop e_ over al
say e_
end e_
return
End
Exit
 
-- -----------------------------------------------------------------------------
sampleData:
procedure
Do
placesList = .array~of( ,
"UK London", "US New York", "US Boston", "US Washington", ,
"UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" ,
)
 
return placesList
End
Exit
 
</syntaxhighlight>
{{out}}
<pre style="height: 20ex; overflow: scroll;">
UK London
US New York
US Boston
US Washington
UK Washington
US Birmingham
UK Birmingham
UK Boston
 
UK Birmingham
UK Boston
UK London
UK Washington
US Birmingham
US Boston
US New York
US Washington
 
</pre>
===Translation of Pseudocode===
This version is a direct implementation of this task's pseudocode.
<syntaxhighlight lang="oorexx">/* Rexx */
Do
placesList = sampleData()
call show placesList
say
sortedList = bubbleSort(placesList)
call show sortedList
say
 
return
End
Exit
 
-- -----------------------------------------------------------------------------
bubbleSort:
procedure
Do
il = arg(1)
sl = il~copy
itemCount = sl~size
 
loop label c_ until \hasChanged
hasChanged = isFalse()
itemCount = itemCount - 1
loop i_ = 1 to itemCount
if sl[i_] > sl[i_ + 1] then do
t_ = sl[i_]
sl[i_] = sl[i_ + 1]
sl[i_ + 1] = t_
hasChanged = isTrue()
end
end i_
end c_
 
return sl
End
Exit
 
-- -----------------------------------------------------------------------------
show:
procedure
Do
al = arg(1)
 
loop e_ over al
say e_
end e_
return
End
Exit
 
-- -----------------------------------------------------------------------------
sampleData:
procedure
Do
placesList = .array~of( ,
"UK London", "US New York", "US Boston", "US Washington", ,
"UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" ,
)
 
return placesList
End
Exit
 
-- -----------------------------------------------------------------------------
isTrue: procedure
return (1 == 1)
 
-- -----------------------------------------------------------------------------
isFalse: procedure
return \isTrue()
</syntaxhighlight>
 
===Classic [[REXX|Rexx]] Implementation===
A more &quot;traditional&quot; implementation of version 1 using only Rexx primitive constructs. This version has been tested with the ''Open Object Rexx'' and ''Regina'' Rexx interpreters and could equally have been exhibited as a [[#REXX|Rexx]] solution.
<syntaxhighlight lang="oorexx">/* Rexx */
Do
placesList. = ''
sortedList. = ''
call sampleData
call bubbleSort
 
do i_ = 1 to placesList.0
say placesList.i_
end i_
say
 
do i_ = 1 to sortedList.0
say sortedList.i_
end i_
say
 
return
End
Exit
 
/* -------------------------------------------------------------------------- */
bubbleSort:
procedure expose sortedList. placesList.
Do
/* Copy list */
do !_ = 0 to placesList.0
sortedList.!_ = placesList.!_
end !_
 
listLen = sortedList.0
do i_ = 1 to listLen
do j_ = i_ + 1 to listLen
if sortedList.i_ > sortedList.j_ then do
!_ = sortedList.j_
sortedList.j_ = sortedList.i_
sortedList.i_ = !_
end
end j_
end i_
return
End
Exit
 
/* -------------------------------------------------------------------------- */
sampleData:
procedure expose placesList.
Do
! = 0
! = ! + 1; placesList.0 = !; placesList.! = "UK London"
! = ! + 1; placesList.0 = !; placesList.! = "US New York"
! = ! + 1; placesList.0 = !; placesList.! = "US Boston"
! = ! + 1; placesList.0 = !; placesList.! = "US Washington"
! = ! + 1; placesList.0 = !; placesList.! = "UK Washington"
! = ! + 1; placesList.0 = !; placesList.! = "US Birmingham"
! = ! + 1; placesList.0 = !; placesList.! = "UK Birmingham"
! = ! + 1; placesList.0 = !; placesList.! = "UK Boston"
 
return
End
Exit
</syntaxhighlight>
 
=={{header|Oz}}==
In-place sorting of mutable arrays:
<langsyntaxhighlight lang="oz">declare
proc {BubbleSort Arr}
proc {Swap I J}
Line 1,543 ⟶ 5,776:
in
{BubbleSort Arr}
{Inspect Arr}</langsyntaxhighlight>
 
Purely-functional sorting of immutable lists:
<langsyntaxhighlight lang="oz">declare
local
fun {Loop Xs Changed ?IsSorted}
Line 1,570 ⟶ 5,803:
end
in
{Show {BubbleSort [3 1 4 1 5 9 2 6 5]}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">bubbleSort(v)={
for(i=1,#v-1,
for(j=i+1,#v,
if(v[j]<v[i],
my(t=v[j]);
v[j]=v[i];
v[i]=t
)
)
);
v
};</syntaxhighlight>
 
=={{header|Pascal}}==
<syntaxhighlight lang ="pascal">procedure bubble_sort(n: integer; var list: array of real);
var
i, j, n: integer;
t: real;
begin
n := length(list);
for i := n downto 2 do
for i := n downto 2 begindo
for j := 0 to i - 1 do
if list[j] > list[j + 1] beginthen
begin
if list[j] < list[j + 1] then
t := beginlist[j];
list[j] := list[j + continue1];
list[j + 1] := endt;
end;
end;</syntaxhighlight>
 
Usage:<syntaxhighlight lang="pascal">var
t := list[j];
list[j] := listarray[j0 +.. 19] of real;
list[j + 1] := t;
end;
end;
end;</lang>
 
Usage:<lang pascal>
var
list: array[0 .. 9] of real;
// ...
bubble_sort(9, list);</syntaxhighlight>
 
</lang>
=={{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}}==
<langsyntaxhighlight lang="perl"># Sorts an array in place
sub bubble_sort {
for my $i (0 .. $#_){
Line 1,609 ⟶ 5,942:
}
}
}</langsyntaxhighlight>
 
Usage:
 
<langsyntaxhighlight lang="perl">my @a = (39, 25, 30, 28, 36, 72, 98, 25, 43, 38);
bubble_sort(@a);</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{works with|Rakudo|#24 "Seoul"}}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">bubble_sort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">changed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">sn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">si</span><span style="color: #0000FF;">></span><span style="color: #000000;">sn</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sn</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">si</span>
<span style="color: #000000;">changed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">changed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"delta"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">31</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"alfa"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">19</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"gamma"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">13</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"beta"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">782</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Before: "</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"After: "</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">bubble_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Before: {4,15,"delta",2,-31,0,"alpha",19,"gamma",2,13,"beta",782,1}
After: {-31,0,1,2,2,4,13,15,19,782,"alpha","beta","delta","gamma"}
</pre>
 
=={{header|PHP}}==
<lang perl6>sub bubble_sort (@a is rw) {
 
for ^@a -> $i {
<syntaxhighlight lang="php">function bubbleSort(array $array){
for $i ^..^ @a -> $j {
@a[foreach($j]array <as @a[$i] and @a[$i, $j] => @a[&$j, $i];val){
foreach($array as $k => &$val2){
if($k <= $i)
continue;
if($val > $val2) {
list($val, $val2) = [$val2, $val];
break;
}
}
}
return $array;
}</lang>
}</syntaxhighlight>
 
=={{header|PHPPicoLisp}}==
<syntaxhighlight lang="picolisp">(de bubbleSort (Lst)
 
(use Chg
<lang php>function bubbleSort( array &$array )
(loop
{
(off Chg)
do
(for (L Lst (cdr L) (cdr L))
{
(when (> (car L) (cadr L))
$swapped = false;
for( $i = 0, $c = count( $array ) - 1; $i < $c; $i++ (xchg L (cdr L))
(on Chg) ) )
{
(NIL Chg Lst) ) ) )</syntaxhighlight>
if( $array[$i] > $array[$i + 1] )
{
list( $array[$i + 1], $array[$i] ) =
array( $array[$i], $array[$i + 1] );
$swapped = true;
}
}
}
while( $swapped );
}</lang>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">/* A primitive bubble sort */
<lang PL/I>
/* A primitive bubble sort */
bubble_sort: procedure (A);
declare A(*) fixed binary;
Line 1,664 ⟶ 6,027:
end;
end;
end bubble_sort;</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(de bubbleSort (Lst)
(use Chg
(loop
(off Chg)
(for (L Lst (cdr L) (cdr L))
(when (> (car L) (cadr L))
(xchg L (cdr L))
(on Chg) ) )
(NIL Chg Lst) ) ) )</lang>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">define bubble_sort(v);
lvars n=length(v), done=false, i;
while not(done) do
Line 1,697 ⟶ 6,048:
vars ar = { 10 8 6 4 2 1 3 5 7 9};
bubble_sort(ar);
ar =></langsyntaxhighlight>
 
=={{header|PostScript}}==
<syntaxhighlight lang="postscript">
<lang PostScript>
/bubblesort{
/x exch def
Line 1,722 ⟶ 6,073:
x pstack
}def
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">function bubblesort ($a) {
$l = $a.Length
$hasChanged = $true
Line 1,738 ⟶ 6,089:
}
}
}</langsyntaxhighlight>
 
=={{header|PureBasicProlog}}==
It's surprisingly easy in Prolog while coding this sort, to accidentally create a sort that is similar, but not identical
<lang PureBasic>Procedure bubbleSort(Array a(1))
to the bubble sort algorithm. Some of these are easier and shorter to code and work as well if not better. Having said that,
Protected i, itemCount, hasChanged
it's difficult to think of a reason to code the bubble sort these days except as an example of inefficiency.
<syntaxhighlight lang="prolog">%___________________________________________________________________________
itemCount = ArraySize(a())
% Bubble sort
Repeat
 
hasChanged = #False
bubble(0, Res, Res, sorted).
itemCount - 1
bubble(Len, [A,B|T], Res, unsorted) :- A > B, !, bubble(Len,[B,A|T], Res, _).
For i = 0 To itemCount
bubble(Len, [A|T], [A|Ts], Ch) :- L is Len-1, bubble(L, T, Ts, Ch).
If a(i) > a(i + 1)
 
Swap a(i), a(i + 1)
bubblesort(In, Out) :- length(In, Len), bubblesort(Len, In, Out).
hasChanged = #True
bubblesort(0, In, In).
EndIf
bubblesort(Len, In, Out) :-
Next
bubble(Len, In, Bubbled, SortFlag), % bubble the list
Until hasChanged = #False
(SortFlag=sorted -> Out=Bubbled; % list is already sorted
EndProcedure</lang>
SegLen is Len - 1, % one fewer to process
writef('bubbled=%w\n', [Bubbled]), % show progress
bubblesort(SegLen, Bubbled, Out)).
 
test :- In = [8,9,1,3,4,2,6,5,4],
writef(' input=%w\n', [In]),
bubblesort(In, R),
writef('-> %w\n', [R]).</syntaxhighlight>
for example:
<pre>?- test.
input=[8,9,1,3,4,2,6,5,4]
bubbled=[8,1,3,4,2,6,5,4,9]
bubbled=[1,3,4,2,6,5,4,8,9]
bubbled=[1,3,2,4,5,4,6,8,9]
bubbled=[1,2,3,4,4,5,6,8,9]
-> [1,2,3,4,4,5,6,8,9]
true.</pre>
===Alternative version===
Should be ISO (but tested only with GNU Prolog).
Note: doesn't constuct list for each swap, only for each pass.
<syntaxhighlight lang="prolog">:- initialization(main).
 
 
bubble_sort(Xs,Res) :-
write(Xs), nl
, bubble_pass(Xs,Ys,Changed)
, ( Changed == true -> bubble_sort(Ys,Res) ; Res = Xs )
.
 
bubble_pass(Xs,Res,Changed) :-
Xs = [X|Ys], Ys = [Y|Zs]
, ( X > Y -> H = Y, T = [X|Zs], Changed = true
; H = X, T = Ys
)
, Res = [H|R], !, bubble_pass(T,R,Changed)
; Res = Xs
.
 
 
test([8,9,1,3,4,2,6,5,4]).
 
main :- test(T), bubble_sort(T,_), halt.</syntaxhighlight>
{{Out}}
<pre>[8,9,1,3,4,2,6,5,4]
[8,1,3,4,2,6,5,4,9]
[1,3,4,2,6,5,4,8,9]
[1,3,2,4,5,4,6,8,9]
[1,2,3,4,4,5,6,8,9]</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def bubble_sort(seq):
"""Inefficiently sort the mutable sequence (list) in place.
seq MUST BE A MUTABLE SEQUENCE.
Line 1,767 ⟶ 6,166:
while changed:
changed = False
for i in xrangerange(len(seq) - 1):
if seq[i] > seq[i+1]:
seq[i], seq[i+1] = seq[i+1], seq[i]
changed = True
return Noneseq
 
if __name__ == "__main__":
Line 1,778 ⟶ 6,177:
from random import shuffle
 
testset = [_ for _ in range(100)]
testcase = testset[:].copy() # make a copy
shuffle(testcase)
assert testcase != testset # we've shuffled it
bubble_sort(testcase)
assert testcase == testset # we've unshuffled it back into a copy</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ stack ] is sorted ( --> s )
[ rot tuck over peek
2swap tuck over peek
dip rot 2dup < iff
[ dip [ unrot poke ]
swap rot poke
sorted release
false sorted put ]
else
[ drop 2drop nip ] ] is >exch ( [ n n --> [ )
[ dup size 1 - times
[ true sorted put
i 1+ times
[ i^ dup 1+ >exch ]
sorted take if
conclude ] ] is bubble ( [ --> [ )
[] 20 times
[ 10 random join ]
say "Before: " dup echo cr
say "After: " bubble echo</syntaxhighlight>
 
{{out}}
 
<pre>Before: [ 5 2 5 0 4 5 1 5 1 1 0 3 7 2 0 9 6 1 8 7 ]
After: [ 0 0 0 1 1 1 1 2 2 3 4 5 5 5 5 6 7 7 8 9 ]</pre>
 
 
=={{header|Qi}}==
<syntaxhighlight lang="qi">(define bubble-shot
[A] -> [A]
[A B|R] -> [B|(bubble-shot [A|R])] where (> A B)
[A |R] -> [A|(bubble-shot R)])
 
(define bubble-sort
X -> (fix bubble-shot X))
 
(bubble-sort [6 8 5 9 3 2 2 1 4 7])
</syntaxhighlight>
 
=={{header|R}}==
The previously solution missed out on a cool R trick for swapping items and had no check for lists of length 1. As R is 1-indexed, we have aimed to be as faithful as we can to the given pseudo-code.
<lang R>bubblesort <- function(v) {
<syntaxhighlight lang="rsplus">bubbleSort <- function(items)
itemCount <- length(v)
{
repeat {
repeat
{
if((itemCount <- length(items)) <= 1) return(items)
hasChanged <- FALSE
itemCount <- itemCount - 1
for(i in 1:seq_len(itemCount) {)
{
if ( v[i] > v[i+1] ) {
if(items[i] > t <- vitems[i + 1])
v[i] <- v[i+1]{
vitems[c(i, i + 1)] <- titems[c(i + 1, i)]#The cool trick mentioned above.
hasChanged <- TRUE
}
}
if ( !hasChanged ) break;
}
vitems
}
#Examples taken from the Haxe solution.
ints <- c(1, 10, 2, 5, -1, 5, -19, 4, 23, 0)
numerics <- c(1, -3.2, 5.2, 10.8, -5.7, 7.3, 3.5, 0, -4.1, -9.5)
strings <- c("We", "hold", "these", "truths", "to", "be", "self-evident", "that", "all", "men", "are", "created", "equal")</syntaxhighlight>
 
{{out}}
v <- c(9,8,7,3,1,100)
<pre>> bubbleSort(ints)
print(bubblesort(v))</lang>
[1] -19 -1 0 1 2 4 5 5 10 23
> bubbleSort(numerics)
[1] -9.5 -5.7 -4.1 -3.2 0.0 1.0 3.5 5.2 7.3 10.8
> bubbleSort(strings)
[1] "all" "are" "be" "created" "equal" "hold" "men"
[8] "self-evident" "that" "these" "to" "truths" "We"</pre>
 
=={{header|REXXRa}}==
<syntaxhighlight lang="ra">
<lang rexx>
class BubbleSort
/*REXX program sorts an array using the bubble-sort method. */
**Sort a list with the Bubble Sort algorithm**
on start
args := program arguments
.sort(args)
print args
define sort(list) is shared
**Sort the list**
test
list := [4, 2, 7, 3]
.sort(list)
assert list = [2, 3, 4, 7]
body
last := list.count - 1
post while changed
changed := false
for i in last
if list[i] > list[i + 1]
temp := list[i]
list[i] := list[i + 1]
list[i + 1] := temp
changed := true
</syntaxhighlight>
 
=={{header|Racket}}==
call gen@ /*generate array elements. */
call show@ 'before sort' /*show before array elements*/
call bubbleSort highItem /*invoke the bubble sort. */
call show@ ' after sort' /*show after array elements*/
exit
 
This bubble sort sorts the elelement in the vector v with regard to <?.
 
<syntaxhighlight lang="racket">
/*─────────────────────────────────────BUBBLESORT subroutine───────*/
#lang racket
bubbleSort: procedure expose @.; parse arg n /*n=number of items.*/
/*diminish #items each time.*/
do until done /*sort until it's done. */
done=1 /*assume it's done (1=true).*/
 
(define (bubble-sort <? v)
do j=1 for n-1 /*sort M items this time. */
(define len (vector-length v))
k=j+1 /*point to next item. */
(define ref vector-ref)
if @.j>@.k then do /*out of order ? */
(let loop ([max len]
_=@.j /*assign to a temp variable.*/
[again? #f])
@.j=@.k /*swap current with next. */
(for ([i (in-range 0 (- max 1))]
@.k=_ /*... and next with _ */
[j (in-range 1 max)])
done=0 /*indicate it's not done. */
(define vi (ref v i))
end /* 1=true 0=false */
(when (<? (ref v j) vi)
end
(vector-set! v i (ref v j))
(vector-set! v j vi)
(set! again? #t)))
(when again? (loop (- max 1) #f)))
v)
</syntaxhighlight>
 
Example: Sorting a vector of length 10 with random entries.
end
<syntaxhighlight lang="racket">
(bubble-sort < (for/vector ([_ 10]) (random 20)))
</syntaxhighlight>
 
=={{header|Raku}}==
return
(formerly Perl 6)
{{works with|Rakudo|#24 "Seoul"}}
 
<syntaxhighlight lang="raku" line>sub bubble_sort (@a) {
for ^@a -> $i {
for $i ^..^ @a -> $j {
@a[$j] < @a[$i] and @a[$i, $j] = @a[$j, $i];
}
}
}</syntaxhighlight>
 
=={{header|REXX}}==
/*─────────────────────────────────────GEN@ subroutine─────────────*/
===version 0, alpha-numeric vertical list===
gen@: @.='' /*assign default value. */
This REXX version sorts (using a bubble sort) and displays an array &nbsp; (of alpha-numeric items) &nbsp; in a vertical list.
<syntaxhighlight lang="rexx">/*REXX program sorts an array (of any kind of items) using the bubble─sort algorithm.*/
call gen /*generate the array elements (items).*/
call show 'before sort' /*show the before array elements. */
say copies('▒', 70) /*show a separator line (before/after).*/
call bSort # /*invoke the bubble sort with # items.*/
call show ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg n /*N: is the number of @ array elements.*/
do m=n-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*elements in order? */
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap two elements; flag as not done.*/
end /*j*/
end /*m*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1 = '---letters of the Hebrew alphabet---' ; @.13= "kaph [kaf]"
@.2 = '====================================' ; @.14= "lamed"
@.3 = 'aleph [alef]' ; @.15= "mem"
@.4 = 'beth [bet]' ; @.16= "nun"
@.5 = 'gimel' ; @.17= "samekh"
@.6 = 'daleth [dalet]' ; @.18= "ayin"
@.7 = 'he' ; @.19= "pe"
@.8 = 'waw [vav]' ; @.20= "sadhe [tsadi]"
@.9 = 'zayin' ; @.21= "qoph [qof]"
@.10= 'heth [het]' ; @.22= "resh"
@.11= 'teth [tet]' ; @.23= "shin"
@.12= 'yod' ; @.24= "taw [tav]"
do #=1 until @.#==''; end; #=#-1 /*determine #elements in list; adjust #*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)":" @.j; end; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal array list:}}
<br>(Shown at &nbsp; '''<sup>5</sup>/<sub>6</sub>''' &nbsp; size.)
<pre style="font-size:84%">
element 1 before sort: ---letters of the Hebrew alphabet---
element 2 before sort: ====================================
element 3 before sort: aleph [alef]
element 4 before sort: beth [bet]
element 5 before sort: gimel
element 6 before sort: daleth [dalet]
element 7 before sort: he
element 8 before sort: waw [vav]
element 9 before sort: zayin
element 10 before sort: heth [het]
element 11 before sort: teth [tet]
element 12 before sort: yod
element 13 before sort: kaph [kaf]
element 14 before sort: lamed
element 15 before sort: mem
element 16 before sort: nun
element 17 before sort: samekh
element 18 before sort: ayin
element 19 before sort: pe
element 20 before sort: sadhe [tsadi]
element 21 before sort: qoph [qof]
element 22 before sort: resh
element 23 before sort: shin
element 24 before sort: taw [tav]
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
element 1 after sort: ---letters of the Hebrew alphabet---
element 2 after sort: ====================================
element 3 after sort: aleph [alef]
element 4 after sort: ayin
element 5 after sort: beth [bet]
element 6 after sort: daleth [dalet]
element 7 after sort: gimel
element 8 after sort: he
element 9 after sort: heth [het]
element 10 after sort: kaph [kaf]
element 11 after sort: lamed
element 12 after sort: mem
element 13 after sort: nun
element 14 after sort: pe
element 15 after sort: qoph [qof]
element 16 after sort: resh
element 17 after sort: sadhe [tsadi]
element 18 after sort: samekh
element 19 after sort: shin
element 20 after sort: taw [tav]
element 21 after sort: teth [tet]
element 22 after sort: waw [vav]
element 23 after sort: yod
element 24 after sort: zayin
</pre>
 
===version 1, random integers, horizontal list===
@.1 ='---letters of the Hebrew alphabet---'
This REXX version sorts (using a bubble sort) and displays a random array of numbers &nbsp; (amount is specifiable from the command line) &nbsp; in a horizontal list.
@.2 ='===================================='
@.3 ='aleph [alef]'
@.4 ='beth [bet]'
@.5 ='gimel'
@.6 ='daleth [dalet]'
@.7 ='he'
@.8 ='waw [vav]'
@.9 ='zayin'
@.10='heth [het]'
@.11='teth [tet]'
@.12='yod'
@.13='kaph [kaf]'
@.14='lamed'
@.15='mem'
@.16='nun'
@.17='samekh'
@.18='ayin'
@.19='pe'
@.20='sadhe [tsadi]'
@.21='qoph [qof]'
@.22='resh'
@.23='shin'
@.24='taw [tav]'
 
Programming note: &nbsp; a check was made to not exceed REXX's upper range limit of the &nbsp; '''random''' &nbsp; BIF.
do highItem=1 while @.highItem\=='' /*find how many entries. */
<syntaxhighlight lang="rexx">/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
end
parse arg N .; if N=='' | N=="," then N=30 /*obtain optional size of array from CL*/
call gen N /*generate the array elements (items). */
call show 'before sort:' /*show the before array elements. */
call bSort N /*invoke the bubble sort with N items.*/
call show ' after sort:' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg n /*N: is the number of @ array elements.*/
do m=n-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do j=1 for m; k=j+1; if @.j>@.k then parse value @.j @.k 0 with @.k @.j ok
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
end /*m*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: h=min(N+N,1e5); w=length(h); do j=1 for N; @.j=random(h); end; return
show: parse arg $; do k=1 for N; $=$ right(@.k, w); end; say $; return</syntaxhighlight>
{{out|output|text=&nbsp; when using a internally generated random array of thirty integers &nbsp; (which are right-justified for alignment in the display):}}
<pre>
before sort: 20 57 49 20 31 51 37 1 8 0 38 42 33 41 5 23 34 60 10 15 60 54 36 13 25 24 59 3 35 10
after sort: 0 1 3 5 8 10 10 13 15 20 20 23 24 25 31 33 34 35 36 37 38 41 42 49 51 54 57 59 60 60
</pre>
 
===version 2, random integers, horizontal list===
highItem=highItem-1 /*adjust highItem slightly. */
{{trans|PL/I}}
return
<syntaxhighlight lang="rexx">
/*REXX*/
Call random ,,1000
Do i=1 To 10
a.i=random(20)
End
a.0=i-1
Call show 'vorher '
Call bubble_sort
Call show 'nachher'
Exit
bubble_sort: Procedure Expose a.
Do Until no_more_swaps
no_more_swaps=1
Do i=1 To a.0-1
i1=i+1
if a.i > a.i1 Then Do
temp=a.i; a.i=a.i1; a.i1=temp
no_more_swaps=0
End
End
End
Return
show:
l=''; Do i=1 To a.0; l=l a.i; End; Say arg(1)':'l
Return</syntaxhighlight>
{{out}}
<pre>vorher : 9 17 16 19 5 7 3 20 16 0
nachher: 0 3 5 7 9 16 16 17 19 20</pre>
 
===version 3, random integers, horizontal list, with interim plots===
This REXX program is a modified version of the first REXX program, with produces a snapshot of the plot in progress.
 
The random number generation uses the numbers from &nbsp; &nbsp; '''1''' ───► '''N''' &nbsp; &nbsp; (in sequential
/*─────────────────────────────────────SHOW@ subroutine────────────*/
order), &nbsp; and then those numbers
show@: widthH=length(highItem) /*maximum width of any line.*/
<br>are randomized. &nbsp; This is done to make the displaying of the plot symmetric &nbsp; (a straight upward diagonal slope).
 
Note that the command to clear the terminal screen is hard-coded as: &nbsp; '''CLS'''
do j=1 for highItem
say 'element' right(j,widthH) arg(1)':' @.j
end
 
Also note that only four snapshots of the sort-in-progress is shown here, &nbsp; the REXX program will show a snapshot of ''every''
say copies('─',80) /*show a seperator line. */
<br>sorting pass; &nbsp; the &nbsp; &nbsp; &nbsp; ''at &nbsp; (about) &nbsp; nnn% sorted'' &nbsp; &nbsp; &nbsp; was added after-the-fact.
return
<syntaxhighlight lang="rexx">/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
</lang>
parse arg N seed . /*obtain optional size of array from CL*/
Output:
if N=='' | N=="," then N=30 /*Not specified? Then use the default.*/
<pre style="height:30ex;overflow:scroll">
if datatype(seed, 'W') then call random ,,seed /*An integer? Use the seed for RANDOM.*/
element 1 before sort: ---letters of the Hebrew alphabet---
call gen N /*generate the array elements (items). */
element 2 before sort: ====================================
call show 'before sort:' /*show the before array elements. */
element 3 before sort: aleph [alef]
$$= $ /*keep "before" copy for after the sort*/
element 4 before sort: beth [bet]
call bSort N /*invoke the bubble sort with N items.*/
element 5 before sort: gimel
say $$
element 6 before sort: daleth [dalet]
call show ' after sort:' /*show the after array elements. */
element 7 before sort: he
exit /*stick a fork in it, we're all done. */
element 8 before sort: waw [vav]
/*──────────────────────────────────────────────────────────────────────────────────────*/
element 9 before sort: zayin
bSort: procedure expose @.; parse arg # /*N: is the number of @ array elements.*/
element 10 before sort: heth [het]
call disp /*show a snapshot of the unsorted array*/
element 11 before sort: teth [tet]
do m=#-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
element 12 before sort: yod
do j=1 for m; k=j+1
element 13 before sort: kaph [kaf]
if @.j>@.k then do; parse value @.j @.k 0 with @.k @.j ok
element 14 before sort: lamed
end
element 15 before sort: mem
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
element 16 before sort: nun
call disp /*show snapshot of partially sorted @. */
element 17 before sort: samekh
end /*m*/; return
element 18 before sort: ayin
/*──────────────────────────────────────────────────────────────────────────────────────*/
element 19 before sort: pe
gen: do j=1 for N; @.j= j; end
element 20 before sort: sadhe [tsadi]
do k=1 for N; g= random(1,N); parse value @.k @.g with @.g @.k; end; return
element 21 before sort: qoph [qof]
/*──────────────────────────────────────────────────────────────────────────────────────*/
element 22 before sort: resh
show: parse arg $; do k=1 for N; $=$ right(@.k, length(N)); end; say $; return
element 23 before sort: shin
/*──────────────────────────────────────────────────────────────────────────────────────*/
element 24 before sort: taw [tav]
disp: 'CLS'; $.= /*"CLS" is the command to clear screen.*/
────────────────────────────────────────────────────────────────────────────────
do e=1 for #; $.e= '│'overlay("☼", $.e, @.e); end /*e*/
element 1 after sort: ---letters of the Hebrew alphabet---
do s=# for # by -1; say $.s; end /*s*/
element 2 after sort: ====================================
say "└"copies('─', #) /*display the horizontal axis at bottom*/
element 3 after sort: aleph [alef]
return</syntaxhighlight>
element 4 after sort: ayin
{{out|output|text=&nbsp; when using the default input:}}
element 5 after sort: beth [bet]
<pre>
element 6 after sort: daleth [dalet]
│ ☼
element 7 after sort: gimel
│ ☼
element 8 after sort: he
│ ☼
element 9 after sort: heth [het]
element 10 after sort: kaph [kaf]
│ ☼
element 11 after sort: lamed
│ ☼
element 12 after sort: mem
│ ☼
element 13 after sort: nun
│ ☼
element 14 after sort: pe
│☼
element 15 after sort: qoph [qof]
│ ☼
element 16 after sort: resh
element 17 after sort: sadhe [tsadi]
│ ☼ at 0% sorted
element 18 after sort: samekh
│ ☼
element 19 after sort: shin
│ ☼
element 20 after sort: taw [tav]
│ ☼
element 21 after sort: teth [tet]
element 22 after sort: waw [vav]
│ ☼
element 23 after sort: yod
│ ☼
element 24 after sort: zayin
│ ☼
────────────────────────────────────────────────────────────────────────────────
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
└────────────────────────
</pre>
<pre>
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼ at about 25% sorted
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│☼
│ ☼
└──────────────────────────────
</pre>
<pre>
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼ at about 50% sorted
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│☼
└──────────────────────────────
</pre>
<pre>
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼ at 100% sorted
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│☼
└──────────────────────────────
 
before sort: 11 3 15 4 12 14 7 20 22 5 8 19 24 13 6 1 16 23 17 2 10 9 21 18
after sort: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">bubbleList = [4,2,1,3]
flag = 0
bubbleSort(bubbleList)
see bubbleList
 
func bubbleSort A
n = len(A)
while flag = 0
flag = 1
for i = 1 to n-1
if A[i] > A[i+1]
temp = A[i]
A[i] = A[i+1]
A[i+1] = temp
flag = 0
ok
next
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}}==
{{eff note|Ruby|Array.sort!}}This example adds the bubblesort! method to the Array object. Below are two different methods that show four different iterating constructs in ruby.
 
<langsyntaxhighlight lang="ruby">class Array
def bubblesort1!
length.times do |j|
Line 1,955 ⟶ 6,713:
each_index do |index|
(length - 1).downto( index ) do |i|
aself[i-1], bself[i] = self[i], self[i-1], if self[i-1] < self[i]
a, b = b, a if b < a
end
end
Line 1,965 ⟶ 6,722:
ary.bubblesort1!
p ary
# => [3, 4, 6, 6, 8, 23, 78]</langsyntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn bubble_sort<T: Ord>(values: &mut[T]) {
let mut n = values.len();
let mut swapped = true;
 
while swapped {
swapped = false;
 
for i in 1..n {
if values[i - 1] > values[i] {
values.swap(i - 1, i);
swapped = true;
}
}
 
n = n - 1;
}
}
 
fn main() {
// Sort numbers.
let mut numbers = [8, 7, 1, 2, 9, 3, 4, 5, 0, 6];
println!("Before: {:?}", numbers);
 
bubble_sort(&mut numbers);
println!("After: {:?}", numbers);
 
// Sort strings.
let mut strings = ["empty", "beach", "art", "car", "deal"];
println!("Before: {:?}", strings);
 
bubble_sort(&mut strings);
println!("After: {:?}", strings);
}</syntaxhighlight>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class SORT{T < $IS_LT{T}} is
private swap(inout a, inout b:T) is
temp ::= a;
Line 1,988 ⟶ 6,780:
end;
end;
end;</langsyntaxhighlight>
 
<langsyntaxhighlight lang="sather">class MAIN is
main is
a:ARRAY{INT} := |10, 9, 8, 7, 6, -10, 5, 4|;
Line 1,996 ⟶ 6,788:
#OUT + a + "\n";
end;
end;</langsyntaxhighlight>
 
This should be able to sort (in ascending order) any object for which <code>is_lt</code> (less than) is defined.
 
=={{header|Scala}}==
{{libheader|Scala}}
This slightly more complex version of Bubble Sort avoids errors
This slightly more complex version of Bubble Sort avoids errors with indices.
with indices.
 
<langsyntaxhighlight lang="scala">def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) {
import o._
val consecutiveIndices = (arr.indices, arr.indices drop 1).zipped
Line 2,019 ⟶ 6,811:
}
} while(hasChanged)
}</langsyntaxhighlight>
 
<syntaxhighlight lang="scala">import scala.annotation.tailrec
 
def bubbleSort(xt: List[Int]) = {
@tailrec
def bubble(xs: List[Int], rest: List[Int], sorted: List[Int]): List[Int] = xs match {
case x :: Nil =>
if (rest.isEmpty) x :: sorted
else bubble(rest, Nil, x :: sorted)
case a :: b :: xs =>
if (a > b) bubble(a :: xs, b :: rest, sorted)
else bubble(b :: xs, a :: rest, sorted)
}
bubble(xt, Nil, Nil)
}</syntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (bubble-sort x gt?)
(letrec
((fix (lambda (f i)
Line 2,036 ⟶ 6,843:
(cons (car l) (sort-step (cdr l))))))))
 
(fix sort-step x)))</langsyntaxhighlight>
 
This solution iterativelyrecursively finds the fixed point of sort-step. A comparison function must be passed to bubblesort. Example usages:
<langsyntaxhighlight lang="scheme">(bubble-sort (list 1 3 5 9 8 6 4 2) >)
(bubble-sort (string->list "Monkey") char<?)</langsyntaxhighlight>
 
Here is athe recursivesame bubble sort which sorts list 'l'function, using thea comparatordifferent 'f'syntax:
 
<langsyntaxhighlight lang="scheme">(define (bsort f l gt?)
(define (dosort l)
(cond ((equalnull? (cdr l) '()) l)
l)
((f (car l) (cadr l)) (cons (cadr l) (dosort (cons (car l) (cddr l)))))
(else (cons(gt? (car l) (dosort (cdrcadr l))))))
(letcons ((rcadr l) (dosort (cons (car l) (cddr l)))))
(else
(cond ((equal? l r) l)
(cons (car l) (dosort (cdr l))))))
(else (bsort f r)))))</lang>
(let ((try (dosort l)))
(if (equal? l try)
l
(bsort try gt?))))
</syntaxhighlight>
For example, you could do
<langsyntaxhighlight lang="scheme">(bsort > '(3 2 14 6 2))
(1 2 3)</langsyntaxhighlight>
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">function b=BubbleSort(a)
n=length(a)
swapped=%T
while swapped
swapped=%F
for i=1:1:n-1
if a(i)>a(i+1) then
temp=a(i)
a(i)=a(i+1)
a(i+1)=temp
swapped=%T
end
end
end
b=a
endfunction BubbleSort</syntaxhighlight>
{{out}}
<pre style="height:20ex">-->y=[5 4 3 2 1]
y =
5. 4. 3. 2. 1.
-->x=BubbleSort(a)
x =
1. 2. 3. 4. 5. </pre>
 
=={{header|Scratch}}==
This solution is hosted at the [https://scratch.mit.edu/projects/65560042/ Scratch site], because it is difficult to document visual programming solutions directly here at Rosetta Code. There you can see the solution results as well as examine the code. This solution is intended to illustrate the Bubble sort algorithm rather than to maximize performance. Scratch provides visual queues to indicate list access, and these are used to help show what is happening.
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">const proc: bubbleSort (inout array elemType: arr) is func
local
var boolean: swapped is FALSE;
Line 2,074 ⟶ 6,914:
end for;
until not swapped;
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#bubbleSort]
 
=={{header|Shen}}==
Bubble sort a vector in-place, using the < operator for comparison.
<syntaxhighlight lang="shen">(tc +)
 
(define swap
{ (vector number) --> number --> number --> (vector number) }
A I1 I2 -> (let Z (<-vector A I1)
(do (vector-> A I1 (<-vector A I2))
(vector-> A I2 Z))))
 
(define one-pass
{ (vector number) --> number --> boolean --> number --> boolean }
A N Swapped N -> (do (if (> (<-vector A (- N 1)) (<-vector A N))
(swap A (- N 1) N))
Swapped)
A N Swapped I -> (if (> (<-vector A (- I 1)) (<-vector A I))
(do (swap A (- I 1) I)
(one-pass A N true (+ I 1)))
(one-pass A N Swapped (+ I 1))))
 
(define bubble-h
{ boolean --> (vector number) --> number --> (vector number) }
true A N -> (bubble-h (one-pass A N false 2) A N)
false A N -> A)
 
(define bubble-sort
{ (vector number) --> (vector number) }
A -> (let N (limit A)
(bubble-h (one-pass A N false 2) A N)))</syntaxhighlight>
 
<syntaxhighlight lang="shen">(datatype some-globals
 
__________
(value *arr*) : (vector number);)
 
(set *arr* (vector 5))
(vector-> (value *arr*) 1 5)
(vector-> (value *arr*) 2 1)
(vector-> (value *arr*) 3 4)
(vector-> (value *arr*) 4 2)
(vector-> (value *arr*) 5 8)
(bubble-sort (value *arr*))</syntaxhighlight>
 
Here is a more idiomatic implementation:
{{trans|Qi}}
 
<syntaxhighlight lang="shen">(tc +)
 
(define bubble-shot
{ (vector number) --> (vector number) }
(@v A <>) -> (@v A <>)
(@v A B R) -> (@v B (bubble-shot (@v A R))) where (> A B)
(@v A R) -> (@v A (bubble-shot R)))
 
(define bubble-sort
{ (vector number) --> (vector number) }
X -> (fix (function bubble-shot) X))</syntaxhighlight>
 
<syntaxhighlight lang="shen">(bubble-sort (@v 5 1 4 2 3 <>))</syntaxhighlight>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func bubble_sort(arr) {
loop {
var swapped = false
{ |i|
if (arr[i] > arr[i+1]) {
arr[i, i+1] = arr[i+1, i]
swapped = true
}
} << ^arr.end
swapped || break
}
return arr
}</syntaxhighlight>
 
=={{header|Simula}}==
<syntaxhighlight lang="simula">BEGIN
 
PROCEDURE BUBBLESORT(A); NAME A; INTEGER ARRAY A;
BEGIN
INTEGER LOW, HIGH, I;
BOOLEAN SWAPPED;
 
PROCEDURE SWAP(I, J); INTEGER I, J;
BEGIN
INTEGER TEMP;
TEMP := A(I); A(I) := A(J); A(J) := TEMP;
END**OF**SWAP;
 
LOW := LOWERBOUND(A, 1);
HIGH := UPPERBOUND(A, 1);
SWAPPED := TRUE;
WHILE SWAPPED DO
BEGIN
SWAPPED := FALSE;
FOR I := LOW + 1 STEP 1 UNTIL HIGH DO
BEGIN
COMMENT IF THIS PAIR IS OUT OF ORDER ;
IF A(I - 1) > A(I) THEN
BEGIN
COMMENT SWAP THEM AND REMEMBER SOMETHING CHANGED ;
SWAP(I - 1, I);
SWAPPED := TRUE;
END;
END;
END;
END**OF**BUBBLESORT;
 
INTEGER ARRAY A(1:10);
INTEGER I, N;
I := 1;
FOR N := 6, 8, 5, 9, 3, 2, 2, 1, 4, 7 DO
BEGIN
A(I) := N; I := I + 1;
END;
BUBBLESORT(A);
FOR I:= 1 STEP 1 UNTIL 10 DO
OUTINT(A(I), 5);
OUTIMAGE;
 
END;</syntaxhighlight>
{{out}}
<pre>
1 2 2 3 4 5 6 7 8 9
</pre>
 
=={{header|Smalltalk}}==
A straight translation from the pseudocode above. Swap is done with a [[wp:Smalltalk#Code_blocks|block closure]].
 
<langsyntaxhighlight lang="smalltalk">|item swap itemCount hasChanged|
item := #(1 4 5 6 10 8 7 61 0 -3) copy.
swap :=
Line 2,098 ⟶ 7,064:
[swap value: index value: index + 1.
hasChanged := true]].
hasChanged] whileTrue.</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
 
<langsyntaxhighlight SNOBOL4lang="snobol4">* # Sort array in place, return array
define('bubble(a,alen)i,j,ub,tmp') :(bubble_end)
bubble i = 1; ub = alen
Line 2,125 ⟶ 7,091:
sloop j = j + 1; str = str arr<j> ' ' :s(sloop)
output = trim(str)
end</langsyntaxhighlight>
 
{{out}}
Output:
<pre>33 99 15 54 1 20 88 47 68 72
1 15 20 33 47 54 68 72 88 99</pre>
Line 2,137 ⟶ 7,103:
 
Static analysis of this code shows that it is guaranteed free of any run-time error when called from any other SPARK code.
<langsyntaxhighlight Adalang="ada">package Bubble
is
 
Line 2,173 ⟶ 7,139:
 
end Bubble;
</syntaxhighlight>
</lang>
The next version has a postcondition to guarantee that the returned array is sorted correctly. This requires the two proof rules that follow the source. The Ada code is identical with the first version.
<langsyntaxhighlight Adalang="ada">package Bubble
is
Line 2,224 ⟶ 7,190:
end Bubble;
</syntaxhighlight>
</lang>
The proof rules are stated here without justification (but they are fairly obvious). A formal proof of these rules from the definition of Sorted has been completed.
<pre>
Line 2,239 ⟶ 7,205:
 
The final version scans over a reducing portion of the array in the inner loop, consequently the proof becomes more complex. The package specification for this version is the same as the second version above. The package body defines two more proof functions.
<langsyntaxhighlight Adalang="ada">package body Bubble
is
procedure Sort (A : in out Arr)
Line 2,302 ⟶ 7,268:
 
end Bubble;
</syntaxhighlight>
</lang>
Completion of the proof of this version requires more rules than the previous version and they are rather more complex. Creation of these rules is quite straightforward - I tend to write whatever rules the Simplifier needs first and then validate them afterwards. A formal proof of these rules from the definition of Sorted, In_Position and Swapped has been completed.
<pre>bubble_sort_rule(1): sorted(A, I, J)
Line 2,373 ⟶ 7,339:
</pre>
 
{{works with|SPARK GPL|2014}}
=={{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>
: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
 
File '''bubble.ads''':
[[wp:Odd-even sort|Odd-Even Bubble Sort]] (same IO):
<syntaxhighlight lang="ada">package Bubble with SPARK_Mode is
:"ODD-EVEN"
 
:L<sub>1</sub>→L<sub>2</sub>(
type Arr is array (Integer range <>) of Integer;
:1+dim(L<sub>2</sub>)→N
:For(D,1,dim(L<sub>2</sub>))
function Sorted (A : Arr) return Boolean is
:N-1→N
(for all I in A'First .. A'Last - 1 => A(I) <= A(I + 1))
:0→O
with
:For(C,1,dim(L<sub>2</sub>)-2)
Ghost,
:For(A,dim(L<sub>2</sub>)-N+2,dim(L<sub>2</sub>)-1,2)
Pre => A'Last > Integer'First;
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1)
:Then
function Bubbled (A : Arr) return Boolean is
:1→O
(for all I in A'First .. A'Last - 1 => A(I) <= A(A'Last))
:L<sub>2</sub>(A)→B
with
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
Ghost,
:B→L<sub>2</sub>(A+1)
Pre => A'Last > Integer'First;
:End
:End
procedure Sort (A : in out Arr)
:For(A,dim(L<sub>2</sub>)-N+1,dim(L<sub>2</sub>)-1,2)
with
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1)
Pre => A'Last > Integer'First and A'Last < Integer'Last,
:Then
Post => Sorted (A);
:1→O
:L<sub>2</sub>(A)→B
end Bubble;
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A)
</syntaxhighlight>
:B→L<sub>2</sub>(A+1)
 
:End
File '''bubble.adb''':
:End
<syntaxhighlight lang="ada">package body Bubble with SPARK_Mode is
:End
:If O=0
procedure Sort (A : in out Arr)
:Goto C
is
:End
Prev : Arr (A'Range) with Ghost;
:Lbl C
Done : Boolean;
:If L<sub>2</sub>(1)>L<sub>2</sub>(2)
begin
:Then
for I in reverse A'First .. A'Last - 1 loop
:L<sub>2</sub>(1)→B
Prev := A;
:L<sub>2</sub>(2)→L<sub>2</sub>(1)
Done := True;
:B→L<sub>2</sub>(2)
for J in A'First .. I loop
:End
if A(J) > A(J + 1) then
:DelVar A
declare
:DelVar B
TMP : Integer := A(J);
:DelVar C
begin
:DelVar D
A(J) := A(J + 1);
:DelVar N
A(J + 1) := TMP;
:DelVar O
Done := False;
:Return
end;
end if;
pragma Loop_Invariant (if Done then Sorted (A(A'First .. J + 1)));
pragma Loop_Invariant (Bubbled (A(A'First .. J + 1)));
pragma Loop_Invariant (A(J + 2 .. A'Last) = Prev(J + 2 .. A'Last));
pragma Loop_Invariant (for some K in A'First .. J + 1 =>
A(J + 1) = Prev(K));
end loop;
exit when Done;
pragma Loop_Invariant (if Done then Sorted (A));
pragma Loop_Invariant (Bubbled (A(A'First .. I + 1)));
pragma Loop_Invariant (Sorted (A(I + 1 .. A'Last)));
end loop;
end Sort;
end Bubble;
</syntaxhighlight>
 
File '''main.adb''':
<syntaxhighlight lang="ada">with Ada.Integer_Text_IO;
with Bubble;
 
procedure Main is
A : Bubble.Arr := (5,4,6,3,7,2,8,1,9);
begin
Bubble.Sort (A);
for I in A'Range loop
Ada.Integer_Text_IO.Put (A(I));
end loop;
end Main;
</syntaxhighlight>
 
File '''bubble.gpr''':
<syntaxhighlight lang="ada">project Bubble is
 
for Main use ("main.adb");
 
end Bubble;
</syntaxhighlight>
 
To verify the program, execute the command: '''gnatprove -P bubble.gpr -j0 --level=2'''
 
File '''gnatprove/gnatprove.out''':
<pre>Summary of SPARK analysis
=========================
 
--------------------------------------------------------------------------------------------------------------------
SPARK Analysis results Total Flow Interval CodePeer Provers Justified Unproved
--------------------------------------------------------------------------------------------------------------------
Data Dependencies . . . . . . .
Flow Dependencies . . . . . . .
Initialization 6 6 . . . . .
Non-Aliasing . . . . . . .
Run-time Checks 36 . . . 36 (CVC4) . .
Assertions 14 . . . 14 (CVC4 64%, Z3 36%) . .
Functional Contracts 7 . . . 7 (CVC4 89%, Z3 11%) . .
LSP Verification . . . . . . .
--------------------------------------------------------------------------------------------------------------------
Total 63 6 (10%) . . 57 (90%) . .
 
 
Analyzed 2 units
in unit bubble, 4 subprograms and packages out of 4 analyzed
Bubble at bubble.ads:1 flow analyzed (0 errors, 0 checks and 0 warnings) and proved (0 checks)
Bubble.Bubbled at bubble.ads:11 flow analyzed (0 errors, 0 checks and 0 warnings) and proved (3 checks)
Bubble.Sort at bubble.ads:17 flow analyzed (0 errors, 0 checks and 0 warnings) and proved (50 checks)
Bubble.Sorted at bubble.ads:5 flow analyzed (0 errors, 0 checks and 0 warnings) and proved (4 checks)
in unit main, 0 subprograms and packages out of 1 analyzed
Main at main.adb:4 skipped
</pre>
 
=={{header|Standard ML}}==
Assumes a list of integers.
<syntaxhighlight lang="sml">
fun bubble_select [] = []
| bubble_select [a] = [a]
| bubble_select (a::b::xs) =
if b < a then b::(bubble_select(a::xs)) else a::(bubble_select(b::xs))
fun bubblesort [] = []
| bubblesort (x::xs) =bubble_select (x::(bubblesort xs))
</syntaxhighlight>
 
=={{header|Stata}}==
<syntaxhighlight lang="stata">mata
function bubble_sort(a) {
n = length(a)
for (j = n; j >= 2; j--) {
q = 1
for (i = 2; i <= j; i++) {
if (a[i-1] > a[i]) {
q = 0
s = a[i-1]
a[i-1] = a[i]
a[i] = s
}
}
if (q) return
}
}
end</syntaxhighlight>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">func bubbleSort<T:Comparable>(list:inout[T]) {
var done = false
while !done {
done = true
for i in 1..<list.count {
if list[i - 1] > list[i] {
(list[i], list[i - 1]) = (list[i - 1], list[i])
done = false
}
}
}
}
 
var list1 = [3, 1, 7, 5, 2, 5, 3, 8, 4]
print(list1)
bubbleSort(list: &list1)
print(list1)</syntaxhighlight>
 
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
 
x : 23 : 15 : 99 : 146 : 3 : 66 : 71 : 5 : 23 : 73 : 19
 
bubble_sort param index size
+ index size limit
lp
changes
- limit
index i
if i < limit
+ 1 i ip1
if base.i > base.ip1
swap base.i base.ip1
+ changes
endif
+ i
goif
endif
if changes > 0
go lp
endif
return
 
 
start
' original values : ' $r
call showvalues
call bubble_sort @x #x
' sorted values : ' $r
call showvalues
stop
showvalues
$s
i
if i < #x
"$s ' ' x.i ' '" $s
+ i
goif
endif
" $r $s " []
return
</syntaxhighlight>
 
=={{header|Tailspin}}==
<syntaxhighlight lang="tailspin">
templates bubblesort
templates bubble
@: 1;
1..$-1 -> #
$@ !
when <?($@bubblesort($+1) <..~$@bubblesort($)>)> do
@: $;
def temp: $@bubblesort($@);
@bubblesort($@): $@bubblesort($@+1);
@bubblesort($@+1): $temp;
end bubble
@: $;
$::length -> #
$@ !
when <2..> do
$ -> bubble -> #
end bubblesort
[4,5,3,8,1,2,6,7,9,8,5] -> bubblesort -> !OUT::write
</syntaxhighlight>
 
=={{header|Tcl}}==
{{tcllib|struct::list}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require struct::list
 
Line 2,476 ⟶ 7,601:
}
 
puts [bubblesort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</langsyntaxhighlight>
 
Idiomatic code uses the builtin <code>lsort</code> instead, which is a stable O(''n'' log ''n'') sort.
Line 2,483 ⟶ 7,608:
Toka does not have a bubble sort predefined, but it is easy to code a simple one:
 
<langsyntaxhighlight lang="toka">#! A simple Bubble Sort function
value| array count changed |
[ ( address count -- )
Line 2,520 ⟶ 7,645:
foo 10 .array
foo 10 bsort
foo 10 .array</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
 
<syntaxhighlight lang="torquescript">//Note that we're assuming that the list of numbers is separated by tabs.
function bubbleSort(%list)
{
%ct = getFieldCount(%list);
for(%i = 0; %i < %ct; %i++)
{
for(%k = 0; %k < (%ct - %i - 1); %k++)
{
if(getField(%list, %k) > getField(%list, %k+1))
{
%tmp = getField(%list, %k);
%list = setField(%list, %k, getField(%list, %k+1));
%list = setField(%list, %k+1, %tmp);
}
}
}
return %list;
}</syntaxhighlight>
 
=={{header|Unicon}}==
Line 2,526 ⟶ 7,672:
 
=={{header|UnixPipes}}==
<langsyntaxhighlight lang="bash">rm -f _sortpass
 
reset() {
Line 2,548 ⟶ 7,694:
}
 
cat to.sort | bubblesort</langsyntaxhighlight>
 
=={{header|Ursala}}==
The bubblesort function is parameterized by a relational predicate.
<langsyntaxhighlight Ursalalang="ursala">#import nat
 
bubblesort "p" = @iNX ^=T ^llPrEZryPrzPlrPCXlQ/~& @l ~&aitB^?\~&a "p"?ahthPX/~&ahPfatPRC ~&ath2fahttPCPRC
Line 2,558 ⟶ 7,704:
#cast %nL
 
example = bubblesort(nleq) <362,212,449,270,677,247,567,532,140,315></langsyntaxhighlight>
{{out}}
output:
<pre><140,212,247,270,315,362,449,532,567,677></pre>
 
=={{header|VBScriptVala}}==
<syntaxhighlight lang="vala">void swap(int[] array, int i1, int i2) {
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.
if (array[i1] == array[i2])
return;
var tmp = array[i1];
array[i1] = array[i2];
array[i2] = tmp;
}
 
void bubble_sort(int[] array) {
For those unfamiliar with Perth, WA Australia, the five strings being sorted are names of highways.
bool flag = true;
int j = array.length;
while(flag) {
flag = false;
for (int i = 1; i < j; i++) {
if (array[i] < array[i - 1]) {
swap(array, i - 1, i);
flag = true;
}
}
j--;
}
}
 
void main() {
=====Implementation=====
int[] array = {5, -1, 101, -4, 0, 1, 8, 6, 2, 3};
<lang vb>
bubble_sort(array);
sub decr( byref n )
foreach (int i in array)
n = n - 1
print("%d ", i);
end sub
}</syntaxhighlight>
{{out}}
<pre>
-4 -1 0 1 2 3 5 6 8 101
</pre>
 
=={{header|V (Vlang)}}==
sub incr( byref n )
<syntaxhighlight lang="v (vlang)">fn bubble(mut arr []int) {
n = n + 1
println('Input: ${arr.str()}')
end sub
mut count := arr.len
for {
if count <= 1 {
break
}
mut has_changed := false
count--
for i := 0; i < count; i++ {
if arr[i] > arr[i + 1] {
temp := arr[i + 1]
arr[i + 1] = arr[i]
arr[i] = temp
has_changed = true
}
}
if !has_changed {
break
}
}
println('Output: ${arr.str()}')
}
 
fn main() {
sub swap( byref a, byref b)
mut arr := [3, 5, 2, 1, 4]
dim tmp
bubble(mut arr)
tmp = a
}</syntaxhighlight>
a = b
{{out}}
b = tmp
<pre>Input: [3, 5, 2, 1, 4]
end sub
Output: [1, 2, 3, 4, 5]</pre>
 
=={{header|Wren}}==
function bubbleSort( a )
Based on the pseudo-code in the Wikipedia article.
dim changed
<syntaxhighlight lang="wren">var bubbleSort = Fn.new { |a|
dim itemCount
var n = a.count
itemCount = ubound(a)
if (n < 2) return
do
while (true) {
changed = false
var swapped = false
decr itemCount
for (i = 0in to1..n-1) itemCount{
if a(a[i)-1] > a([i+1]) then{
var t = a[i-1]
swap a(i), a(i+1)
a[i-1] = a[i]
changed = true
a[i] = t
end if
swapped = true
next
}
loop until not changed
}
bubbleSort = a
if (!swapped) return
end function
}
</lang>
}
 
var array = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
=====Invocation=====
for (a in array) {
<lang vb>
System.print("Before: %(a)")
dim a
bubbleSort.call(a)
a = array( "great eastern", "roe", "stirling", "albany", "leach")
System.print("After : %(a)")
wscript.echo join(a,", ")
System.print()
bubbleSort a
}</syntaxhighlight>
wscript.echo join(a,", ")
</lang>
 
{{out}}
=====Output=====
<pre>
Before: [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
great eastern, roe, stirling, albany, leach
After : [-31, 0, 1, 2, 2, 4, 65, 83, 99, 782]
albany, great eastern, leach, roe, stirling
 
Before: [7, 5, 2, 6, 1, 4, 2, 6, 3]
After : [1, 2, 2, 3, 4, 5, 6, 6, 7]
</pre>
 
=={{header|VisualX86 Basic .NETAssembly}}==
Translation of XPL0. Assemble with tasm, tlink /t
'''Platform:''' [[.NET]]
<syntaxhighlight lang="asm"> .model tiny
.code
.486
org 100h
start: mov si, offset array
mov ax, 40 ;length of array (not including $)
call bsort
mov dx, si ;point to array
mov ah, 09h ;display it as a string
int 21h
ret
array db "Pack my box with five dozen liquor jugs.$"
 
;Bubble sort: si = array addrsss, ax = number of bytes
{{works with|Visual Basic .NET|9.0+}}
bsort: pusha
<lang vbnet>Do Until NoMoreSwaps = True
xchg cx, ax ;get size of array N
NoMoreSwaps = True
dec cx ;for J:= N-1 downto 0
For Counter = 1 To (NumberOfItems - 1)
bs10: xor bx, bx ;for I:= 0 to J-1
If List(Counter) > List(Counter + 1) Then
bs20: mov ax, NoMoreSwaps = False[bx+si]
cmp Tempal, =ah List ;if A(CounterI) > A(I+1) then
jbe List(Counter) = List(Counter + 1)bs30
xchg al, List(Counterah + 1) = Temp ; swap bytes
Endmov If [bx+si], ax
bs30: inc bx ;next I
Next
cmp bx, cx
NumberOfItems = NumberOfItems - 1
jb bs20
Loop</lang>
loop bs10
popa
ret
end start</syntaxhighlight>
 
{{out}}
<pre>
.Pabcdeefghiiijklmnoooqrstuuvwxyz
</pre>
 
=={{header|Xojo}}==
<syntaxhighlight lang="xojo">Dim temp, count As Integer
Dim isDirty As Boolean
count = Ubound(list) // count the array size
 
// loop through until we don't move any numbers... this means we are sorted
Do
isDirty = False // we haven't touched anything yet
For i As Integer = 1 To count - 1 // loop through all the numbers
If list(i) > list(i + 1) Then // if the right number is smaller then the left.. swap
temp = list(i + 1)
list(i + 1) = list(i)
list(i) = temp
isDirty = True // we touched the data so mark it as dirty
End
Next
Loop Until isDirty = False // if we made it without touching the data then we are done</syntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
string 0; \use zero-terminated strings
 
proc BSort(A, N); \Bubble sort array in ascending order
char A; \address of array
int N; \number of items in array (size)
int I, J, T;
[for J:= N-1 downto 0 do
for I:= 0 to J-1 do
if A(I) > A(I+1) then
[T:= A(I); A(I):= A(I+1); A(I+1):= T]; \swap items
]; \BSort
 
func StrLen(Str); \Return number of characters in an ASCIIZ string
char Str;
int I;
for I:= 0 to -1>>1-1 do
if Str(I) = 0 then return I;
 
char Str;
[Str:= "Pack my box with five dozen liquor jugs.";
BSort(Str, StrLen(Str));
Text(0, Str); CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
" .Pabcdeefghiiijklmnoooqrstuuvwxyz"
</pre>
 
=={{header|Yorick}}==
<langsyntaxhighlight lang="yorick">func bubblesort(&items) {
itemCount = numberof(items);
do {
Line 2,647 ⟶ 7,912:
}
} while(hasChanged);
}</langsyntaxhighlight>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn bubbleSort(list){
itemCount := list.len();
do{
hasChanged := False;
foreach index in (itemCount -= 1){
if (list[index] > list[index + 1]){
list.swap(index,index + 1);
hasChanged = True;
}
}
}while(hasChanged);
list
}</syntaxhighlight>
Or, punting early termination:
<syntaxhighlight lang="zkl">fcn bubbleSort(list){
foreach n,index in ([list.len()-1..0,-1],n){
if (list[index] > list[index + 1]) list.swap(index,index + 1);
}
list
}</syntaxhighlight>
<syntaxhighlight lang="zkl">bubbleSort("This is a test".split("")).println();</syntaxhighlight>
{{out}}
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre>
 
{{omit from|GUISS}}
{{omit from|Tiny BASIC}}
23

edits