Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
m (Redundant cat link)
Line 214: Line 214:


{{works with|DMD|1.025}}
{{works with|DMD|1.025}}
module BubbleSort;
import std.stdio;
import std.stdio;
void bubbleSort(T)(T[] array)
void bubbleSort(T)(T[] array) {
{
int itemCount = array.length;
int itemCount = array.length;
bool hasChanged;
bool hasChanged;
do {
do {
hasChanged = false;
hasChanged = false;
--itemCount;
itemCount--;
for (int index = 0; index < itemCount; ++index) {
for (int index = 0; index < itemCount; index++) {
if (array[index] > array[index + 1]) {
if (array[index] > array[index + 1]) {
T temp = array[index];
T temp = array[index];
Line 236: Line 233:
}
}
void main()
void main() {
auto array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].dup;
{

// array literal
auto array = [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ] ;
// member function invocation syntax for arrays
// member function invocation syntax for arrays
array.bubbleSort();
array.bubbleSort();
foreach (index, value; array) {
foreach (index, value; array)
writefln("array[%d] = %d", index, value);
writefln("array[%d] = %d", index, value);
}
}
}