Jump to content

Sorting algorithms/Bubble sort: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 40:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F bubble_sort(&seq)
V changed = 1B
L changed == 1B
Line 54:
assert(testcase != testset)
bubble_sort(&testcase)
assert(testcase == testset)</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="360 Assemblyassembly">* Bubble Sort 01/11/2014 & 23/06/2016
BUBBLE CSECT
USING BUBBLE,R13,R12 establish base registers
Line 117:
RN EQU 7 n-1
RM EQU 8 more
END BUBBLE</langsyntaxhighlight>
{{out}}
<pre>
Line 124:
=={{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. ;)
<langsyntaxhighlight lang="6502asm">define z_HL $00
define z_L $00
define z_H $01
Line 178:
jmp BUBBLESORT
DoneSorting:
rts</langsyntaxhighlight>
{{out}}
<pre>
Line 201:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program bubbleSort64.s */
Line 371:
.include "../includeARM64.inc"
 
</syntaxhighlight>
</lang>
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(defun bubble (xs)
(if (endp (rest xs))
(mv nil xs)
Line 398:
 
(defun bsort (xs)
(bsort-r xs (len xs)))</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Line 456:
Test(c,8)
Test(d,12)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bubble_sort.png Screenshot from Atari 8-bit computer]
Line 482:
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">public function bubbleSort(toSort:Array):Array
{
var changed:Boolean = false;
Line 504:
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 552:
end loop;
New_Line;
end Main;</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|A60}}
<langsyntaxhighlight lang="algol60">begin
comment Sorting algorithms/Bubble sort - Algol 60;
integer nA;
Line 603:
writetable(1,nA)
end
end </langsyntaxhighlight>
{{out}}
<pre>
Line 611:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">MODE DATA = INT;
PROC swap = (REF[]DATA slice)VOID:
(
Line 642:
sort(random);
printf(($"After: "10(g(3))l$,random))
)</langsyntaxhighlight>
{{out}}
<pre>
Line 650:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% As algol W does not allow overloading, we have to have type-specific %
% sorting procedures - this bubble sorts an integer array %
Line 709:
writeData;
end test
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 720:
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.
 
<langsyntaxhighlight lang="applescript">-- In-place bubble sort.
on bubbleSort(theList, l, r) -- Sort items l thru r of theList.
set listLen to (count theList)
Line 757:
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</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight 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}</langsyntaxhighlight>
 
=={{header|Arendelle}}==
Line 794:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 952:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">bubbleSort: function [items][
len: size items
loop len [j][
Line 971:
]
 
print bubbleSort [3 1 2 8 5 7 9 4 6]</langsyntaxhighlight>
 
{{out}}
Line 978:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">var =
(
dog
Line 1,012:
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 1,035:
print line[i]
}
}</langsyntaxhighlight>
 
GNU awk contains built in functions for sorting, but POSIX
Line 1,046:
variables.
 
<langsyntaxhighlight lang="awk">
# Test this example file from command line with:
#
Line 1,092:
exit
}
</syntaxhighlight>
</lang>
 
=={{header|bash}}==
Line 1,098:
I hope to see vastly improved versions of bubble_sort.
 
