Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(Add language: /.ROFF/.) |
(Added Algol W) |
||
Line 240: | Line 240: | ||
abcdefghiijklmnopqrstuvwxyz |
abcdefghiijklmnopqrstuvwxyz |
||
big fjords vex quick waltz nymph |
big fjords vex quick waltz nymph |
||
</pre> |
|||
=={{header|ALGOL W}}== |
|||
External in-place insertion sort routine for integers. From the pseudo code but with variable bounds. |
|||
<lang algolw>% insertion sorts in-place the array A. As Algol W procedures can't find the bounds % |
|||
% of an array parameter, the lower and upper bounds must be specified in lb and ub % |
|||
procedure insertionSortI ( integer array A ( * ); integer value lb, ub ) ; |
|||
for i := lb + 1 until ub do begin |
|||
integer v, j; |
|||
v := A( i ); |
|||
j := i - 1; |
|||
while j >= lb and A( j ) > v do begin |
|||
A( j + 1 ) := A( j ); |
|||
j := j - 1 |
|||
end while_j_ge_0_and_Aj_gt_v ; |
|||
A( j + 1 ) := v |
|||
end insertionSortI ;</lang> |
|||
Test the insertionSortI procedure. |
|||
<lang algolw>begin |
|||
% external in-place insertion sort procedure % |
|||
procedure insertionSortI ( integer array A( * ); integer value lb, ub ) ; |
|||
algol "ISORTI" ; |
|||
integer array d ( 1 :: 8 ); |
|||
integer p; |
|||
p := 1; |
|||
for i := 34, 2, -1, 0, 0, 9, -56, 3 do begin |
|||
d( p ) := i; |
|||
p := p + 1 |
|||
end for_i ; |
|||
insertionSortI( d, 1, 8 ); |
|||
write( i_w := 1, d( 1 ) ); |
|||
for i := 2 until 8 do writeon( i_w := 1, d( i ) ) |
|||
end.</lang> |
|||
{{out}} |
|||
<pre> |
|||
-56 -1 0 0 2 3 9 34 |
|||
</pre> |
</pre> |
||