Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
m (Added assumption to Standard ML solution) |
(Updated D entry) |
||
Line 714: | Line 714: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.algorithm; |
<lang d>import std.stdio, std.algorithm; |
||
void bubbleSort(T)(T[] data) pure nothrow { |
void bubbleSort(T)(T[] data) pure nothrow { |
||
auto itemCount = data.length; |
|||
bool hasChanged = false; |
bool hasChanged = false; |
||
do { |
do { |
||
hasChanged = false; |
hasChanged = false; |
||
itemCount--; |
itemCount--; |
||
foreach (i; 0 .. itemCount) |
foreach (immutable i; 0 .. itemCount) |
||
if (data[i] > data[i + 1]) { |
if (data[i] > data[i + 1]) { |
||
swap(data[i], data[i + 1]); |
swap(data[i], data[i + 1]); |
||
Line 729: | Line 729: | ||
} while (hasChanged); |
} while (hasChanged); |
||
} |
} |
||
void main() { |
void main() { |
||
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4]; |
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4]; |
||
Line 735: | Line 735: | ||
writeln(array); |
writeln(array); |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre> |
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre> |
||