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, std.algorithm;
<lang d>import std.stdio;

void insertionSort(Range)(Range data) {
void insertionSort(T)(T[] data) {
foreach (i; 1 .. data.length) {
foreach (i, value; data[1 .. $]) {
auto value = data[i];
auto j = i + 1;
ptrdiff_t j = i - 1;
for ( ; j > 0 && value < data[j - 1]; j--)
while (j >= 0 && data[j] > value) {
data[j] = data[j - 1];
data[j + 1] = data[j];
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];