Sorting algorithms/Bubble sort: Difference between revisions

fix bugs and nicer implementation
(fix bugs and nicer implementation)
Line 1,062:
 
=={{header|D}}==
<lang d>import std.stdio, std.algorithm : swap;
 
voidT[] bubbleSort(T)(T[] data) pure nothrow {
{
auto itemCount = data.length;
foreach_reverse (n; 0 .. data.length)
bool hasChanged = false;
{
 
do { bool swapped;
hasChangedforeach = false(i; 0 .. n)
itemCount--;
foreach (immutable i; 0 .. itemCount)
if (data[i] > data[i + 1]) {
swap(data[i], data[i + 1]);
hasChangedswapped = true;
}
} while if (hasChanged!swapped);
itemCount-- break;
}
return data;
}
 
 
void main() {
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
writeln(array.bubbleSort());
writeln(array);
}</lang>
{{out}}
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
 
 
=={{header|Dart}}==
Anonymous user