Sorting algorithms/Bubble sort: Difference between revisions

m
→‎{{header|Standard ML}}: fix syntax highlighting
m (→‎{{header|Standard ML}}: fix syntax highlighting)
 
(24 intermediate revisions by 12 users not shown)
Line 1,243:
-31 0 1 2 2 4 65 83 99 782
</pre>
 
==={{header|Chipmunk Basic}}===
The [[#Commodore_BASIC|Commodore BASIC]] solution works without any changes.
 
==={{header|Commodore BASIC}}===
Line 1,301 ⟶ 1,304:
dim list[size]
 
gosub fill
let index = 0
gosub sort
gosub show
do
 
end
let list[index] = int: (rnd) * 100
 
sub fill
let index = index + 1
 
loop for index <= 0 to size - 1
 
let list[index] = int(rnd * 100)
do
 
next index
let sort = 0
 
return
let index = 0
 
sub sort
 
do
 
let temp1sort = index + 10
for index = 0 to size - 2
 
let temp1 = index + 1
if list[index] > list[temp1] then
 
letif temp2list[index] => list[indextemp1] then
let list[index] = list[temp1]
let list[temp1] = temp2
let sort = 1
 
let temp2 = list[index]
endif
let list[index] = list[temp1]
let list[temp1] = temp2
let sort = 1
 
endif
let index = index + 1
 
loop next index < size - 1
 
wait
 
loop sort = 1
 
return
let index = 0
 
sub show
do
 
printfor index ,"= :0 ",to list[index]size - 1
 
let print index =," index: +", 1list[index]
 
next index
loop index < size</syntaxhighlight>
 
return</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 1,400 ⟶ 1,408:
<pre>unsort -7 3 -4 -6 4 -1 -2 2 7 0 5 1 -3 -5 6
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>
=={{header|FTCBASIC}}==
<syntaxhighlight lang="basic">define sorting = 0, index = 0, size = 10, temp1 = 0, temp2 = 0
 
==={{header|FTCBASIC}}===
dim list[10]
<syntaxhighlight lang="basic">rem bubble sort benchmark example
rem compile with FTCBASIC
 
use time.inc
use random.inc
 
define const size = 32000
 
dim list[size]
 
define sorting = 0, index = 0, elements = 0
define timestamp = 0, sorttime = 0
define temp1 = 0, temp2 = 0
 
cls
 
print "Bubble sort benchmark test"
 
do
 
print "How many elements to generate and sort (max " \
print size \
print ")? " \
 
input elements
 
loop elements > size
 
gosub fill
gosub sort
 
print "done!"
print "sort time: " \
print sorttime
print "Press any key to view sorted data..."
 
pause
 
gosub output
 
Line 1,414 ⟶ 1,454:
 
sub fill
 
print "filling..."
 
0 index
Line 1,419 ⟶ 1,461:
do
 
gosub generaterand
print "Enter value #" \
print index \
print ": " \
 
inputlet @list[index] = rand
 
+1 index
 
loop index < sizeelements
 
return
 
sub sort
 
print "sorting..."
 
gosub systemtime
let timestamp = loworder
 
do
Line 1,456 ⟶ 1,501:
+1 index
 
loop index < sizeelements - 1
 
loop sorting = 1
 
gosub systemtime
let sorttime = ( loworder - timestamp ) / 18
 
return
 
sub output
 
print "printing..."
 
0 index
Line 1,468 ⟶ 1,518:
do
 
print "#" \
print index \
print ": " \
print @list[index]
 
+1 index
 
loop index < sizeelements
 
return</syntaxhighlight>
Line 1,711 ⟶ 1,758:
next i
end
</syntaxhighlight>
 
==={{header|microA BASIC}}===
{{works with|microA }}
<syntaxhighlight lang="basic">
'bubble sort in micro(A) BASIC
wcolor 0,0,0 : fcolor 150,180,240
var start,endtime,time
var sort,index,size,temp1,temp2,x,xl,y
size = 1000 : x = 10 : y = 20 : xl = 40
var list[size]
index = 1
GETTICK start
'---------------------------------------------
label do1
list[index] = rand(100)
index = index + 1
if index < size : goto do1 : endif
'---------------------------------------------
label do2
sort = 0
index = 1
label do3
'---------------------------------------------
temp1 = index + 1
if list[index] > list[temp1]
temp2 = list[index]
list[index] = list[temp1]
list[temp1] = temp2
sort = 1
endif
index = index + 1
if index < size - 1 : goto do3 : endif
'-----------------------------------------------
if sort = 1 : goto do2 : endif
'-----------------------------------------------
index = 1
GETTICK endtime
time = (endTime - start) / 1000
fcolor 230,140,120: print 300,10,time : swap
index = 970
'check sort /////////////////////////////////////
label do4
print x,y ,index : print xl,y, list[index]
y = y + 20 : swap
index = index + 1
swap
if index < 1000 : goto do4 :endif
'////////////////////////////////////////////////
</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|QuickBASIC}}
{{works with|Bywater BASIC|2.61}}
<syntaxhighlight lang="basic">
100 REM Sorting algorithms/Bubble sort
Line 1,769 ⟶ 1,866:
 
