Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(Undo revision 101335 by 208.80.119.67 (talk) — For this task, I think it is more to the point to have the algorithm)
(Cleaned D code)
Line 318: Line 318:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio: writefln;
<lang d>import std.stdio, std.algorithm;

void insertionSort(T)(T[] A) {
void insertionSort(Range)(Range data) {
for(int i = 1; i < A.length; i++) {
foreach (i; 1 .. data.length) {
T value = A[i];
auto value = data[i];
int j = i - 1;
int j = i - 1;
while (j >= 0 && A[j] > value) {
while (j >= 0 && data[j] > value) {
A[j + 1] = A[j];
data[j + 1] = data[j];
j = j - 1;
j--;
}
}
A[j+1] = value;
data[j + 1] = value;
}
}
}
}

void main() {
void main() {
auto a1 = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
insertionSort(a1);
insertionSort(array);
writefln(a1);
writeln(array);
auto a2 = [4.0,65.0,2.0,-31.0,0.0,99.0,2.0,83.0,782.0,1.0];
insertionSort(a2);
writefln(a2);
}</lang>
}</lang>
Output:
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>

=={{header|E}}==
=={{header|E}}==
{{Lines_too_long}}
{{Lines_too_long}}