Sorting algorithms/Bubble sort: Difference between revisions

m
<lang>
m (<lang>)
Line 17:
 
=={{header|ActionScript}}==
<codelang actionscript>
public function bubbleSort(toSort:Array):Array
{
Line 41:
return toSort;
}
</codelang>
 
=={{header|Ada}}==
{{works with|GCC|4.1.2}}
 
<codelang ada>
generic
type Element is private;
Line 91:
New_Line;
end Main;
</codelang>
 
=={{header|ALGOL 68}}==
Line 136:
 
Assume numbers are in a DIM of size "size" called "nums".
<codelang qbasic>
DO
changed = 0
Line 147:
END IF
LOOP WHILE(NOT changed)
</codelang>
 
=={{header|C}}==
 
<codelang c>
void swap(int *p)
{
Line 173:
} while (!sorted);
}
</codelang>
 
=={{header|C++}}==
{{works with|g++|4.0.2}}
 
<codelang cpp>
#include <iostream>
#include <algorithm>
Line 225:
std::cout << std::endl ;
}
</codelang>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|3.0+}}
<codelang csharp>
using System;
using System.Collections.Generic;
Line 273:
}
}
}
}</code>
</lang>
 
=={{header|Clean}}==
Line 298 ⟶ 299:
 
{{works with|DMD|1.025}}
<codelang d>
import std.stdio;
 
Line 326 ⟶ 327:
writefln("array[%d] = %d", index, value);
}
</codelang>
 
=={{header|E}}==
Line 395 ⟶ 396:
=={{header|Fortran}}==
 
<codelang fortran>
SUBROUTINE Bubble_Sort(a)
REAL, INTENT(in out), DIMENSION(:) :: a
Line 415 ⟶ 416:
END DO
END SUBROUTINE Bubble_Sort
</codelang>
 
=={{header|Haskell}}==
This version checks for changes in a separate step for simplicity, because Haskell has no variables to track them with.
<codelang haskell>
bsort :: Ord a => [a] -> [a]
bsort s = case _bsort s of
Line 427 ⟶ 428:
| otherwise = x:(_bsort (x2:xs))
_bsort s = s
</codelang>
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.
<codelang haskell>
bsort :: Ord a => [a] -> [a]
bsort s = case _bsort s of
Line 441 ⟶ 442:
Just xs2 -> Just $ x:xs2
_bsort _ = Nothing
</codelang>
 
=={{header|Java}}==
Bubble sorting (ascending) an array of any <tt>Comparable</tt> type:
<codelang java>
do{
boolean changed = false;
Line 457 ⟶ 458:
}
}while(!changed);
</codelang>
 
For descending, simply switch the direction of comparison:
<codelang java>
if(comparable[a].compareTo(comparable[b]) < 0){
//same swap code as before
}
</codelang>
 
=={{header|JavaScript}}==
 
<codelang javascript>
Array.prototype.bubblesort = function() {
var done = false;
Line 482 ⟶ 483:
return this;
}
</codelang>
 
{{works with|SEE|3.0}}
{{works with|OSSP js|1.6.20070208}}
<codelang javascript>
Array.prototype.bubblesort = function() {
var done = false;
Line 502 ⟶ 503:
return this;
}
</codelang>
 
Example:
<codelang javascript>
var my_arr = ["G", "F", "C", "A", "B", "E", "D"];
my_arr.bubblesort();
print(my_arr);
</codelang>
 
Output:
Line 557 ⟶ 558:
=={{header|Modula-3}}==
 
<codelang modula3>
MODULE Bubble;
 
Line 578 ⟶ 579:
END sort;
END Bubble.
</codelang>
 
=={{header|OCaml}}==
Line 584 ⟶ 585:
 
This version checks for changes in a separate step for simplicity.
<codelang ocaml>
let rec bsort s =
let rec _bsort = function
Line 596 ⟶ 597:
if t = s then t
else bsort t
</codelang>
 
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.
<codelang ocaml>
let rec bsort s =
let rec _bsort = function
Line 617 ⟶ 618:
| None -> s
| Some s2 -> bsort s2
</codelang>
 
=={{header|Perl}}==
{{works with|Perl|5.8.8}}
<codelang perl>
# Sorts an array in place and returns a copy
sub bubble_sort (@) {
Line 634 ⟶ 635:
return @_;
}
</codelang>
<codelang perl>
# usage
@a = qw/G F C A B E D/;
bubble_sort(@a);
</codelang>
 
Alternate "Long Hand" Perl Method
 
<codelang perl>
sub Bubble_Sort {
my @list = @_;
Line 665 ⟶ 666:
return @list;
}
</codelang>
<codelang perl>
# usage
my @test = (1, 3, 256, 0, 3, 4, -1);
print join(",", Bubble_Sort(@test));
</codelang>
 
=={{header|Pop11}}==
Line 695 ⟶ 696:
 
=={{header|Python}}==
<codelang python>
def bubble_sort(seq):
"""Inefficiently sort the mutable sequence (list) in place.
Line 722 ⟶ 723:
bubble_sort(testcase)
assert testcase == testset # we've unshuffled it back into a copy
</codelang>
 
=={{header|Ruby}}==
Although the native Ruby sort method for Arrays if much faster (O(n*log(n)) versus O(n**2)), you can find a Ruby version of Bubble sort hereunder. It adds the bubblesort! method to the Array object. Below are two different methods that show four different iterating constructs in ruby.
 
<codelang ruby>
class Array
def bubblesort1!
Line 740 ⟶ 741:
return self
end
</codelang>
<codelang ruby>
def bubblesort2!
each_index do |index|
Line 753 ⟶ 754:
end
end
</codelang>
<codelang ruby>
puts [3, 78, 4, 23, 6, 8, 6].bubblesort1!
# => [3, 4, 6, 6, 8, 23, 78]
</codelang>
 
=={{header|Scheme}}==
<codelang scheme>
(define (bubble-sort x gt?)
(letrec
Line 776 ⟶ 777:
 
(fix sort-step x)))
</codelang>
 
This solution iteratively finds the fixed point of sort-step. A comparison function must be passed to bubblesort. Example usages:
<codelang scheme>
(bubble-sort (list 1 3 5 9 8 6 4 2) >)
(bubble-sort (string->list "Monkey") char<?)
</codelang>
 
=={{header|Seed7}}==
Line 809 ⟶ 810:
A straight translation from the pseudocode above. Swap is done with a [[wp:Smalltalk#Code_blocks|block closure]].
 
<codelang smalltalk>
|item swap itemCount hasChanged|
item := #(1 4 5 6 10 8 7 61 0 -3) copy.
Line 828 ⟶ 829:
hasChanged := true]].
hasChanged] whileTrue.
</codelang>
 
=={{header|Toka}}==
Line 874 ⟶ 875:
 
=={{header|UnixPipes}}==
<codelang bash>
rm -f _sortpass
 
Line 898 ⟶ 899:
 
cat to.sort | bubblesort
</codelang>
973

edits