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 |
<lang d>import std.stdio, std.algorithm; |
||
void insertionSort( |
void insertionSort(Range)(Range data) { |
||
foreach (i; 1 .. data.length) { |
|||
auto value = data[i]; |
|||
int j = i - 1; |
int j = i - 1; |
||
while (j >= 0 && |
while (j >= 0 && data[j] > value) { |
||
data[j + 1] = data[j]; |
|||
j |
j--; |
||
} |
} |
||
data[j + 1] = value; |
|||
} |
} |
||
} |
} |
||
void main() { |
void main() { |
||
auto |
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4]; |
||
insertionSort( |
insertionSort(array); |
||
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}} |