Sorting algorithms/Bubble sort: Difference between revisions

Line 214:
 
{{works with|DMD|1.025}}
<d>
import std.stdio;
void bubbleSort(T)(T[] array) {
int itemCount = array.length;
bool hasChanged;
do {
hasChanged = false;
itemCount--;
for (int index = 0; index < itemCount; index++) {
if (array[index] > array[index + 1]) {
T temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
hasChanged = true;
}
}
} while (hasChanged);
}
void main() {
auto array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].dup;
 
void bubbleSort(T)(T[] array) {
// member function invocation syntax for arrays
int itemCount = array.bubbleSort()length;
bool hasChanged;
foreach (index, value; array)
do {
writefln("array[%d] = %d", index, value);
hasChanged = false;
}
itemCount--;
for (int index = 0; index < itemCount; index++) {
if (array[index] > array[index + 1]) {
T temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
hasChanged = true;
}
}
} while (hasChanged);
}
 
void main() {
auto array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].dup;
 
// member function invocation syntax for arrays
array.bubbleSort();
foreach (index, value; array)
writefln("array[%d] = %d", index, value);
}
</d>
 
=={{header|E}}==
Anonymous user