Sorting algorithms/Bubble sort: Difference between revisions

m
→‎{{header|Standard ML}}: fix syntax highlighting
m (→‎{{header|Minimal BASIC}}: {{works with|Bywater BASIC|2.61}})
m (→‎{{header|Standard ML}}: fix syntax highlighting)
 
(9 intermediate revisions by 7 users not shown)
Line 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,207 ⟶ 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,520 ⟶ 3,525:
=={{header|Elena}}==
{{trans|C#}}
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 3,536 ⟶ 3,541:
madeChanges := false;
itemCount -= 1;
for(int i := 0,; i < itemCount,; i += 1)
{
if (list[i] > list[i + 1])
Line 3,913 ⟶ 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,232 ⟶ 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,720 ⟶ 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 7,326 ⟶ 7,458:
=={{header|Standard ML}}==
Assumes a list of integers.
<syntaxhighlight lang="sml">
<pre>
fun bubble_select [] = []
| bubble_select [a] = [a]
Line 7,334 ⟶ 7,466:
fun bubblesort [] = []
| bubblesort (x::xs) =bubble_select (x::(bubblesort xs))
</syntaxhighlight>
</pre>
 
=={{header|Stata}}==
Line 7,646 ⟶ 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,663 ⟶ 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