<langsyntaxhighlight lang="bash">
$ function bubble_sort() {
local a=("$@")
Line 1,139:
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
$
</syntaxhighlight>
</lang>
 
=={{header|BASIC}}==
Line 1,146:
{{trans|Java}}
Assume numbers are in a DIM of size "size" called "nums".
<langsyntaxhighlight lang="qbasic">
DO
changed = 0
Line 1,157:
END IF
NEXT
LOOP WHILE(NOT changed)</langsyntaxhighlight>
 
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight lang="basic">0 GOSUB 7 : IC = I%(0)
1 FOR HC = -1 TO 0
2 LET IC = IC - 1
Line 1,169:
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</langsyntaxhighlight>
 
==={{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.
<langsyntaxhighlight lang="basic"> 10 LET S$="FIRE BURN AND CAULDRON BUBBLE"
20 PRINT S$
30 LET L=LEN S$-1
Line 1,186:
120 NEXT I
130 LET L=L-1
140 IF C THEN GOTO 40</langsyntaxhighlight>
{{out}}
<pre> AABBBBCDDEEFILLNNNORRRUUU</pre>
Line 1,192:
==={{header|BASIC256}}===
{{works with|BASIC256 }}
<langsyntaxhighlight lang="basic256">
Dim a(11): ordered=false
print "Original set"
Line 1,217:
print a[n]+", ";
next n
</syntaxhighlight>
</lang>
{{out}}(example)
<pre>
Line 1,228:
==={{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.
<langsyntaxhighlight lang="bbcbasic"> DIM test(9)
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
PROCbubblesort(test(), 10)
Line 1,249:
n% = l%
UNTIL l% = 0
ENDPROC</langsyntaxhighlight>
{{out}}
<pre>
Line 1,257:
 
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="commodore">
<lang COMMODORE>
5 REM ===============================================
10 REM HTTP://ROSETTACODE.ORG/
Line 1,305:
910 DATA 15
920 DATA 64,34,25,12,22,11,90,13,59,47,19,89,10,17,31
</syntaxhighlight>
</lang>
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">10 REM GENERATE A RANDOM BUNCH OF INTEGERS
20 DIM ARR(20)
30 RANDOMIZE TIMER
Line 1,331:
1020 ARR(I+1) = TEMP
1030 CHANGED = 1
1040 RETURN</langsyntaxhighlight>
{{out}}<pre>
20 59 42 9 5 91 6 64 21 28 65 96 20 66 66 70 91 98 63 31
Line 1,349:
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "BubblSrt.bas"
110 RANDOMIZE
120 NUMERIC ARRAY(-5 TO 9)
Line 1,374:
330 NEXT
340 LOOP WHILE CH
350 END DEF</langsyntaxhighlight>
 
==={{header|BaCon}}===
Numeric example:
<langsyntaxhighlight lang="bacon">LOCAL t[] = { 5, 7, 1, 3, 10, 2, 9, 4, 8, 6 }
total = 10
WHILE total > 1
Line 1,386:
DECR total
WEND
PRINT COIL$(10, STR$(t[_-1]))</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10</pre>
String example:
<langsyntaxhighlight lang="bacon">t$ = "Kiev Amsterdam Lima Moscow Warschau Vienna Paris Madrid Bonn Bern Rome"
total = AMOUNT(t$)
WHILE total > 1
Line 1,398:
DECR total
WEND
PRINT t$</langsyntaxhighlight>
{{out}}
<pre>Amsterdam Bern Bonn Kiev Lima Madrid Moscow Paris Rome Vienna Warschau</pre>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let bubblesort(v, length) be
Line 1,425:
for i=0 to 9 do writef("%N ", v!i)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
void bubble_sort (int *a, int n) {
Line 1,459:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,468:
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|3.0+}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 1,511:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
Uses C++11. Compile with
g++ -std=c++11 bubble.cpp
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
Line 1,539:
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,548:
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 1,574:
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 1,603:
(recur less? result)))))
(println (bubble-sort [10 9 8 7 6 5 4 3 2 1]))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight 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)
Line 1,641:
bubble_sort[int](test)
stream$puts(po, "After: ") print_arr[int](test, 3, po)
end start_up</langsyntaxhighlight>
{{out}}
<pre>Before: 7 -5 0 2 99 16 4 20 47 19
Line 1,649:
Only for lists of integers.
 
<langsyntaxhighlight 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].
Line 1,674:
endforeach(index)
set("${var}" "${answer}" PARENT_SCOPE)
endfunction(bubble_sort)</langsyntaxhighlight>
 
<langsyntaxhighlight lang="cmake">bubble_sort(result 33 11 44 22 66 55)
message(STATUS "${result}")</langsyntaxhighlight>
 
<pre>-- 11;22;33;44;55;66</pre>
Line 1,684:
This is a complete program that demonstrates the bubble sort algorithm in COBOL.
<br/>This version is for COBOL-74 which does not have in-line performs, nor END-IF and related constructs.
<langsyntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. BUBBLESORT.
Line 1,830:
F-999.
EXIT.
</syntaxhighlight>
</lang>
 
