Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
Deadmarshal (talk | contribs) |
(Dialects of BASIC moved to the BASIC section.) |
||
Line 5: | Line 5: | ||
A '''bubble''' sort is also known as a '''sinking''' sort. |
A '''bubble''' sort is also known as a '''sinking''' sort. |
||
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses. |
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses. |
||
Line 1,171: | Line 1,170: | ||
9 FOR I = 1 TO I%(0) : PRINT I%(I)" "; : NEXT I : PRINT : RETURN</syntaxhighlight> |
9 FOR I = 1 TO I%(0) : PRINT I%(I)" "; : NEXT I : PRINT : RETURN</syntaxhighlight> |
||
==={{header| |
==={{header|BaCon}}=== |
||
Numeric example: |
|||
Works with the 1k RAM model. For simplicity, and to make it easy to animate the sort as it is going on, this implementation sorts a string of eight-bit unsigned integers which can be treated as character codes; it could easily be amended to sort an array of numbers or an array of strings, but the array would need to be dimensioned at the start. |
|||
<syntaxhighlight lang=" |
<syntaxhighlight lang="bacon">LOCAL t[] = { 5, 7, 1, 3, 10, 2, 9, 4, 8, 6 } |
||
total = 10 |
|||
20 PRINT S$ |
|||
WHILE total > 1 |
|||
30 LET L=LEN S$-1 |
|||
FOR x = 0 TO total-1 |
|||
40 LET C=0 |
|||
IF t[x] > t[x+1] THEN SWAP t[x], t[x+1] |
|||
50 FOR I=1 TO L |
|||
NEXT |
|||
60 IF S$(I)<=S$(I+1) THEN GOTO 120 |
|||
DECR total |
|||
70 LET T$=S$(I) |
|||
WEND |
|||
80 LET S$(I)=S$(I+1) |
|||
PRINT COIL$(10, STR$(t[_-1]))</syntaxhighlight> |
|||
90 LET S$(I+1)=T$ |
|||
100 PRINT AT 0,I-1;S$(I TO I+1) |
|||
110 LET C=1 |
|||
120 NEXT I |
|||
130 LET L=L-1 |
|||
140 IF C THEN GOTO 40</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<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}}=== |
==={{header|BASIC256}}=== |
||
Line 1,254: | Line 1,260: | ||
-31 0 1 2 2 4 65 83 99 782 |
-31 0 1 2 2 4 65 83 99 782 |
||
</pre> |
</pre> |
||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
Line 1,306: | Line 1,311: | ||
920 DATA 64,34,25,12,22,11,90,13,59,47,19,89,10,17,31 |
920 DATA 64,34,25,12,22,11,90,13,59,47,19,89,10,17,31 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
==={{header|FreeBASIC}}=== |
|||
Per task pseudo code: |
|||
<syntaxhighlight lang="freebasic">' version 21-10-2016 |
|||
' compile with: fbc -s console |
|||
' for boundry checks on array's compile with: fbc -s console -exx |
|||
Sub bubblesort(bs() As Long) |
|||
' sort from lower bound to the highter bound |
|||
' array's can have subscript range from -2147483648 to +2147483647 |
|||
Dim As Long lb = LBound(bs) |
|||
Dim As Long ub = UBound(bs) |
|||
Dim As Long done, i |
|||
Do |
|||
done = 0 |
|||
For i = lb To ub -1 |
|||
' replace "<" with ">" for downwards sort |
|||
If bs(i) > bs(i +1) Then |
|||
Swap bs(i), bs(i +1) |
|||
done = 1 |
|||
End If |
|||
Next |
|||
Loop Until done = 0 |
|||
End Sub |
|||
' ------=< MAIN >=------ |
|||
Dim As Long i, array(-7 To 7) |
|||
Dim As Long a = LBound(array), b = UBound(array) |
|||
Randomize Timer |
|||
For i = a To b : array(i) = i : Next |
|||
For i = a To b ' little shuffle |
|||
Swap array(i), array(Int(Rnd * (b - a +1)) + a) |
|||
Next |
|||
Print "unsort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
bubblesort(array()) ' sort the array |
|||
Print " sort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
' empty keyboard buffer |
|||
While InKey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre>unsort -7 3 -4 -6 4 -1 -2 2 7 0 5 1 -3 -5 6 |
|||
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre> |
|||
==={{header|FutureBasic}}=== |
|||
Bubble sorting is purely an academic exercise since there are much more efficient native sorting functions in FB. |
|||
<syntaxhighlight lang="futurebasic"> |
|||
include "NSLog.incl" |
|||
local fn BubbleSort( array as CFMutableArrayRef ) as CFArrayRef |
|||
NSUInteger i, x, y, count = len(array) |
|||
BOOL swapped = YES |
|||
while (swapped) |
|||
swapped = NO |
|||
for i = 1 to count -1 |
|||
x = fn NumberIntegerValue( array[i-1] ) |
|||
y = fn NumberIntegerValue( array[i] ) |
|||
if ( x > y ) |
|||
MutableArrayExchangeObjects( array, (i-1), i ) |
|||
swapped = YES |
|||
end if |
|||
next |
|||
wend |
|||
end fn = array |
|||
CFMutableArrayRef array |
|||
CFArrayRef unsortedArray, sortedArray |
|||
NSUInteger i |
|||
array = fn MutableArrayWithCapacity(0) |
|||
for i = 0 to 20 |
|||
MutableArrayAddObject( array, fn NumberWithInteger( rnd(100) ) ) |
|||
next |
|||
unsortedArray = fn ArrayWithArray( array ) |
|||
sortedArray = fn BubbleSort( array ) |
|||
NSLog( @"\n-----------------\nUnsorted : Sorted\n-----------------" ) |
|||
for i = 0 to 20 |
|||
NSLog( @"%8ld : %-8ld", fn NumberIntegerValue( unsortedArray[i] ), fn NumberIntegerValue( sortedArray[i] ) ) |
|||
next |
|||
randomize |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
----------------- |
|||
Unsorted : Sorted |
|||
----------------- |
|||
97 : 7 |
|||
91 : 8 |
|||
13 : 13 |
|||
39 : 17 |
|||
50 : 20 |
|||
48 : 28 |
|||
7 : 28 |
|||
61 : 30 |
|||
30 : 30 |
|||
20 : 33 |
|||
69 : 39 |
|||
86 : 42 |
|||
33 : 48 |
|||
65 : 50 |
|||
28 : 50 |
|||
50 : 61 |
|||
28 : 65 |
|||
8 : 69 |
|||
17 : 86 |
|||
42 : 91 |
|||
30 : 97 |
|||
</pre> |
|||
==={{header|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}}=== |
==={{header|GW-BASIC}}=== |
||
Line 1,376: | Line 1,566: | ||
350 END DEF</syntaxhighlight> |
350 END DEF</syntaxhighlight> |
||
==={{header| |
==={{header|Liberty BASIC}}=== |
||
{{works with|Just BASIC}} |
|||
Numeric example: |
|||
<syntaxhighlight lang=" |
<syntaxhighlight lang="lb"> |
||
itemCount = 20 |
|||
dim item(itemCount) |
|||
WHILE total > 1 |
|||
for i = 1 to itemCount |
|||
item(i) = int(rnd(1) * 100) |
|||
IF t[x] > t[x+1] THEN SWAP t[x], t[x+1] |
|||
next i |
|||
NEXT |
|||
print "Before Sort" |
|||
DECR total |
|||
for i = 1 to itemCount |
|||
WEND |
|||
print item(i) |
|||
PRINT COIL$(10, STR$(t[_-1]))</syntaxhighlight> |
|||
next i |
|||
print: print |
|||
counter = itemCount |
|||
do |
|||
hasChanged = 0 |
|||
for i = 1 to counter - 1 |
|||
if item(i) > item(i + 1) then |
|||
temp = item(i) |
|||
item(i) = item(i + 1) |
|||
item(i + 1) = temp |
|||
hasChanged = 1 |
|||
end if |
|||
next i |
|||
counter =counter -1 |
|||
loop while hasChanged = 1 |
|||
print "After Sort" |
|||
for i = 1 to itemCount |
|||
print item(i) |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
|||
==={{header|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|REALbasic}}=== |
|||
Sorts an array of Integers. |
|||
<syntaxhighlight lang="vb"> |
|||
Dim sortable() As Integer = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
|||
sortable.Shuffle() ' sortable is now randomized |
|||
Dim swapped As Boolean |
|||
Do |
|||
Dim index, bound As Integer |
|||
bound = sortable.Ubound |
|||
While index < bound |
|||
If sortable(index) > sortable(index + 1) Then |
|||
Dim s As Integer = sortable(index) |
|||
sortable.Remove(index) |
|||
sortable.Insert(index + 1, s) |
|||
swapped = True |
|||
End If |
|||
index = index + 1 |
|||
Wend |
|||
Loop Until Not swapped |
|||
'sortable is now sorted |
|||
</syntaxhighlight> |
|||
==={{header|Run BASIC}}=== |
|||
<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}} |
{{out}} |
||
<pre> |
<pre> AABBBBCDDEEFILLNNNORRRUUU</pre> |
||
String example: |
|||
==={{header|TI-83 BASIC}}=== |
|||
<syntaxhighlight lang="bacon">t$ = "Kiev Amsterdam Lima Moscow Warschau Vienna Paris Madrid Bonn Bern Rome" |
|||
Input your data into L<sub>1</sub> and run this program to organize it. |
|||
total = AMOUNT(t$) |
|||
:L<sub>1</sub>→L<sub>2</sub> |
|||
WHILE total > 1 |
|||
:1+dim(L<sub>2</sub>)→N |
|||
FOR x = 1 TO total-1 |
|||
:For(D,1,dim(L<sub>2</sub>)) |
|||
IF TOKEN$(t$, x) > TOKEN$(t$, x+1) THEN t$ = EXCHANGE$(t$, x, x+1) |
|||
: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)>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)>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|uBasic/4tH}}=== |
|||
<syntaxhighlight lang="text">PRINT "Bubble sort:" |
|||
n = FUNC (_InitArray) |
|||
PROC _ShowArray (n) |
|||
PROC _Bubblesort (n) |
|||
PROC _ShowArray (n) |
|||
PRINT |
|||
END |
|||
_Bubblesort PARAM(1) ' Bubble sort |
|||
LOCAL (2) |
|||
DO |
|||
b@ = 0 |
|||
FOR c@ = 1 TO a@-1 |
|||
IF @(c@-1) > @(c@) THEN PROC _Swap (c@, c@-1) : b@ = c@ |
|||
NEXT |
NEXT |
||
a@ = b@ |
|||
UNTIL b@ = 0 |
|||
WEND |
|||
LOOP |
|||
PRINT t$</syntaxhighlight> |
|||
RETURN |
|||
_Swap PARAM(2) ' Swap two array elements |
|||
PUSH @(a@) |
|||
@(a@) = @(b@) |
|||
@(b@) = POP() |
|||
RETURN |
|||
_InitArray ' Init example array |
|||
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
|||
FOR i = 0 TO 9 |
|||
@(i) = POP() |
|||
NEXT |
|||
RETURN (i) |
|||
_ShowArray PARAM (1) ' Show array subroutine |
|||
FOR i = 0 TO a@-1 |
|||
PRINT @(i), |
|||
NEXT |
|||
PRINT |
|||
RETURN</syntaxhighlight> |
|||
==={{header|VBA}}=== |
|||
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function bubble_sort(s As Variant) As Variant |
|||
Dim tmp As Variant |
|||
Dim changed As Boolean |
|||
For j = UBound(s) To 1 Step -1 |
|||
changed = False |
|||
For i = 1 To j - 1 |
|||
If s(i) > s(i + 1) Then |
|||
tmp = s(i) |
|||
s(i) = s(i + 1) |
|||
s(i + 1) = tmp |
|||
changed = True |
|||
End If |
|||
Next i |
|||
If Not changed Then |
|||
Exit For |
|||
End If |
|||
Next j |
|||
bubble_sort = s |
|||
End Function |
|||
Public Sub main() |
|||
s = [{4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}] |
|||
Debug.Print "Before: " |
|||
Debug.Print Join(s, ", ") |
|||
Debug.Print "After: " |
|||
Debug.Print Join(bubble_sort(s), ", ") |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>Before: |
|||
4, 15, delta, 2, -31, 0, alfa, 19, gamma, 2, 13, beta, 782, 1 |
|||
After: |
|||
-31, 0, 1, 2, 2, 4, 13, 15, 19, 782, alfa, beta, delta, gamma</pre> |
|||
==={{header|VBScript}}=== |
|||
Doing the decr and incr thing is superfluous, really. I just had stumbled over the byref thing for <code>swap</code> and wanted to see where else it would work. |
|||
For those unfamiliar with Perth, WA Australia, the five strings being sorted are names of highways. |
|||
=====Implementation===== |
|||
<syntaxhighlight lang="vb"> |
|||
sub decr( byref n ) |
|||
n = n - 1 |
|||
end sub |
|||
sub incr( byref n ) |
|||
n = n + 1 |
|||
end sub |
|||
sub swap( byref a, byref b) |
|||
dim tmp |
|||
tmp = a |
|||
a = b |
|||
b = tmp |
|||
end sub |
|||
function bubbleSort( a ) |
|||
dim changed |
|||
dim itemCount |
|||
itemCount = ubound(a) |
|||
do |
|||
changed = false |
|||
decr itemCount |
|||
for i = 0 to itemCount |
|||
if a(i) > a(i+1) then |
|||
swap a(i), a(i+1) |
|||
changed = true |
|||
end if |
|||
next |
|||
loop until not changed |
|||
bubbleSort = a |
|||
end function |
|||
</syntaxhighlight> |
|||
=====Invocation===== |
|||
<syntaxhighlight lang="vb"> |
|||
dim a |
|||
a = array( "great eastern", "roe", "stirling", "albany", "leach") |
|||
wscript.echo join(a,", ") |
|||
bubbleSort a |
|||
wscript.echo join(a,", ") |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
|||
<pre>Amsterdam Bern Bonn Kiev Lima Madrid Moscow Paris Rome Vienna Warschau</pre> |
|||
great eastern, roe, stirling, albany, leach |
|||
albany, great eastern, leach, roe, stirling |
|||
</pre> |
|||
==={{header|Visual Basic .NET}}=== |
|||
'''Platform:''' [[.NET]] |
|||
{{works with|Visual Basic .NET|9.0+}} |
|||
<syntaxhighlight lang="vbnet">Do Until NoMoreSwaps = True |
|||
NoMoreSwaps = True |
|||
For Counter = 1 To (NumberOfItems - 1) |
|||
If List(Counter) > List(Counter + 1) Then |
|||
NoMoreSwaps = False |
|||
Temp = List(Counter) |
|||
List(Counter) = List(Counter + 1) |
|||
List(Counter + 1) = Temp |
|||
End If |
|||
Next |
|||
NumberOfItems = NumberOfItems - 1 |
|||
Loop</syntaxhighlight> |
|||
==={{header|Yabasic}}=== |
|||
<syntaxhighlight lang="yabasic">// Animated sort. |
|||
// Original idea by William Tang, obtained from MicroHobby 25 Years (https://microhobby.speccy.cz/zxsf/MH-25Years.pdf) |
|||
clear screen |
|||
n=15 : m=18 : y=9 : t$=chr$(17)+chr$(205)+chr$(205) |
|||
dim p(n), p$(n) |
|||
for x=1 TO n |
|||
p(x)=ran(15)+1 |
|||
p$(x)=str$(p(x),"##.######") |
|||
print at(0,x) p$(x) |
|||
next x |
|||
for j=1 to n-1 |
|||
for i=j+1 to n |
|||
l=n+j-i+1 |
|||
if p(j) > p(l) then |
|||
print color("yellow","red") at(0,j) p$(j) |
|||
if l<>m then |
|||
for x=m to l step sig(l-m): print at(18,x) t$ : print at (18,x+sig(m-l)) " " : pause .02 : next x |
|||
end if |
|||
for x=17 TO y step -1 : print at(x,l) t$+" " : pause .02 : next x |
|||
for x=0 TO 10 : print at(x,l) " "+p$(l)+t$ : pause .02 : next x |
|||
for x=l TO j STEP -1 : print at(11,x) p$(l)+t$ : print at(11,x+1) " " : pause .02 : next x |
|||
print at(0,j) " " |
|||
for x=j+1 TO l-1 : print color("yellow","red") at(0,x) p$(j) : pause .02 : print at(0,x) p$(x) : pause .02 : next x |
|||
print at(0,l) p$(j) |
|||
for x=10 TO 0 step -1 : print at(x,j) p$(l)+t$+" " : pause .02 : next x |
|||
for x=y TO 17 : print at(x,j) " "+t$ : pause .02 : next x |
|||
m=j |
|||
t=p(l) : tem$=p$(l) |
|||
p(l)=p(j) : p$(l)=p$(j) |
|||
p(j)=t : p$(j)=tem$ |
|||
end if |
|||
pause .02 |
|||
next i |
|||
next j |
|||
for x=m TO 18 : print at(18,x-1) " " : print at(18,x) t$ : pause .02 : next x |
|||
</syntaxhighlight> |
|||
==={{header|ZX Spectrum Basic}}=== |
|||
<syntaxhighlight lang="zxbasic">5000 CLS |
|||
5002 LET a$="": FOR f=1 TO 64: LET a$=a$+CHR$ (32+INT (RND*96)): NEXT f |
|||
5004 PRINT a$; AT 10,0;"ZigZag BubbleSORT" |
|||
5010 LET la=LEN a$ |
|||
5011 LET i=1: LET u=0 |
|||
5020 LET d=0: LET p=(u=0)-(u=1) |
|||
5021 LET l=(i AND u=0)+(la-i+u AND u=1) |
|||
5030 IF u=0 THEN IF a$(l+1)>=a$(l) THEN GO TO 5050 |
|||
5031 IF u=1 THEN IF a$(l-1)<=a$(l) THEN GO TO 5050 |
|||
5040 LET d=1 |
|||
5042 LET t$=a$(l+p) |
|||
5043 LET a$(l+p)=a$(l) |
|||
5044 LET a$(l)=t$ |
|||
5050 LET l=l+p |
|||
5051 PRINT AT 10,21;a$(l);AT 12,0;a$ |
|||
5055 IF l<=la-i AND l>=i THEN GO TO 5023 |
|||
5061 LET i=i+NOT u |
|||
5063 LET u=NOT u |
|||
5064 IF d AND i<la THEN GO TO 5020 |
|||
5072 PRINT AT 12,0;a$ |
|||
9000 STOP </syntaxhighlight> |
|||
The traditional solution: |
|||
<syntaxhighlight lang="zxbasic"> 10 LET siz=32 |
|||
20 DIM d$(siz) |
|||
30 REM Populate d$ |
|||
40 FOR n=1 TO siz: LET d$(n)=CHR$ (48+INT (RND*75)): NEXT n |
|||
50 PRINT d$ |
|||
60 LET unSorted=0 |
|||
70 FOR i=1 TO siz-1 |
|||
80 IF d$(i)>d$(i+1) THEN LET t$=d$(i): LET d$(i)=d$(i+1): LET d$(i+1)=t$: LET unSorted=1 |
|||
90 NEXT i |
|||
100 IF unSorted THEN LET siz=siz-1: GO TO 60 |
|||
110 PRINT d$</syntaxhighlight> |
|||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
Line 2,846: | Line 3,452: | ||
END DO |
END DO |
||
END SUBROUTINE Bubble_Sort</syntaxhighlight> |
END SUBROUTINE Bubble_Sort</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
Per task pseudo code: |
|||
<syntaxhighlight lang="freebasic">' version 21-10-2016 |
|||
' compile with: fbc -s console |
|||
' for boundry checks on array's compile with: fbc -s console -exx |
|||
Sub bubblesort(bs() As Long) |
|||
' sort from lower bound to the highter bound |
|||
' array's can have subscript range from -2147483648 to +2147483647 |
|||
Dim As Long lb = LBound(bs) |
|||
Dim As Long ub = UBound(bs) |
|||
Dim As Long done, i |
|||
Do |
|||
done = 0 |
|||
For i = lb To ub -1 |
|||
' replace "<" with ">" for downwards sort |
|||
If bs(i) > bs(i +1) Then |
|||
Swap bs(i), bs(i +1) |
|||
done = 1 |
|||
End If |
|||
Next |
|||
Loop Until done = 0 |
|||
End Sub |
|||
' ------=< MAIN >=------ |
|||
Dim As Long i, array(-7 To 7) |
|||
Dim As Long a = LBound(array), b = UBound(array) |
|||
Randomize Timer |
|||
For i = a To b : array(i) = i : Next |
|||
For i = a To b ' little shuffle |
|||
Swap array(i), array(Int(Rnd * (b - a +1)) + a) |
|||
Next |
|||
Print "unsort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
bubblesort(array()) ' sort the array |
|||
Print " sort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
' empty keyboard buffer |
|||
While InKey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre>unsort -7 3 -4 -6 4 -1 -2 2 7 0 5 1 -3 -5 6 |
|||
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre> |
|||
=={{header|FutureBasic}}== |
|||
Bubble sorting is purely an academic exercise since there are much more efficient native sorting functions in FB. |
|||
<syntaxhighlight lang="futurebasic"> |
|||
include "NSLog.incl" |
|||
local fn BubbleSort( array as CFMutableArrayRef ) as CFArrayRef |
|||
NSUInteger i, x, y, count = len(array) |
|||
BOOL swapped = YES |
|||
while (swapped) |
|||
swapped = NO |
|||
for i = 1 to count -1 |
|||
x = fn NumberIntegerValue( array[i-1] ) |
|||
y = fn NumberIntegerValue( array[i] ) |
|||
if ( x > y ) |
|||
MutableArrayExchangeObjects( array, (i-1), i ) |
|||
swapped = YES |
|||
end if |
|||
next |
|||
wend |
|||
end fn = array |
|||
CFMutableArrayRef array |
|||
CFArrayRef unsortedArray, sortedArray |
|||
NSUInteger i |
|||
array = fn MutableArrayWithCapacity(0) |
|||
for i = 0 to 20 |
|||
MutableArrayAddObject( array, fn NumberWithInteger( rnd(100) ) ) |
|||
next |
|||
unsortedArray = fn ArrayWithArray( array ) |
|||
sortedArray = fn BubbleSort( array ) |
|||
NSLog( @"\n-----------------\nUnsorted : Sorted\n-----------------" ) |
|||
for i = 0 to 20 |
|||
NSLog( @"%8ld : %-8ld", fn NumberIntegerValue( unsortedArray[i] ), fn NumberIntegerValue( sortedArray[i] ) ) |
|||
next |
|||
randomize |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
----------------- |
|||
Unsorted : Sorted |
|||
----------------- |
|||
97 : 7 |
|||
91 : 8 |
|||
13 : 13 |
|||
39 : 17 |
|||
50 : 20 |
|||
48 : 28 |
|||
7 : 28 |
|||
61 : 30 |
|||
30 : 30 |
|||
20 : 33 |
|||
69 : 39 |
|||
86 : 42 |
|||
33 : 48 |
|||
65 : 50 |
|||
28 : 50 |
|||
50 : 61 |
|||
28 : 65 |
|||
8 : 69 |
|||
17 : 86 |
|||
42 : 91 |
|||
30 : 97 |
|||
</pre> |
|||
=={{header|g-fu}}== |
=={{header|g-fu}}== |
||
Line 2,991: | Line 3,471: | ||
(1 2 3) |
(1 2 3) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{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|Go}}== |
=={{header|Go}}== |
||
Line 3,583: | Line 4,002: | ||
{bubblesort {A.new 0 3 86 20 27 67 31 16 37 42 8 47 7 84 5 29}} |
{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] |
-> [0,3,5,7,8,16,20,27,29,31,37,42,47,67,84,86] |
||
</syntaxhighlight> |
|||
=={{header|Liberty BASIC}}== |
|||
<syntaxhighlight lang="lb"> |
|||
itemCount = 20 |
|||
dim item(itemCount) |
|||
for i = 1 to itemCount |
|||
item(i) = int(rnd(1) * 100) |
|||
next i |
|||
print "Before Sort" |
|||
for i = 1 to itemCount |
|||
print item(i) |
|||
next i |
|||
print: print |
|||
counter = itemCount |
|||
do |
|||
hasChanged = 0 |
|||
for i = 1 to counter - 1 |
|||
if item(i) > item(i + 1) then |
|||
temp = item(i) |
|||
item(i) = item(i + 1) |
|||
item(i + 1) = temp |
|||
hasChanged = 1 |
|||
end if |
|||
next i |
|||
counter =counter -1 |
|||
loop while hasChanged = 1 |
|||
print "After Sort" |
|||
for i = 1 to itemCount |
|||
print item(i) |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 5,101: | Line 5,488: | ||
[1,3,2,4,5,4,6,8,9] |
[1,3,2,4,5,4,6,8,9] |
||
[1,2,3,4,4,5,6,8,9]</pre> |
[1,2,3,4,4,5,6,8,9]</pre> |
||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic">Procedure bubbleSort(Array a(1)) |
|||
Protected i, itemCount, hasChanged |
|||
itemCount = ArraySize(a()) |
|||
Repeat |
|||
hasChanged = #False |
|||
itemCount - 1 |
|||
For i = 0 To itemCount |
|||
If a(i) > a(i + 1) |
|||
Swap a(i), a(i + 1) |
|||
hasChanged = #True |
|||
EndIf |
|||
Next |
|||
Until hasChanged = #False |
|||
EndProcedure</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 5,300: | Line 5,670: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|REALbasic}}== |
|||
Sorts an array of Integers |
|||
<syntaxhighlight lang="vb"> |
|||
Dim sortable() As Integer = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
|||
sortable.Shuffle() ' sortable is now randomized |
|||
Dim swapped As Boolean |
|||
Do |
|||
Dim index, bound As Integer |
|||
bound = sortable.Ubound |
|||
While index < bound |
|||
If sortable(index) > sortable(index + 1) Then |
|||
Dim s As Integer = sortable(index) |
|||
sortable.Remove(index) |
|||
sortable.Insert(index + 1, s) |
|||
swapped = True |
|||
End If |
|||
index = index + 1 |
|||
Wend |
|||
Loop Until Not swapped |
|||
'sortable is now sorted |
|||
</syntaxhighlight> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 5,696: | Line 6,040: | ||
p ary |
p ary |
||
# => [3, 4, 6, 6, 8, 23, 78]</syntaxhighlight> |
# => [3, 4, 6, 6, 8, 23, 78]</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
|||
<syntaxhighlight lang="runbasic">siz = 100 |
|||
dim data$(siz) |
|||
unSorted = 1 |
|||
WHILE unSorted |
|||
unSorted = 0 |
|||
FOR i = 1 TO siz -1 |
|||
IF data$(i) > data$(i + 1) THEN |
|||
tmp = data$(i) |
|||
data$(i) = data$(i + 1) |
|||
data$(i + 1) = tmp |
|||
unSorted = 1 |
|||
END IF |
|||
NEXT |
|||
WEND</syntaxhighlight> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 6,594: | Line 6,921: | ||
Idiomatic code uses the builtin <code>lsort</code> instead, which is a stable O(''n'' log ''n'') sort. |
Idiomatic code uses the builtin <code>lsort</code> instead, which is a stable O(''n'' log ''n'') sort. |
||
=={{header|TI-83 BASIC}}== |
|||
Input your data into L<sub>1</sub> and run this program to organize it. |
|||
:L<sub>1</sub>→L<sub>2</sub> |
|||
:1+dim(L<sub>2</sub>)→N |
|||
:For(D,1,dim(L<sub>2</sub>)) |
|||
:N-1→N |
|||
:0→I |
|||
:For(C,1,dim(L<sub>2</sub>)-2) |
|||
:For(A,dim(L<sub>2</sub>)-N+1,dim(L<sub>2</sub>)-1) |
|||
:If L<sub>2</sub>(A)>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)>L<sub>2</sub>(2) |
|||
:Then |
|||
:L<sub>2</sub>(1)→B |
|||
:L<sub>2</sub>(2)→L<sub>2</sub>(1) |
|||
:B→L<sub>2</sub>(2) |
|||
:End |
|||
:DelVar A |
|||
:DelVar B |
|||
:DelVar C |
|||
:DelVar D |
|||
:DelVar N |
|||
:DelVar I |
|||
:Return |
|||
[[wp:Odd-even sort|Odd-Even Bubble Sort]] (same IO): |
|||
:"ODD-EVEN" |
|||
:L<sub>1</sub>→L<sub>2</sub>( |
|||
:1+dim(L<sub>2</sub>)→N |
|||
:For(D,1,dim(L<sub>2</sub>)) |
|||
:N-1→N |
|||
:0→O |
|||
:For(C,1,dim(L<sub>2</sub>)-2) |
|||
:For(A,dim(L<sub>2</sub>)-N+2,dim(L<sub>2</sub>)-1,2) |
|||
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1) |
|||
:Then |
|||
:1→O |
|||
:L<sub>2</sub>(A)→B |
|||
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A) |
|||
:B→L<sub>2</sub>(A+1) |
|||
:End |
|||
:End |
|||
:For(A,dim(L<sub>2</sub>)-N+1,dim(L<sub>2</sub>)-1,2) |
|||
:If L<sub>2</sub>(A)>L<sub>2</sub>(A+1) |
|||
:Then |
|||
:1→O |
|||
:L<sub>2</sub>(A)→B |
|||
:L<sub>2</sub>(A+1)→L<sub>2</sub>(A) |
|||
:B→L<sub>2</sub>(A+1) |
|||
:End |
|||
:End |
|||
:End |
|||
:If O=0 |
|||
:Goto C |
|||
:End |
|||
:Lbl C |
|||
:If L<sub>2</sub>(1)>L<sub>2</sub>(2) |
|||
:Then |
|||
:L<sub>2</sub>(1)→B |
|||
:L<sub>2</sub>(2)→L<sub>2</sub>(1) |
|||
:B→L<sub>2</sub>(2) |
|||
:End |
|||
:DelVar A |
|||
:DelVar B |
|||
:DelVar C |
|||
:DelVar D |
|||
:DelVar N |
|||
:DelVar O |
|||
:Return |
|||
Implementation of the pseudo code given at the top of the page. Place data to be sorted in L<sub>1</sub> |
|||
:dim(L<sub>1</sub>)→D |
|||
:Repeat C=0 |
|||
:0→C |
|||
:D–1→D |
|||
:For(I,1,D) |
|||
:If L<sub>1</sub>(I)>L<sub>1</sub>(I+1):Then |
|||
:L<sub>1</sub>(I)→C |
|||
:L<sub>1</sub>(I+1)→L<sub>1</sub>(I) |
|||
:C→L<sub>1</sub>(I+1) |
|||
:1→C |
|||
:End |
|||
:End |
|||
:End |
|||
:L<sub>1</sub> |
|||
=={{header|Toka}}== |
=={{header|Toka}}== |
||
Line 6,754: | Line 6,984: | ||
return %list; |
return %list; |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|uBasic/4tH}}== |
|||
<syntaxhighlight lang="text">PRINT "Bubble sort:" |
|||
n = FUNC (_InitArray) |
|||
PROC _ShowArray (n) |
|||
PROC _Bubblesort (n) |
|||
PROC _ShowArray (n) |
|||
PRINT |
|||
END |
|||
_Bubblesort PARAM(1) ' Bubble sort |
|||
LOCAL (2) |
|||
DO |
|||
b@ = 0 |
|||
FOR c@ = 1 TO a@-1 |
|||
IF @(c@-1) > @(c@) THEN PROC _Swap (c@, c@-1) : b@ = c@ |
|||
NEXT |
|||
a@ = b@ |
|||
UNTIL b@ = 0 |
|||
LOOP |
|||
RETURN |
|||
_Swap PARAM(2) ' Swap two array elements |
|||
PUSH @(a@) |
|||
@(a@) = @(b@) |
|||
@(b@) = POP() |
|||
RETURN |
|||
_InitArray ' Init example array |
|||
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
|||
FOR i = 0 TO 9 |
|||
@(i) = POP() |
|||
NEXT |
|||
RETURN (i) |
|||
_ShowArray PARAM (1) ' Show array subroutine |
|||
FOR i = 0 TO a@-1 |
|||
PRINT @(i), |
|||
NEXT |
|||
PRINT |
|||
RETURN</syntaxhighlight> |
|||
=={{header|Unicon}}== |
=={{header|Unicon}}== |
||
Line 6,879: | Line 7,059: | ||
-4 -1 0 1 2 3 5 6 8 101 |
-4 -1 0 1 2 3 5 6 8 101 |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
|||
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function bubble_sort(s As Variant) As Variant |
|||
Dim tmp As Variant |
|||
Dim changed As Boolean |
|||
For j = UBound(s) To 1 Step -1 |
|||
changed = False |
|||
For i = 1 To j - 1 |
|||
If s(i) > s(i + 1) Then |
|||
tmp = s(i) |
|||
s(i) = s(i + 1) |
|||
s(i + 1) = tmp |
|||
changed = True |
|||
End If |
|||
Next i |
|||
If Not changed Then |
|||
Exit For |
|||
End If |
|||
Next j |
|||
bubble_sort = s |
|||
End Function |
|||
Public Sub main() |
|||
s = [{4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}] |
|||
Debug.Print "Before: " |
|||
Debug.Print Join(s, ", ") |
|||
Debug.Print "After: " |
|||
Debug.Print Join(bubble_sort(s), ", ") |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>Before: |
|||
4, 15, delta, 2, -31, 0, alfa, 19, gamma, 2, 13, beta, 782, 1 |
|||
After: |
|||
-31, 0, 1, 2, 2, 4, 13, 15, 19, 782, alfa, beta, delta, gamma</pre> |
|||
=={{header|VBScript}}== |
|||
Doing the decr and incr thing is superfluous, really. I just had stumbled over the byref thing for <code>swap</code> and wanted to see where else it would work. |
|||
For those unfamiliar with Perth, WA Australia, the five strings being sorted are names of highways. |
|||
=====Implementation===== |
|||
<syntaxhighlight lang="vb"> |
|||
sub decr( byref n ) |
|||
n = n - 1 |
|||
end sub |
|||
sub incr( byref n ) |
|||
n = n + 1 |
|||
end sub |
|||
sub swap( byref a, byref b) |
|||
dim tmp |
|||
tmp = a |
|||
a = b |
|||
b = tmp |
|||
end sub |
|||
function bubbleSort( a ) |
|||
dim changed |
|||
dim itemCount |
|||
itemCount = ubound(a) |
|||
do |
|||
changed = false |
|||
decr itemCount |
|||
for i = 0 to itemCount |
|||
if a(i) > a(i+1) then |
|||
swap a(i), a(i+1) |
|||
changed = true |
|||
end if |
|||
next |
|||
loop until not changed |
|||
bubbleSort = a |
|||
end function |
|||
</syntaxhighlight> |
|||
=====Invocation===== |
|||
<syntaxhighlight lang="vb"> |
|||
dim a |
|||
a = array( "great eastern", "roe", "stirling", "albany", "leach") |
|||
wscript.echo join(a,", ") |
|||
bubbleSort a |
|||
wscript.echo join(a,", ") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
great eastern, roe, stirling, albany, leach |
|||
albany, great eastern, leach, roe, stirling |
|||
</pre> |
|||
=={{header|Visual Basic .NET}}== |
|||
'''Platform:''' [[.NET]] |
|||
{{works with|Visual Basic .NET|9.0+}} |
|||
<syntaxhighlight lang="vbnet">Do Until NoMoreSwaps = True |
|||
NoMoreSwaps = True |
|||
For Counter = 1 To (NumberOfItems - 1) |
|||
If List(Counter) > List(Counter + 1) Then |
|||
NoMoreSwaps = False |
|||
Temp = List(Counter) |
|||
List(Counter) = List(Counter + 1) |
|||
List(Counter + 1) = Temp |
|||
End If |
|||
Next |
|||
NumberOfItems = NumberOfItems - 1 |
|||
Loop</syntaxhighlight> |
|||
=={{header|V (Vlang)}}== |
=={{header|V (Vlang)}}== |
||
Line 7,140: | Line 7,215: | ||
" .Pabcdeefghiiijklmnoooqrstuuvwxyz" |
" .Pabcdeefghiiijklmnoooqrstuuvwxyz" |
||
</pre> |
</pre> |
||
=={{header|Yabasic}}== |
|||
<syntaxhighlight lang="yabasic">// Animated sort. |
|||
// Original idea by William Tang, obtained from MicroHobby 25 Years (https://microhobby.speccy.cz/zxsf/MH-25Years.pdf) |
|||
clear screen |
|||
n=15 : m=18 : y=9 : t$=chr$(17)+chr$(205)+chr$(205) |
|||
dim p(n), p$(n) |
|||
for x=1 TO n |
|||
p(x)=ran(15)+1 |
|||
p$(x)=str$(p(x),"##.######") |
|||
print at(0,x) p$(x) |
|||
next x |
|||
for j=1 to n-1 |
|||
for i=j+1 to n |
|||
l=n+j-i+1 |
|||
if p(j) > p(l) then |
|||
print color("yellow","red") at(0,j) p$(j) |
|||
if l<>m then |
|||
for x=m to l step sig(l-m): print at(18,x) t$ : print at (18,x+sig(m-l)) " " : pause .02 : next x |
|||
end if |
|||
for x=17 TO y step -1 : print at(x,l) t$+" " : pause .02 : next x |
|||
for x=0 TO 10 : print at(x,l) " "+p$(l)+t$ : pause .02 : next x |
|||
for x=l TO j STEP -1 : print at(11,x) p$(l)+t$ : print at(11,x+1) " " : pause .02 : next x |
|||
print at(0,j) " " |
|||
for x=j+1 TO l-1 : print color("yellow","red") at(0,x) p$(j) : pause .02 : print at(0,x) p$(x) : pause .02 : next x |
|||
print at(0,l) p$(j) |
|||
for x=10 TO 0 step -1 : print at(x,j) p$(l)+t$+" " : pause .02 : next x |
|||
for x=y TO 17 : print at(x,j) " "+t$ : pause .02 : next x |
|||
m=j |
|||
t=p(l) : tem$=p$(l) |
|||
p(l)=p(j) : p$(l)=p$(j) |
|||
p(j)=t : p$(j)=tem$ |
|||
end if |
|||
pause .02 |
|||
next i |
|||
next j |
|||
for x=m TO 18 : print at(18,x-1) " " : print at(18,x) t$ : pause .02 : next x |
|||
</syntaxhighlight> |
|||
=={{header|Yorick}}== |
=={{header|Yorick}}== |
||
Line 7,223: | Line 7,255: | ||
{{out}} |
{{out}} |
||
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre> |
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre> |
||
=={{header|ZX Spectrum Basic}}== |
|||
<syntaxhighlight lang="zxbasic">5000 CLS |
|||
5002 LET a$="": FOR f=1 TO 64: LET a$=a$+CHR$ (32+INT (RND*96)): NEXT f |
|||
5004 PRINT a$; AT 10,0;"ZigZag BubbleSORT" |
|||
5010 LET la=LEN a$ |
|||
5011 LET i=1: LET u=0 |
|||
5020 LET d=0: LET p=(u=0)-(u=1) |
|||
5021 LET l=(i AND u=0)+(la-i+u AND u=1) |
|||
5030 IF u=0 THEN IF a$(l+1)>=a$(l) THEN GO TO 5050 |
|||
5031 IF u=1 THEN IF a$(l-1)<=a$(l) THEN GO TO 5050 |
|||
5040 LET d=1 |
|||
5042 LET t$=a$(l+p) |
|||
5043 LET a$(l+p)=a$(l) |
|||
5044 LET a$(l)=t$ |
|||
5050 LET l=l+p |
|||
5051 PRINT AT 10,21;a$(l);AT 12,0;a$ |
|||
5055 IF l<=la-i AND l>=i THEN GO TO 5023 |
|||
5061 LET i=i+NOT u |
|||
5063 LET u=NOT u |
|||
5064 IF d AND i<la THEN GO TO 5020 |
|||
5072 PRINT AT 12,0;a$ |
|||
9000 STOP </syntaxhighlight> |
|||
The traditional solution: |
|||
<syntaxhighlight lang="zxbasic"> 10 LET siz=32 |
|||
20 DIM d$(siz) |
|||
30 REM Populate d$ |
|||
40 FOR n=1 TO siz: LET d$(n)=CHR$ (48+INT (RND*75)): NEXT n |
|||
50 PRINT d$ |
|||
60 LET unSorted=0 |
|||
70 FOR i=1 TO siz-1 |
|||
80 IF d$(i)>d$(i+1) THEN LET t$=d$(i): LET d$(i)=d$(i+1): LET d$(i+1)=t$: LET unSorted=1 |
|||
90 NEXT i |
|||
100 IF unSorted THEN LET siz=siz-1: GO TO 60 |
|||
110 PRINT d$</syntaxhighlight> |
|||
{{omit from|GUISS}} |
{{omit from|GUISS}} |
||
{{omit from|Tiny BASIC}} |