Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(Improved D code) |
(Updated D code) |
||
Line 308: | Line 308: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio |
<lang d>import std.stdio; |
||
void insertionSort( |
void insertionSort(T)(T[] data) { |
||
foreach (i; 1 .. |
foreach (i, value; data[1 .. $]) { |
||
auto |
auto j = i + 1; |
||
for ( ; j > 0 && value < data[j - 1]; j--) |
|||
data[j] = data[j - 1]; |
|||
data[j] = value; |
|||
j--; |
|||
} |
|||
data[j + 1] = value; |
|||
} |
} |
||
} |
} |
||
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]; |