==={{header|QuickBASIC}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">
Line 1,805 ⟶ 1,903:
NEXT I
PRINT
END</syntaxhighlight>
END
</syntaxhighlight>
{{out}} (2 samples)
<pre>Before: 91 97 3 62 17 48 89 7 2 66
<pre>
BeforeAfter: 91 97 2 3 62 7 17 48 8962 66 789 91 2 6697</pre>
After<pre>Before: 22 60 245 44 354 93 784 1727 4821 62 66 89 91 9764
After: 21 22 27 44 45 54 60 64 84 93</pre>
</pre>
 
<pre>
==={{header|Quite BASIC}}===
Before: 22 60 45 44 54 93 84 27 21 64
<syntaxhighlight lang="qbasic">100 rem Sorting algorithms/Bubble sort
After: 21 22 27 44 45 54 60 64 84 93
110 LET n = 10
</pre>
120 array a
130 GOSUB 310
140 PRINT "unsort ";
150 GOSUB 360
160 rem Sort the array
170 GOSUB 210
180 PRINT " sort ";
190 GOSUB 360
200 END
210 rem Bubble sort the list A of length N
220 FOR i = 1 TO n-1
230 FOR j = 1 TO n-i
240 IF a[j] <= a[j+1] THEN GOTO 280
250 LET x = a[j]
260 LET a[j] = a[j+1]
270 LET a[j+1] = x
280 NEXT j
290 NEXT i
300 RETURN
310 rem Create a RANDOM list of N integers
320 FOR i = 1 TO n
330 LET a[i] = FLOOR(RAND(100))
340 NEXT i
350 RETURN
360 rem Print the list a
370 FOR i = 1 TO n
380 PRINT a[i];" ";
390 NEXT i
400 PRINT
410 RETURN</syntaxhighlight>
{{out}}
<pre>unsort 19 78 39 54 63 68 66 52 94 2
sort 2 19 39 52 54 63 66 68 78 94 </pre>
 
==={{header|RapidQ}}===
Line 1,853 ⟶ 1,983:
NEXT I
PRINT
END</syntaxhighlight>
END
</syntaxhighlight>
{{out}} (2 samples)
<pre>Before: 82 34 57 44 48 71 19 33 73 62
<pre>
BeforeAfter: 82 3419 5733 34 44 48 7157 1962 3371 73 6282</pre>
After<pre>Before: 194 3315 3496 4493 4827 5724 62 719 7380 8210 21
After: 4 9 10 15 21 24 27 80 93 96</pre>
</pre>
<pre>
Before: 4 15 96 93 27 24 9 80 10 21
After: 4 9 10 15 21 24 27 80 93 96
</pre>
 
==={{header|REALbasic}}===
Line 1,890 ⟶ 2,015:
 
==={{header|Run BASIC}}===
{{works with|QBasic|1.1}}
{{works with|Just BASIC}}
<syntaxhighlight lang="runbasic">siz = 100
dim data$(siz)
Line 1,897 ⟶ 2,024:
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
Line 2,021 ⟶ 2,148:
:End
:L<sub>1</sub>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">OPTION BASE 1
LET size = 10
DIM nums(0)
MAT REDIM nums(size)
RANDOMIZE
PRINT "Before:";
FOR i = 1 TO size
LET nums(i) = INT(RND*100)
PRINT USING " ##": nums(i);
NEXT i
PRINT
 
! Sort
LET counter = size
DO
LET changed = 0
FOR i = 1 TO counter-1
IF nums(i) > nums(i+1) THEN
LET tmp = nums(i)
LET nums(i) = nums(i+1)
LET nums(i+1) = tmp
LET changed = 1
END IF
NEXT i
LET counter = counter-1
LOOP WHILE (changed<>0)
 
! Display result
PRINT "After: ";
FOR i = 1 TO 10
PRINT USING " ##": nums(i);
NEXT i
PRINT
END</syntaxhighlight>
{{out}}
<pre>Similar as QuickBASIC entry.</pre>
 
==={{header|uBasic/4tH}}===
Line 2,283 ⟶ 2,448:
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10</pre>
 
=={{header|Befunge}}==
 
<syntaxhighlight lang="befunge">
000p&: v >20g:7g\1+7g2v
v00p7g00 _10p0>20p20g:7g\1+7g`|0p7+1g02p7g0<
>g1+00p&:^ |-\g00:+1g02 <
0 >$10gv
|-\g00:+1 <
>1->:7g\:#v_$>:#._25*,@
^ <
</syntaxhighlight>
 
=={{header|C}}==
Line 3,042 ⟶ 3,219:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
funcproc bubbleSort . arraya[] .
repeat
itemCountchanged = len array[]0
iffor itemCounti <= 1 to len a[] - 1
breakif 2a[i] > a[i + 1]
swap a[i] a[i + 1]
.
hasChanged$ changed = "false"1
itemCount -= 1
for index = 1 to itemCount
if array[index] > array[index + 1]
temp = array[index]
array[index] = array[index + 1]
array[index + 1] = temp
hasChanged$ = "true"
.
.
until hasChanged$changed = "false"0
.
.
array[] = [ 5 1 19 25 12 1 14 7 ]
call bubbleSort array[]
print array[]
</syntaxhighlight>
Line 3,355 ⟶ 3,525:
=={{header|Elena}}==
{{trans|C#}}
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 3,371 ⟶ 3,541:
madeChanges := false;
itemCount -= 1;
for(int i := 0,; i < itemCount,; i += 1)
{
if (list[i] > list[i + 1])
Line 3,748 ⟶ 3,918:
(1 2 3)
</syntaxhighlight>
 
=={{header|GDScript}}==
Here is an implementation of Bubble Sort algorithm in GDScript for array of primitive types:
<syntaxhighlight lang="gdscript">
extends Node2D
 
 
func BubbleSort(_array : Array) -> void:
for i in range(_array.size() - 1):
var swapped : bool = false
for j in range(_array.size() - i - 1):
if _array[j] > _array[j + 1]:
var tmp = _array[j]
_array[j] = _array[j + 1]
_array[j + 1] = tmp
swapped = true
if not swapped:
break
return
 
 
func _ready() -> void:
var array : Array = range(-10, 10)
array.shuffle()
 
print("Initial array:")
print(array)
 
BubbleSort(array)
 
print("Sorted array:")
print(array)
return
</syntaxhighlight>
 
Possible output:
{{out}}
<pre>
Initial array:
[-7, -6, 2, -8, 4, -1, -3, -5, 5, -10, -4, 7, 3, 8, 0, -9, 6, 9, -2, 1]
Sorted array:
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
 
If you need to sort objects, this can be done in way like this:
<syntaxhighlight lang="gdscript">
class Comparable:
var Value
 
func _init(_val):
self.Value = _val
 
func compare(_other : Comparable) -> int:
# Here is the simple implementation of compare
# for primitive type wrapper.
return self.Value - _other.Value
 
 
func BubbleSortObjects(_array : Array) -> void:
for i in range(_array.size() - 1):
var swapped : bool = false
for j in range(_array.size() - i - 1):
if _array[j].compare(_array[j + 1]) > 0:
var tmp = _array[j]
_array[j] = _array[j + 1]
_array[j + 1] = tmp
swapped = true
if not swapped:
break
return
 
</syntaxhighlight>
This approach is slow though. To sort array of primitive types use Array.sort() method instead.
To sort array of objects you can use Array.sort_custom(func : Callable) method.
 
=={{header|Go}}==
Line 4,067 ⟶ 4,312:
 
For the most part, bubble sort works against J's strengths. However, once a single pass has been implemented as a list operation, <code>^:_</code> tells J to repeat this until the result stops changing.
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn bubble_sort<T>(anon mut array: [T]) {
mut item_count = array.size()
mut has_changed = true
while item_count > 1 and has_changed {
has_changed = true
item_count--
for i in 0..item_count {
if array[i] > array[i + 1] {
let temp = array[i]
array[i] = array[i + 1]
array[i + 1] = temp
has_changed = true
}
}
}
}
 
 
fn main() {
mut array = [7, 8, 9, 6, 5, 3, 1, 10, 4, 2]
println("{}", array)
bubble_sort(array)
println("{}", array)
}
</syntaxhighlight>
 
=={{header|Janet}}==
Line 4,555 ⟶ 4,828:
 
1 2 3 4 5 6 7 8 9</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
bubble_sort(u) := block(
[n: length(u), swapped: true, temp],
while swapped do (
swapped: false,
for i: 1 thru n - 1 do (
if u[i] > u[i + 1] then (
temp: u[i],
u[i]: u[i + 1],
u[i + 1]: temp,
swapped: true
)
)
),
u
);
/* Example */
/* sample:[3,65,6,24,24,89,2,59,6]$
bubble_sort(%);
[2,3,6,6,24,24,59,65,89]
*/
</syntaxhighlight>
 
=={{header|MAXScript}}==
Line 6,382 ⟶ 6,679:
end
</syntaxhighlight>
 
=={{header|RPL}}==
The bubble algorithm is slow by construction, but since its RPL implementation uses basic stack handling, it is actually quicker than algorithms that work directly on the array.
{{works with|Halcyon Calc|4.2.7}}
≪ '''IF''' DUP SIZE 2 ≥ '''THEN'''
LIST→ → len
≪ len 1 '''FOR''' n
1 n 1 - '''START'''
'''IF''' DUP2 > '''THEN''' SWAP '''END'''
n ROLLD
'''NEXT'''
n ROLLD
-1 '''STEP'''
len →LIST
'''END''' ≫
 
=={{header|Ruby}}==
Line 7,145 ⟶ 7,458:
=={{header|Standard ML}}==
Assumes a list of integers.
<syntaxhighlight lang="sml">
<pre>
fun bubble_select [] = []
| bubble_select [a] = [a]
Line 7,153 ⟶ 7,466:
fun bubblesort [] = []
| bubblesort (x::xs) =bubble_select (x::(bubblesort xs))
</syntaxhighlight>
</pre>
 
=={{header|Stata}}==
Line 7,465 ⟶ 7,778:
=={{header|Wren}}==
Based on the pseudo-code in the Wikipedia article.
<syntaxhighlight lang="ecmascriptwren">var bubbleSort = Fn.new { |a|
var n = a.count
if (n < 2) return
Line 7,482 ⟶ 7,795:
}
 
var asarray = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
for (a in asarray) {
System.print("Before: %(a)")
bubbleSort.call(a)
23

edits