Sorting algorithms/Bubble sort: Difference between revisions

D code ported to D2
(D code ported to D2)
Line 605:
 
=={{header|D}}==
<lang d>import std.stdio, std.algorithm;
{{works with|DMD|1.025}}
 
<lang d>import std.stdio;
void bubbleSort(TRange)(T[]Range arraydata) {
int itemCount = arraydata.length;
bool hasChanged = false;
 
void bubbleSort(T)(T[] array) {
int itemCount = array.length;
bool hasChanged;
do {
hasChanged = false;
itemCount--;
forforeach (int index =i; 0; index <.. itemCount; index++) {
if (arraydata[indexi] > arraydata[indexi + 1]) {
Tswap(data[i], tempdata[i =+ array[index1]);
array[index] = array[index + 1];
array[index + 1] = temp;
hasChanged = true;
}
}
} while (hasChanged);
}
 
void main() {
auto array = [1028, 944, 846, 724, 619, 52, 417, 311, 225, 14].dup;
 
// member function invocation syntax for arrays
array.bubbleSort();
foreach writeln(index, value; array);
writefln("array[%d] = %d", index, value);
}</lang>
Output:
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
 
=={{header|E}}==
Anonymous user