A more modern version of COBOL.
<langsyntaxhighlight lang="cobol">
identification division.
program-id. BUBBLSRT.
Line 1,888:
end-perform
.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,895:
=={{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 1,904:
(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 1,929:
(etypecase sequence
(list (bubble-sort-list sequence predicate))
(vector (bubble-sort-vector sequence predicate))))</langsyntaxhighlight>
 
=={{header|Cowgol}}==
 
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Comparator interface, on the model of C, i.e:
Line 1,987:
i := i + 1;
end loop;
print_nl();</langsyntaxhighlight>
 
{{out}}
Line 1,994:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm : swap;
 
T[] bubbleSort(T)(T[] data) pure nothrow
Line 2,017:
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
writeln(array.bubbleSort());
}</langsyntaxhighlight>
{{out}}
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
Line 2,047:
 
Static array is an arbitrary-based array of fixed length
<langsyntaxhighlight Delphilang="delphi">program TestBubbleSort;
 
{$APPTYPE CONSOLE}
Line 2,100:
Writeln;
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,108:
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">/* Bubble sort an array of integers */
proc nonrec bubblesort([*] int a) void:
bool sorted;
Line 2,138:
write("After sorting: ");
for i from 0 upto 9 do write(a[i]:5) od
corp</langsyntaxhighlight>
{{out}}
<pre>Before sorting: 9 -5 3 3 24 -16 3 -120 250 17
Line 2,145:
=={{header|Dyalect}}==
 
<langsyntaxhighlight lang="dyalect">func bubbleSort(list) {
var done = false
while !done {
Line 2,162:
var xs = [3,1,5,4,2,6]
bubbleSort(xs)
print(xs)</langsyntaxhighlight>
 
{{out}}
Line 2,169:
 
=={{header|E}}==
<langsyntaxhighlight lang="e">def bubbleSort(target) {
__loop(fn {
var changed := false
Line 2,181:
changed
})
}</langsyntaxhighlight>
 
(Uses the primitive __loop directly because it happens to map to the termination test for this algorithm well.)
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; sorts a vector of objects in place
;; proc is an user defined comparison procedure
Line 2,203:
(bubble-sort V sort/length)
→ #(zen simon elvis albert antoinette)
</syntaxhighlight>
</lang>
 
=={{header|EDSAC order code}}==
Line 2,212:
 
To clarify the EDSAC program, an equivalent Pascal program is added as a comment.
<langsyntaxhighlight lang="edsac">
[Bubble sort demo for Rosetta Code website]
[EDSAC program. Initial Orders 2]
Line 2,364:
E 14 Z [define entry point]
P F [acc = 0 on entry]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,384:
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 2,413:
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 2,452:
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 2,473:
{{trans|C#}}
ELENA 5.0 :
<langsyntaxhighlight lang="elena">import system'routines;
import extensions;
Line 2,506:
var list := new int[]{3, 7, 3, 2, 1, -4, 10, 12, 4};
console.printLine(list.bubbleSort().asEnumerable())
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,513:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Sort do
def bsort(list) when is_list(list) do
t = bsort_iter(list)
Line 2,523:
def bsort_iter([x, y | t]), do: [x | bsort_iter([y | t])]
def bsort_iter(list), do: list
end</langsyntaxhighlight>
 
=={{header|Erlang}}==
sort/3 copied from Stackoverflow.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( bubble_sort ).
 
Line 2,544:
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>
</lang>
{{out}}
<pre>
Line 2,552:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM BUBBLE_SORT
 
Line 2,584:
PRINT
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function bubble_sort(sequence s)
object tmp
integer changed
Line 2,613:
pretty_print(1,s,{2})
puts(1,"\nAfter: ")
pretty_print(1,bubble_sort(s),{2})</langsyntaxhighlight>
 
{{out}}
Line 2,650:
 
=={{header|Ezhil}}==
<syntaxhighlight lang="ezhil">
<lang Ezhil>
 
## இந்த நிரல் ஒரு பட்டியலில் உள்ள எண்களை Bubble Sort என்ற முறைப்படி ஏறுவரிசையிலும் பின்னர் அதையே இறங்குவரிசையிலும் அடுக்கித் தரும்
Line 2,718:
பதிப்பி எண்கள்பிரதி
 
</syntaxhighlight>
</lang>
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let BubbleSort (lst : list<int>) =
let rec sort accum rev lst =
match lst, rev with
Line 2,729:
| head::tail, _ -> sort (head::accum) rev tail
sort [] true lst
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: fry kernel locals math math.order sequences
sequences.private ;
IN: rosetta.bubble
Line 2,754:
 
: 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 }
Line 2,770:
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.
 
<langsyntaxhighlight lang="fish">v Sorts the (pre-loaded) stack
with bubblesort.
v <
Line 2,777:
>~{ao ^
>~}l &{ v
o","{n:&-1^?=0:&<</langsyntaxhighlight>
 
=={{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 2,790:
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 2,811:
then
cell +loop ( sorted )
until 2drop ;</langsyntaxhighlight>
 
Test any version with this:
Line 2,827:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">SUBROUTINE Bubble_Sort(a)
REAL, INTENT(in out), DIMENSION(:) :: a
REAL :: temp
Line 2,845:
IF (.NOT. swapped) EXIT
END DO
END SUBROUTINE Bubble_Sort</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
Per task pseudo code:
<langsyntaxhighlight FreeBASIClang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Line 2,895:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>unsort -7 3 -4 -6 4 -1 -2 2 7 0 5 1 -3 -5 6
Line 2,901:
 
=={{header|g-fu}}==
<langsyntaxhighlight lang="g-fu">
(fun bubbles (vs)
(let done? F n (len vs))
Line 2,917:
---
(1 2 3)
</syntaxhighlight>
</lang>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=ba84832d633cb92bbe6c2f54704819c3 Click this link to run this code]'''
<langsyntaxhighlight 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]
Line 2,952:
Print
 
End</langsyntaxhighlight>
Output:
<pre>
Line 2,982:
=={{header|Go}}==
Per task pseudocode:
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 3,007:
}
}
}</langsyntaxhighlight>
 
More generic version that can sort anything that implements <code>sort.Interface</code>:
<langsyntaxhighlight lang="go">package main
 
import (
Line 3,038:
}
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def makeSwap = { a, 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) } }
Line 3,050:
while (swapped) { swapped = (1..<list.size()).any { checkSwap(list, it-1) } }
list
}</langsyntaxhighlight>
 
Test Program:
<langsyntaxhighlight 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])
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])</langsyntaxhighlight>
 
{{out}}
Line 3,062:
=={{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 3,068:
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 3,080:
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 (>).
 
<langsyntaxhighlight lang="haskell">import Data.Maybe (fromMaybe)
import Control.Monad
 
Line 3,099:
 
bsort :: Ord a => [a] -> [a]
bsort = bubbleSortBy (<)</langsyntaxhighlight>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">class BubbleSort {
@:generic
public static function sort<T>(arr:Array<T>) {
Line 3,140:
Sys.println('Sorted Strings: ' + stringArray);
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,153:
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="fortran">SUBROUTINE Bubble_Sort(a)
REAL :: a(1)
Line 3,168:
IF (swapped == 0) RETURN
ENDDO
END</langsyntaxhighlight>
 
=={{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 3,187:
X[i-1] :=: X[swapped := i]
return X
end</langsyntaxhighlight>
 
{{out}}
Line 3,207:
* '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 3,265:
write()
return
end</langsyntaxhighlight>
 
=={{header|Io}}==
<syntaxhighlight lang="io">
<lang Io>
List do(
bubblesort := method(
Line 3,284:
)
)
</syntaxhighlight>
</lang>
 
=={{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|Janet}}==
<langsyntaxhighlight lang="janet">
(defn bubble-sort!
[arr]
Line 3,326:
 
)
</syntaxhighlight>
</lang>
 
=={{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 3,343:
}
} 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 3,363:
}
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 3,381:
}
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}}
Line 3,394:
 
webpage version (for the rest of us):
<langsyntaxhighlight lang="javascript"><script>
Array.prototype.bubblesort = function() {
var done = false;
Line 3,416:
}
document.write(output);
</script></langsyntaxhighlight>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def bubble_sort:
def swap(i;j): .[i] as $x | .[i]=.[j] | .[j]=$x;
 
Line 3,432:
else .
end )
end ) | .[1] ;</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">(
[3,2,1],
[1,2,3],
["G", "F", "C", "A", "B", "E", "D"]
) | bubble_sort</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="sh">$ jq -c -n -f Bubble_sort.jq
[1,2,3]
[1,2,3]
["A","B","C","D","E","F","G"]</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function bubblesort!(arr::AbstractVector)
for _ in 2:length(arr), j in 1:length(arr)-1
if arr[j] > arr[j+1]
Line 3,458:
 
v = rand(-10:10, 10)
println("# unordered: $v\n -> ordered: ", bubblesort!(v))</langsyntaxhighlight>
 
{{out}}
Line 3,467:
{{trans|Java}}
 
<langsyntaxhighlight lang="scala">import java.util.Comparator
 
fun <T> bubbleSort(a: Array<T>, c: Comparator<T>) {
Line 3,482:
}
} while (changed)
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def bubblesort
{def bubblesort.swap!
Line 3,510:
{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>
</lang>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
itemCount = 20
dim item(itemCount)
Line 3,542:
next i
end
</syntaxhighlight>
</lang>
 
=={{header|Lisaac}}==
<langsyntaxhighlight Lisaaclang="lisaac">Section Header
 
+ name := BUBBLE_SORT;
Line 3,587:
};
}.do_while {!sorted};
);</langsyntaxhighlight>
 
=={{header|Lua}}==
 
<syntaxhighlight lang="lua">
<lang Lua>
function bubbleSort(A)
local itemCount=#A
Line 3,606:
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 3,615:
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 3,634:
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}}==
Line 3,642:
 
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Bubble {
function bubblesort {
Line 3,687:
}
Bubble
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">divert(-1)
 
define(`randSeed',141592653)
Line 3,734:
show(`a')
bubblesort(`a')
show(`a')</langsyntaxhighlight>
 
{{out}}
Line 3,742:
 
=={{header|Maple}}==
<syntaxhighlight lang="text">arr := Array([17,3,72,0,36,2,3,8,40,0]):
len := numelems(arr):
while(true) do
Line 3,757:
if (not change) then break end if:
end do:
arr;</langsyntaxhighlight>
{{Out|Output}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
Line 3,763:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===Using pattern matching===
<langsyntaxhighlight Mathematicalang="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}]</langsyntaxhighlight>
{{out}}
<pre>{1, 3, 3, 4, 7, 8, 9, 10, 13}</pre>
===Classic version===
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[BubbleSort]
BubbleSort[in_List] := Module[{x = in, l = Length[in], swapped},
swapped = True;
Line 3,785:
x
]
BubbleSort[{1, 12, 3, 7, 2, 8, 4, 7, 6}]</langsyntaxhighlight>
{{out}}
<pre>{1, 2, 3, 4, 6, 7, 7, 8, 12}</pre>
Line 3,791:
=={{header|MATLAB}}==
 
<langsyntaxhighlight MATLABlang="matlab">function list = bubbleSort(list)
 
hasChanged = true;
Line 3,810:
end %for
end %while
end %bubbleSort</langsyntaxhighlight>
 
{{out}}
Line 3,820:
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">fn bubbleSort arr =
(
while true do
Line 3,836:
)
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 3,921:
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 3,942:
END
UNTIL NOT changed
END BubbleSort;</langsyntaxhighlight>
 
=={{header|Modula-3}}==
 
<langsyntaxhighlight lang="modula3">MODULE Bubble;
 
PROCEDURE Sort(VAR a: ARRAY OF INTEGER) =
Line 3,965:
END;
END Sort;
END Bubble.</langsyntaxhighlight>
 
=={{header|N/t/roff}}==
Line 3,977:
{{works with|GROFF (GNU Troff)|1.22.2}}
 
<langsyntaxhighlight Nlang="n/t/roff">
.ig
Bubble sort algorithm in Troff
Line 4,048:
.nr Ai 0 1
.while \n(Ai<\n(Ac \n[A\n+[Ai]]
</syntaxhighlight>
</lang>
 
===Output===
Line 4,057:
=={{header|Nanoquery}}==
{{trans|Python}}
<langsyntaxhighlight Nanoquerylang="nanoquery">def bubble_sort(seq)
changed = true
 
Line 4,084:
testset = bubble_sort(testset)
println testset
end</langsyntaxhighlight>
 
{{out}}
Line 4,093:
=={{header|Nemerle}}==
===Functional===
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
 
Line 4,135:
WriteLine(Bubblesort(several));
}
}</langsyntaxhighlight>
===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.
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
 
Line 4,171:
Write($"$i ");
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 4,210:
 
return list
</syntaxhighlight>
</lang>
{{out}}
<pre style="height: 20ex; overflow: scroll;">
Line 4,235:
===Translation of Pseudocode===
This version is a direct implementation of this task's pseudocode.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 4,281:
method isFalse public constant binary returns boolean
return \isTrue
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc bubbleSort[T](a: var openarray[T]) =
var t = true
for n in countdown(a.len-2, 0):
Line 4,296:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
bubbleSort a
echo a</langsyntaxhighlight>
{{out}}
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
Line 4,302:
=={{header|Objeck}}==
{{trans|C}}
<langsyntaxhighlight lang="objeck">
function : Swap(p : Int[]) ~ Nil {
t := p[0];
Line 4,322:
while (sorted = false);
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">- (NSArray *) bubbleSort:(NSMutableArray *)unsorted {
BOOL done = false;
Line 4,340:
return unsorted;
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
Line 4,346:
 
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 4,356:
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 4,375:
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 4,391:
until(hasChanged == false)
s = v;
endfunction</langsyntaxhighlight>
 
<langsyntaxhighlight lang="octave">v = [9,8,7,3,1,100];
disp(bubblesort(v));</langsyntaxhighlight>
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(define (bubble-sort x ??)
(define (sort-step l)
Line 4,416:
(print
(bubble-sort (iota 100) <))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 4,429:
{{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.
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
Do
placesList = sampleData()
Line 4,491:
Exit
 
</syntaxhighlight>
</lang>
{{out}}
<pre style="height: 20ex; overflow: scroll;">
Line 4,515:
===Translation of Pseudocode===
This version is a direct implementation of this task's pseudocode.
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
Do
placesList = sampleData()
Line 4,587:
isFalse: procedure
return \isTrue()
</syntaxhighlight>
</lang>
 
===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.
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
Do
placesList. = ''
Line 4,652:
End
Exit
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
In-place sorting of mutable arrays:
<langsyntaxhighlight lang="oz">declare
proc {BubbleSort Arr}
proc {Swap I J}
Line 4,678:
in
{BubbleSort Arr}
{Inspect Arr}</langsyntaxhighlight>
 
Purely-functional sorting of immutable lists:
<langsyntaxhighlight lang="oz">declare
local
fun {Loop Xs Changed ?IsSorted}
Line 4,705:
end
in
{Show {BubbleSort [3 1 4 1 5 9 2 6 5]}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">bubbleSort(v)={
for(i=1,#v-1,
for(j=i+1,#v,
Line 4,719:
);
v
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">procedure bubble_sort(var list: array of real);
var
i, j, n: integer;
Line 4,736:
list[j + 1] := t;
end;
end;</langsyntaxhighlight>
 
Usage:<langsyntaxhighlight lang="pascal">var
list: array[0 .. 9] of real;
// ...
bubble_sort(list);</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl"># Sorts an array in place
sub bubble_sort {
for my $i (0 .. $#_){
Line 4,751:
}
}
}</langsyntaxhighlight>
 
Usage:
 
<langsyntaxhighlight lang="perl">my @a = (39, 25, 30, 28, 36, 72, 98, 25, 43, 38);
bubble_sort(@a);</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 4,786:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,795:
=={{header|PHP}}==
 
<langsyntaxhighlight lang="php">function bubbleSort(array $array){
foreach($array as $i => &$val){
foreach($array as $k => &$val2){
Line 4,807:
}
return $array;
}</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de bubbleSort (Lst)
(use Chg
(loop
Line 4,818:
(xchg L (cdr L))
(on Chg) ) )
(NIL Chg Lst) ) ) )</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">/* A primitive bubble sort */
bubble_sort: procedure (A);
declare A(*) fixed binary;
Line 4,836:
end;
end;
end bubble_sort;</langsyntaxhighlight>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">define bubble_sort(v);
lvars n=length(v), done=false, i;
while not(done) do
Line 4,857:
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 4,882:
x pstack
}def
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">function bubblesort ($a) {
$l = $a.Length
$hasChanged = $true
Line 4,898:
}
}
}</langsyntaxhighlight>
 
=={{header|Prolog}}==
Line 4,904:
to the bubble sort algorithm. Some of these are easier and shorter to code and work as well if not better. Having said that,
it's difficult to think of a reason to code the bubble sort these days except as an example of inefficiency.
<langsyntaxhighlight lang="prolog">%___________________________________________________________________________
% Bubble sort
 
Line 4,923:
writef(' input=%w\n', [In]),
bubblesort(In, R),
writef('-> %w\n', [R]).</langsyntaxhighlight>
for example:
<pre>?- test.
Line 4,936:
Should be ISO (but tested only with GNU Prolog).
Note: doesn't constuct list for each swap, only for each pass.
<langsyntaxhighlight lang="prolog">:- initialization(main).
 
 
Line 4,957:
test([8,9,1,3,4,2,6,5,4]).
 
main :- test(T), bubble_sort(T,_), halt.</langsyntaxhighlight>
{{Out}}
<pre>[8,9,1,3,4,2,6,5,4]
Line 4,966:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure bubbleSort(Array a(1))
Protected i, itemCount, hasChanged
Line 4,980:
Next
Until hasChanged = #False
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def bubble_sort(seq):
"""Inefficiently sort the mutable sequence (list) in place.
seq MUST BE A MUTABLE SEQUENCE.
Line 5,008:
assert testcase != testset # we've shuffled it
bubble_sort(testcase)
assert testcase == testset # we've unshuffled it back into a copy</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ stack ] is sorted ( --> s )
[ rot tuck over peek
Line 5,034:
[ 10 random join ]
say "Before: " dup echo cr
say "After: " bubble echo</langsyntaxhighlight>
 
{{out}}
Line 5,043:
 
=={{header|Qi}}==
<langsyntaxhighlight Qilang="qi">(define bubble-shot
[A] -> [A]
[A B|R] -> [B|(bubble-shot [A|R])] where (> A B)
Line 5,052:
 
(bubble-sort [6 8 5 9 3 2 2 1 4 7])
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight lang="rsplus">bubbleSort <- function(items)
{
repeat
Line 5,078:
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")</langsyntaxhighlight>
 
{{out}}
Line 5,090:
 
=={{header|Ra}}==
<syntaxhighlight lang="ra">
<lang Ra>
class BubbleSort
**Sort a list with the Bubble Sort algorithm**
Line 5,122:
list[i + 1] := temp
changed := true
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 5,128:
This bubble sort sorts the elelement in the vector v with regard to <?.
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 5,145:
(when again? (loop (- max 1) #f)))
v)
</syntaxhighlight>
</lang>
 
Example: Sorting a vector of length 10 with random entries.
<langsyntaxhighlight lang="racket">
(bubble-sort < (for/vector ([_ 10]) (random 20)))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 5,156:
{{works with|Rakudo|#24 "Seoul"}}
 
<syntaxhighlight lang="raku" perl6line>sub bubble_sort (@a) {
for ^@a -> $i {
for $i ^..^ @a -> $j {
Line 5,162:
}
}
}</langsyntaxhighlight>
 
=={{header|REALbasic}}==
Line 5,168:
Sorts an array of Integers
 
<syntaxhighlight lang="vb">
<lang vb>
Dim sortable() As Integer = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sortable.Shuffle() ' sortable is now randomized
Line 5,188:
Loop Until Not swapped
'sortable is now sorted
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
===version 0, alpha-numeric vertical list===
This REXX version sorts (using a bubble sort) and displays an array &nbsp; (of alpha-numeric items) &nbsp; in a vertical list.
<langsyntaxhighlight 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. */
Line 5,223:
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)":" @.j; end; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal array list:}}
<br>(Shown at &nbsp; '''<sup>5</sup>/<sub>6</sub>''' &nbsp; size.)
Line 5,282:
 
Programming note: &nbsp; a check was made to not exceed REXX's upper range limit of the &nbsp; '''random''' &nbsp; BIF.
<langsyntaxhighlight lang="rexx">/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
parse arg N .; if N=='' | N=="," then N=30 /*obtain optional size of array from CL*/
call gen N /*generate the array elements (items). */
Line 5,297:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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</langsyntaxhighlight>
{{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>
Line 5,306:
===version 2, random integers, horizontal list===
{{trans|PL/I}}
<langsyntaxhighlight lang="rexx">
/*REXX*/
Call random ,,1000
Line 5,331:
show:
l=''; Do i=1 To a.0; l=l a.i; End; Say arg(1)':'l
Return</langsyntaxhighlight>
{{out}}
<pre>vorher : 9 17 16 19 5 7 3 20 16 0
Line 5,347:
Also note that only four snapshots of the sort-in-progress is shown here, &nbsp; the REXX program will show a snapshot of ''every''
<br>sorting pass; &nbsp; the &nbsp; &nbsp; &nbsp; ''at &nbsp; (about) &nbsp; nnn% sorted'' &nbsp; &nbsp; &nbsp; was added after-the-fact.
<langsyntaxhighlight lang="rexx">/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
parse arg N seed . /*obtain optional size of array from CL*/
if N=='' | N=="," then N=30 /*Not specified? Then use the default.*/
Line 5,378:
do s=# for # by -1; say $.s; end /*s*/
say "└"copies('─', #) /*display the horizontal axis at bottom*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 5,512:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">bubbleList = [4,2,1,3]
flag = 0
bubbleSort(bubbleList)
Line 5,530:
next
end
</syntaxhighlight>
</lang>
 
=={{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 5,558:
ary.bubblesort1!
p ary
# => [3, 4, 6, 6, 8, 23, 78]</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">siz = 100
dim data$(siz)
unSorted = 1
Line 5,575:
END IF
NEXT
WEND</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn bubble_sort<T: Ord>(values: &mut[T]) {
let mut n = values.len();
let mut swapped = true;
Line 5,610:
bubble_sort(&mut strings);
println!("After: {:?}", strings);
}</langsyntaxhighlight>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class SORT{T < $IS_LT{T}} is
private swap(inout a, inout b:T) is
temp ::= a;
Line 5,633:
end;
end;
end;</langsyntaxhighlight>
 
<langsyntaxhighlight lang="sather">class MAIN is
main is
a:ARRAY{INT} := |10, 9, 8, 7, 6, -10, 5, 4|;
Line 5,641:
#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.
Line 5,649:
This slightly more complex version of Bubble Sort avoids errors 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 5,664:
}
} while(hasChanged)
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="scala">import scala.annotation.tailrec
 
def bubbleSort(xt: List[Int]) = {
Line 5,679:
}
bubble(xt, Nil, Nil)
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (bubble-sort x gt?)
(letrec
((fix (lambda (f i)
Line 5,696:
(cons (car l) (sort-step (cdr l))))))))
 
(fix sort-step x)))</langsyntaxhighlight>
 
This solution recursively 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 the same function, using a different syntax:
 
<langsyntaxhighlight lang="scheme">(define (bsort l gt?)
(define (dosort l)
(cond ((null? (cdr l))
Line 5,716:
l
(bsort try gt?))))
</syntaxhighlight>
</lang>
For example, you could do
<langsyntaxhighlight lang="scheme">(bsort > '(2 4 6 2))
(1 2 3)</langsyntaxhighlight>
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">function b=BubbleSort(a)
n=length(a)
swapped=%T
Line 5,737:
end
b=a
endfunction BubbleSort</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">-->y=[5 4 3 2 1]
Line 5,750:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">const proc: bubbleSort (inout array elemType: arr) is func
local
var boolean: swapped is FALSE;
Line 5,767:
end for;
until not swapped;
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#bubbleSort]
Line 5,773:
=={{header|Shen}}==
Bubble sort a vector in-place, using the < operator for comparison.
<langsyntaxhighlight lang="shen">(tc +)
 
(define swap
Line 5,799:
{ (vector number) --> (vector number) }
A -> (let N (limit A)
(bubble-h (one-pass A N false 2) A N)))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="shen">(datatype some-globals
 
__________
Line 5,812:
(vector-> (value *arr*) 4 2)
(vector-> (value *arr*) 5 8)
(bubble-sort (value *arr*))</langsyntaxhighlight>
 
Here is a more idiomatic implementation:
{{trans|Qi}}
 
<langsyntaxhighlight lang="shen">(tc +)
 
(define bubble-shot
Line 5,827:
(define bubble-sort
{ (vector number) --> (vector number) }
X -> (fix (function bubble-shot) X))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="shen">(bubble-sort (@v 5 1 4 2 3 <>))</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func bubble_sort(arr) {
loop {
var swapped = false
Line 5,844:
}
return arr
}</langsyntaxhighlight>
 
=={{header|Simula}}==
<langsyntaxhighlight lang="simula">BEGIN
 
PROCEDURE BUBBLESORT(A); NAME A; INTEGER ARRAY A;
Line 5,891:
OUTIMAGE;
 
END;</langsyntaxhighlight>
{{out}}
<pre>
Line 5,900:
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 5,917:
[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 5,944:
sloop j = j + 1; str = str arr<j> ' ' :s(sloop)
output = trim(str)
end</langsyntaxhighlight>
 
{{out}}
Line 5,956:
 
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 5,992:
 
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 6,043:
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 6,058:
 
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 6,121:
 
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 6,195:
 
File '''bubble.ads''':
<langsyntaxhighlight lang="ada">package Bubble with SPARK_Mode is
 
type Arr is array (Integer range <>) of Integer;
Line 6,217:
end Bubble;
</syntaxhighlight>
</lang>
 
File '''bubble.adb''':
<langsyntaxhighlight lang="ada">package body Bubble with SPARK_Mode is
procedure Sort (A : in out Arr)
Line 6,254:
end Bubble;
</syntaxhighlight>
</lang>
 
File '''main.adb''':
<langsyntaxhighlight lang="ada">with Ada.Integer_Text_IO;
with Bubble;
 
Line 6,268:
end loop;
end Main;
</syntaxhighlight>
</lang>
 
File '''bubble.gpr''':
<langsyntaxhighlight lang="ada">project Bubble is
 
for Main use ("main.adb");
 
end Bubble;
</syntaxhighlight>
</lang>
 
To verify the program, execute the command: '''gnatprove -P bubble.gpr -j0 --level=2'''
Line 6,322:
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">mata
function bubble_sort(a) {
n = length(a)
Line 6,338:
}
}
end</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">func bubbleSort<T:Comparable>(list:inout[T]) {
var done = false
while !done {
Line 6,357:
print(list1)
bubbleSort(list: &list1)
print(list1)</langsyntaxhighlight>
 
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
<lang Symsyn>
 
x : 23 : 15 : 99 : 146 : 3 : 66 : 71 : 5 : 23 : 73 : 19
Line 6,405:
" $r $s " []
return
</syntaxhighlight>
</lang>
 
=={{header|Tailspin}}==
<langsyntaxhighlight lang="tailspin">
templates bubblesort
templates bubble
Line 6,430:
[4,5,3,8,1,2,6,7,9,8,5] -> bubblesort -> !OUT::write
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
{{tcllib|struct::list}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require struct::list
 
Line 6,454:
}
 
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 6,558:
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 6,595:
foo 10 .array
foo 10 bsort
foo 10 .array</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
 
<langsyntaxhighlight TorqueScriptlang="torquescript">//Note that we're assuming that the list of numbers is separated by tabs.
function bubbleSort(%list)
{
Line 6,616:
}
return %list;
}</langsyntaxhighlight>
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">PRINT "Bubble sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
Line 6,666:
PRINT
RETURN</langsyntaxhighlight>
 
=={{header|Unicon}}==
Line 6,672:
 
=={{header|UnixPipes}}==
<langsyntaxhighlight lang="bash">rm -f _sortpass
 
reset() {
Line 6,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 6,704:
#cast %nL
 
example = bubblesort(nleq) <362,212,449,270,677,247,567,532,140,315></langsyntaxhighlight>
{{out}}
<pre><140,212,247,270,315,362,449,532,567,677></pre>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">void swap(int[] array, int i1, int i2) {
if (array[i1] == array[i2])
return;
Line 6,737:
foreach (int i in array)
print("%d ", i);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 6,744:
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Private Function bubble_sort(s As Variant) As Variant
Dim tmp As Variant
Dim changed As Boolean
Line 6,770:
Debug.Print "After: "
Debug.Print Join(bubble_sort(s), ", ")
End Sub</langsyntaxhighlight>{{out}}
<pre>Before:
4, 15, delta, 2, -31, 0, alfa, 19, gamma, 2, 13, beta, 782, 1
Line 6,782:
 
=====Implementation=====
<syntaxhighlight lang="vb">
<lang vb>
sub decr( byref n )
n = n - 1
Line 6,814:
bubbleSort = a
end function
</syntaxhighlight>
</lang>
 
=====Invocation=====
<syntaxhighlight lang="vb">
<lang vb>
dim a
a = array( "great eastern", "roe", "stirling", "albany", "leach")
Line 6,823:
bubbleSort a
wscript.echo join(a,", ")
</syntaxhighlight>
</lang>
 
{{out}}
Line 6,835:
 
{{works with|Visual Basic .NET|9.0+}}
<langsyntaxhighlight lang="vbnet">Do Until NoMoreSwaps = True
NoMoreSwaps = True
For Counter = 1 To (NumberOfItems - 1)
Line 6,846:
Next
NumberOfItems = NumberOfItems - 1
Loop</langsyntaxhighlight>
 
=={{header|Vlang}}==
<langsyntaxhighlight lang="vlang">fn bubble(mut arr []int) {
println('Input: ${arr.str()}')
mut count := arr.len
Line 6,876:
mut arr := [3, 5, 2, 1, 4]
bubble(mut arr)
}</langsyntaxhighlight>
{{out}}
<pre>Input: [3, 5, 2, 1, 4]
Line 6,883:
=={{header|Wren}}==
Based on the pseudo-code in the Wikipedia article.
<langsyntaxhighlight lang="ecmascript">var bubbleSort = Fn.new { |a|
var n = a.count
if (n < 2) return
Line 6,906:
System.print("After : %(a)")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 6,919:
=={{header|X86 Assembly}}==
Translation of XPL0. Assemble with tasm, tlink /t
<langsyntaxhighlight lang="asm"> .model tiny
.code
.486
Line 6,948:
popa
ret
end start</langsyntaxhighlight>
 
{{out}}
Line 6,956:
 
=={{header|Xojo}}==
<langsyntaxhighlight lang="xojo">Dim temp, count As Integer
Dim isDirty As Boolean
count = Ubound(list) // count the array size
Line 6,971:
End
Next
Loop Until isDirty = False // if we made it without touching the data then we are done</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
string 0; \use zero-terminated strings
 
Line 6,997:
BSort(Str, StrLen(Str));
Text(0, Str); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 7,005:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">// Animated sort.
// Original idea by William Tang, obtained from MicroHobby 25 Years (https://microhobby.speccy.cz/zxsf/MH-25Years.pdf)
 
Line 7,045:
 
for x=m TO 18 : print at(18,x-1) " " : print at(18,x) t$ : pause .02 : next x
</syntaxhighlight>
</lang>
 
=={{header|Yorick}}==
<langsyntaxhighlight lang="yorick">func bubblesort(&items) {
itemCount = numberof(items);
do {
Line 7,060:
}
} while(hasChanged);
}</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn bubbleSort(list){
itemCount := list.len();
do{
Line 7,075:
}while(hasChanged);
list
}</langsyntaxhighlight>
Or, punting early termination:
<langsyntaxhighlight 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
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">bubbleSort("This is a test".split("")).println();</langsyntaxhighlight>
{{out}}
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre>
 
=={{header|ZX Spectrum Basic}}==
<langsyntaxhighlight 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"
Line 7,108:
5064 IF d AND i<la THEN GO TO 5020
5072 PRINT AT 12,0;a$
9000 STOP </langsyntaxhighlight>
 
The traditional solution:
 
<langsyntaxhighlight lang="zxbasic"> 10 LET siz=32
20 DIM d$(siz)
30 REM Populate d$
Line 7,122:
90 NEXT i
100 IF unSorted THEN LET siz=siz-1: GO TO 60
110 PRINT d$</langsyntaxhighlight>
 
{{omit from|GUISS}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.