Bubble sort: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m interwiki |
Nessun oggetto della modifica |
||
Riga 7:
Questo algoritmo è usato solo per scopi didattici o quando gli elementi da ordinare sono pochi poiché, siccome richiede una quantità di confronti troppo alta, risulta molto poco efficente (dell'ordine di ''n²'').
Seguono alcune implementazioni in vari [[Linguaggio di programmazione|linguaggi]].
=== [[linguaggio C|C]] ===
void bubble_sort(TYPE *array, int count) {
Riga 26 ⟶ 28:
}
''Due note di carattere tecnico:''
a e b sono variabili dichiarate come register, il chè significa che, se possibile, il [[compilatore]] C farà in modo che vengano collocate nei [[registro|registri]] della [[CPU]]; questo renderà più rapido l'accesso a dette variabili.
tmp è dichiarata di tipo int, quindi dovrà contenere interi; se l'array contiene elementi di tipo diverso, sarà sufficiente modificare la sua dichiarazione.
=== [[C plus plus|C++]] ===
template <class Type> void bubbleSort(Type *array, size_t length)
{
int i, j;
for(i = length - 1; i > 0; i--)
for(j = 0; j < i; j++)
if(array[j] > array[j+1]) /* compara gli elementi vicini */
{
array[j] ^= array[j+1]; // Usa XOR per scambiare i valori più velocemente,
array[j+1] ^= array[j]; // ma solo quando sei sicuro
array[j] ^= array[j+1]; // di poterlo fare (dipende dal tipo di dati)
}
}
template<typename T> void bubble_sort(T *base, size_t n) {
T *p, *q, t;
while (n--) {
for (q = (p = base) + 1; p < base + n; ++q, ++p) {
(*p > *q) && (t = *p, *p = *q, *q = t);
}
}
}
=== [[Linguaggio di programmazione Java|Java]] ===
public void bubbleSort(int [] array, int length)
{
int i, j;
for(i = length - 1; i > 0; i--)
for(j = 0; j < i; j++)
if(array[j] > array[j+1]) /* compara gli elementi vicini */
{
array[j] ^= array[j+1]; // Usa XOR per scambiare i valori più velocemente
array[j+1] ^= array[j];
array[j] ^= array[j+1];
}
}
===[[BASIC]]===
Sub Bubblesort(Array() as Integer, Length as Integer)
Dim I as Integer
Dim J as Integer
Dim Temp as Integer
For I = Length -1 To 1 Step -1
For J = 0 to I - 1
IF Array(J)>Array(J+1) THEN ' compara gli elementi vicini
Temp = Array(j)
Array(J) = Array(J+1)
Array(J+1) = Temp
End If
Next J
Next I
End Sub
===[[Perl]]===
sub bubble_sort(@) {
my @a = @_;
foreach $i (reverse 0..@#a) {
foreach $j (0..$i-1) {
($a[$j],$a[$j+1]) = ($a[$j+1],$a[$j]) if ($a[$j] > $a[$j+1]);
}
}
return @a;
}
===[[Python]]===
def bubblesort(iterable):
seq = list(iterable)
for passesLeft in range(len(seq)-1, 0, -1):
for index in range(passesLeft):
if seq[index] > seq[index + 1]:
seq[index], seq[index + 1] = seq[index + 1], seq[index]
return seq
===[[AppleScript]]===
-- Nota: AppleScript è 1-based, cioè il primo elemento di una lista è 1
--
on bubblesort( array )
repeat with i from length of array to 2 by -1 --> go backwards
repeat with j from 1 to i - 1 --> go forwards
tell array
if item j > item ( j + 1 ) then
set { item j, item ( j + 1 ) } to { item ( j + 1 ), item j } -- swap
end if
end tell
end repeat
end repeat
end bubblesort
[[Categoria:Algoritmi]]
|