Bubble sort
From Algorithm wiki
| |
Contents |
[edit] Pseudocode
[edit] Deutsch(German Language)
prozedur bubbleSort( A : Liste sortierbarer Elemente )
n := Länge( A )
wiederhole
vertauscht := richtig
für jedes i von 1 bis n - 1 wiederhole:
falls A[ i ] > A[ i + 1 ] dann
vertausche( A[ i ], A[ i + 1 ] )
vertauscht := wahr
falls_ende
wiederhole_ende
n := n - 1
solange vertauscht und n >= 1
prozedur_ende
[edit] C Code
void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;
for (i = (array_size - 1); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
Bold text'Image:Italic text== C++ Code ==
[edit] Deutsch(German Language)
#include <iostream>
using namespace std;
void swap(int& a, int& b)
{
int z=a;
a=b;
b=z;
}
int main()
{
int size;
cout<<"Wie viele Elemente wollen sie eingeben?"<<endl;
cin>>size;
int* arr=new int[size];
for(int i=0; i<size; i++)
{
cout<<">";
cin>>arr[i];
}
cout<<"\n\nBeginne Ausgabe\n\n"<<endl;
for(int i=0; i < size; i++)
{
for(int j=1; j < size-i; j++)
if(arr[j] > arr[j-1])
swap(arr[j], arr[j-1]);
}
for(int i=0; i<size; i++) cout<<i<<endl; //Ausgeben
return 0;
}
[edit] Español(Spanish)
// bubble sort function
template<typename T>
void sort(T *array, size_t array_size, int (*comp)(T a, T b))
{
int cmp_result;
for(int i = array_size - 1; i > 0; --i)
{
for(int j = 0; j < i; ++j)
{
cmp_result = comp(array[j], array[j+1]);
if(cmp_result > 0)
{
swap(array[j], array[j+1]);
}
}
}
}
[edit] Italiano (Italian)
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <cstring>
#include <float.h>
#include <dos.h>
#include <windows.h>
#define N 5 //Definisco la costante N=5 cioè ad ogni occorrenza del carattere N viene sostituito il numero 5
using namespace std;
main(){
int tmp;
int array[N];
//Caricamento Array di dati da tastiera acquisiti tramite operatore "cin"
for(int i=0;i<N;i++){
cout<<"Posizione "<<i<<": ";
cin>>array[i];
}
//BubbleSort
for(int i=0;i<N;i++){
for(int j=0;j<N-1;j++){
if(array[j]>array[j+1]){
swap(array[j], array[j+1]);
}
}
}
//Stampa l'array di interi ordinato
for(int i=0;i<N;i++){
cout<<array[i]<<", ";
}
cout<<"\nPremi un tasto...";
getch(); //acquisisce un carattere ed esce
}
[edit] C# Code
//small to big sorting
int[] myArray = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 };
for( int j=1;j<myArray.Length;j ++ )
{
for(int i=0;i<myArray.Length - 1;i ++)
{
if( myArray[i]>myArray[i+1])
{
int temp = myArray[i];
myArray[i] = myArray[i+1];
myArray[i+1] = temp;
}
}
}
//big to small sorting
int[] myArray = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 };
for( int j=1;j<myArray.Length;j ++ )
{
for(int i=0;i<myArray.Length - 1;i ++)
{
if( myArray[i]<myArray[i+1])
{
int temp = myArray[i];
myArray[i] = myArray[i+1];
myArray[i+1] = temp;
}
}
}
[edit] Java Code1
public final class Bubble {
public static int[] bubble(int[] x) { //sorting
int length = x.length;
boolean changed = true;
while (changed) {
int count=0;
for(int j=0;j<=length-1;j++,length--)
for (int i = 0; i < length-1; i++) {
if(x[i]>x[i+1]){
int mid=x[i];
x[i]=x[i+1];
x[i+1]=mid;
count++;
}
}
if(count==0){
changed=false;
}
}
return x;
}
public static void bubbleShow(int[] x) { //print out
bubble
for(int i=0;i<x.length;i++){
System.out.print("x["+i+"]="+x[i]+",");
}
[edit] Java Code2
class BubbleSortAlgorithm extends SortAlgorithm {
void sort(int a[]) throws Exception {
for (int i = a.length; ++i>=0; )
for (int j = 0; j>i; j++) {
if (stopRequested) {
return;
}
if (a[j] > a[j+1]) {
int T = a[j];
a[j] = a[j+1];
a[j+1] = T;
}
compex(j, j+1);
pause(i);
}
}
}
[edit] VB Code
Public Function BubbleSort() As Integer
Dim Vec(1 To 10) As Integer
Dim aux As Integer
For i = 1 To 10 - 1
For j = 1 To 10 - 1
If (Vec(j) > vec (j+1)) then
aux = vec(j)
vec(j) = vec (j+1)
vec(j+1) = aux
End If
Next j
Next i
End Function
des is foisch du noob
[edit] Perl Code
@data = qw(A L G O R I T H M);
do {
$j = 1;
$action = 0;
for ($i = 0; $i < $#data; $i++) {
if ($data[$i] gt $data[$j]) { # use gt/lt for strings or >/< for numbers
@data[$i,$j] = @data[$j,$i];
$action = 1;
}
$j++;
}
} while ($action);
print "@data";
[edit] Prolog Code
bubblesort([], []). bubblesort(L, SL) :- bubbleup(L, ZL), !, bubblesort(ZL, SL). bubblesort(SL, SL). bubbleup([X, Y|L], [Y, X|L]) :- X > Y. bubbleup([Z|L], [Z|ZL]) :- bubbleup(L, ZL).
Quelle: http://www.stefan-baur.de/cs.algo.bubblesort.html
[edit] Python Code
def bubblesort(l):
"Sorts l in place and returns it."
for passesLeft in range(len(l)-1, 0, -1):
for index in range(passesLeft):
if l[index] > l[index + 1]:
l[index], l[index + 1] = l[index + 1], l[index]
return l
[edit] Python Code - rekursiv
def bubbleSort(sequenz,rechts):
if rechts==0:
return sequenz
getauscht= False
for index in range(rechts):
if sequenz[index] > sequenz[index + 1]:
sequenz[index], sequenz[index + 1] = sequenz[index + 1], sequenz[index]
getauscht= True
if not getauscht:
return sequenz
else:
return bubbleSort(sequenz,rechts-1)
if __name__=="__main__":
# Selbsttest
print "Test - Sortieren durch Vertauschen von Nachbarn"
array=['Y', 'M', 'D', 'A', 'Z', 'G', 'Q']
print "unsortiert:",array
print "sortiert: ",bubbleSort(array,len(array)-1)
[edit] Ruby Code
module BubbleSort
def self.sort(keys)
sort!(keys.clone)
end
def self.sort!(keys)
0.upto(keys.size-1) do |i|
(keys.size-1).downto(i+1) do |j|
(keys[j], keys[j-1] = keys[j-1], keys[j]) if keys[j] < keys[j-1]
end
end
keys
end
end
[edit] Ada Code
type Realarray is array (Integer range <>) of Float;
procedure Bubble_Sort (A : in out Realarray) is
Help : Float;
begin
for I in 1 .. (A'Last - A'First) loop
for J in A'First .. (A'Last - I) loop
if A (J) > A (J + 1) then
Help := A (J);
A (J) := A (J + 1);
A (J + 1) := Help;
end if;
end loop;
end loop;
end Bubble_Sort;
[edit] Pascal/Delphi Code
procedure BubbleSort(var A:array of Integer);
Var
i, j, z: Integer;
begin
for i:= 0 to pred(high(A)) do
for j := i + 1 to high(A) do
if(A[i] > A[j])then
begin
z := A[i];
A[i] := A[j];
A[j] := z;
end;
end;
[edit] Basic Code
By Dave Stratford
The bubble sort is inherently inefficient. This version uses a number of coding tricks to marginally improve on that inefficiency.
DEF PROC_BubbleSort(Size%)
I%=Size%+1
REPEAT
I%-=1
LastChange%=2
FOR J% = 2 TO I%
IF data%(J%-1) > data%(J%) THEN
SWAP data%(J%-1),data%(J%)
LastChange%=J%
ENDIF
NEXT J%
I%=LastChange%
UNTIL I% = 2
ENDPROC

