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--; |
||
for (int index = 0; index < itemCount; ++ |
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() { |
||
⚫ | |||
{ |
|||
// array literal |
|||
⚫ | |||
// 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); |
||
} |
|||
} |
} |
||